How to back up & restore a rooted phone? Before handing in my OnePlus 6T for repair, I want to create a full system backup and reset the phone to factory defaults. Afterwards, I want to restore the original state. My phone is rooted with Magisk.
How do I do a full system backup?
Note that this is different from a backup as part of migration to a new phone or a partial backup as part of fixing software issues.
Note: This method will not work with work profiles or parallel apps!
Here's how I performed the backup on a OnePlus 6T in 2020:

Downloaded TWRP for my device.

Rebooted the phone into fastboot mode by pressing volume down
plus the power button and following the menu displayed on the
phone. Note the process of entering fastboot mode varies from
phone to phone.

Connected the phone to the PC, then booted TWRP from the Windows
command line (cmd.exe):
fastboot boot twrp-3.4.0-0-fajita.img

I installed fastboot as part of the Chocolatey package adb.

In TWRP, I entered my password, then selected Backup.

Backed up all partitions to internal storage. See also the TWRP
FAQ: What should I back up in TWRP?
Note that not all partitions need to be backed up, and in fact,
according to the TWRP FAQ, it may be a bad idea to restore certain
partitions. Still, it doesn’t hurt to have the backup of all of
them.
Not included is /data/media (internal storage). This needs to be
backed up separately. See the TWRP FAQ: What is EXCLUDED from a
TWRP backup?

With TWRP running, I connected the phone via USB to my PC. Then I
moved the following folder to my PC, which contains the TWRP
backup of the phone:
This PC\OnePlus A6013\Internal Storage\TWRP

This step is not strictly necessary as in the following step I
did back up the entire internal storage. However, it makes the
next step execute faster as less data needs to be copied. The next
step does not give feedback about progress.

For backing up internal storage I decided on the method found in
Tadej Janež’s article Backing up Android's /data/media (i.e.
internal storage) using adb and TWRP. From the Windows command
line I ran:
adb exec-out &quot;tar --create data/media/0 2&gt;/errors.txt | gzip&quot; &gt;internal_storage.tar.gz

After completion, to check for errors, I ran (no errors):
adb shell cat /errors.txt


To leave TWRP, I rebooted the phone. Note that it is not
necessary to install TWRP.


Restoration still needs to be tested.