[Android Software/Hacking General [Developers Only]] BillGoss:
What this is: it's a shell script that generates commands for TWRP using its Open Recovery Script (ORS) language. See: https://twrp.me/faq/openrecoveryscript.html

Over the years I've found that I repeatedly did the same set of actions in TWRP, with just file names changing. Being inherently lazy I decided to automate these processes. So now I simply do the following in a Terminal session on my phone:

To backup my current rom: . bf

To backup my current rom and update my current rom (flash over the current rom) which is sort of like an OTA update: .bf -o

To NOT backup my current rom, restore a previous backup, and clean update the rom (wipe system, flash updated rom, Gapps, and Magisk): .bf -brf

To do a backup and clean install (wipe data and system) a rom: . bf -c

The script generates the ORS commands in /cache/recovery/openrecoveryscript and reboots the phone into recovery. TWRP will automatically process the file and, if there are no errors, reboot the system.

Requirements:

- the script must run as root (for access to /cache)

- flashed files must all be in a single directory (/sdcard/@install) and adhere to the following naming conventions:

File names (no spaces):

rom-*.zip

gapps-*.zip

kernel-*.zip (if flashed after Magisk)

Kernel-*.zip (if flashed before Magisk)

MyMods-*.zip (regular mod)

MyTemp-*.zip (one-off mod/fix)

su-*.zip (Magisk or other root installer)

Note: there must only be one file of each these types in the folder, if required. I normally prefix files that I'm not using with an x to avoid multiples, for example I'll have rom-lineage-17.1-20200223* and xrom-lineage-17.1-20200205* in the directory

- a system property (my.version) must be set as it's used to name new backups. I used to add the property to build.prop (using a MyMods zip) but now I use a Magisk MyMods module.

- two additional scripts (depending on the options chosen) are needed to be called in TWRP and must exist in /sdcard/Tools. They are:

rmpwd (remove the lock screen password after restoring a backup)

wipesys (wipes /system or /system_root because TWRP lost the 'wipe system' command - but not 'wipe data' - in version 3)

Limitations:

I've only used it on A-only systems. I have no idea if it works on A/B.

Design notes:

- this is not an executable script because files in /sdcard can't be. But I wanted it (and all my other scripts) in /sdcard so that it is easily accessible. That's why there are no exit statements (only returns) in it as it's run in-line using ". bf" or "source bf".

Usage notes:

- I recommend adding the directory to $PATH (export PATH=$PATH:/sdcard/Tools)

- many of the options (for example b,p,m,s,...) are toggles (on/off switches). They can easily be "flipped" by adding them to the command options. For example, -b is on by default, so specifying it again (. bf -b) turns it off (. bf -bb would leave it on).

- the -v option caters for the transition from one Android version to another. It makes it possible to have a sub-directory with the new version files (gapps, rom, mods), yet using the same su version. For example, if I'm currently on Android 9 but want to test Android 10, then I'd create a directory under /sdcard/@install called 10 (or Q or whatever). then I could switch from my current rom to a new 10 rom (clean install) on using .bf -cv 10 and the script will look in /sdcard/@install/10 for the gapps, mods, and rom. Always specify the -v option last in the list or separately (. bf -...v 10 or . bf -... -v 10).