[Xiaomi Mi 9 Guides, News, & Discussion] seemebreakthis:
My phone ran into problems after I upgraded Magisk (I was on Canary build). No problem I figured, I will just restore from my TWRP backup.

... only issue was, I have never restored from a TWRP backup. And it turned out to be not as straightforward as I thought. I ended up learning a lot about the process to be able to successfully backup AND RESTORE. So I thought I might write something here to make it easier for you to do this, so you don't have to learn it the hard way like I did.

First I think most of us know data storage is not backed up by default. The partitions selected by TWRP for you (i.e. system, data, vendor, and boot) are sufficient to save an image of your OS plus the installed apps, but not app data (specifically /data/media and everything underneath, so actually this is dependent on how each app stores its data, some or all of an app's data may be stored in /data/data and not /data/media. /data/data IS backed up by TWRP.), and the excluded portion contains your own personal data as well, i.e. the internal storage.

Simply put, if you want a so-called 'full backup', or a complete snapshot of your phone, you will want to do something more than simply a TWRP backup. See below.

Another thing to note is that the location you are backing up to (and I recommend external storage like a USB-OTG device) may *not* have enough space. And while backing up you may not even realize that you have run out of space (as there is no warning particularly with the tar command which I will detail below) therefore your backup may become corrupt ! Take extra precaution to validate the integrity of your backup, and make sure there is always enough space to create the backup. TWRP *does* warn you about lack of space though.

Last but not least, if you have a password (either pattern or numeric) to lock your device, after restoring it is gone. So first thing after restore is to go into settings to setup the password once again. Interestingly the fingerprint data is still there. Once you set up a password the fingerprints will reattach to it. For the apps that have fingerprint login registered, you will need to re-register as the app thinks you have updated your password/fingerprint.

If you don't set up your password after restoring, in TWRP it will just go straight into the main menu as if no password has been set, so basically anyone who knows how TWRP works will be able to access your data if they get a hold of your phone.

Oh one more thing. The format of the backup media (e.g. usb-otg) has to be ex-FAT. Very important. I got badly burned because I had FAT32. And like I said the backup process did NOT warn me at all that this could be a potential issue. Not the tar command anyway.

OK here is the HOW on BACKing UP:

Backup using TWRP should be easy enough. It is documented just above everywhere (restoring is another story however). Just make sure you mount your USB-OTG before starting your backup, and pick USB-OTG as your destination. Let TWRP choose the partitions to be backed up.

This will create a TWRP folder with subfolders, one of which containing the actual backup files.

On to your personal data - in TWRP (while your USB is still mounted), go to Advanced, Terminal, then issue this command: "tar -cpvzf /usb_otg/data_media_backup.tar.gz /data/media"

This will create a .tar.gz file. This is your own data (and like I said some apps write to this space too so it is also app backup).

Validate the integrity of the .tar.gz file with this command: "tar -tf /usb_otg/data_media_backup.tar.gz". If you see no error message then you are good.

Unmount your usb finger, and take it out before leaving TWRP. Otherwise the usb finger may need repairing when you access it in say Windows. And that's always a scary thing.

If you need to archive the backup somewhere in your PC etc, copy 2 things from your USB: The TWRP folder, or at least the subfolder that contains your TWRP backup files (copy the entire folder, or better yet the entire folder structure); and the .tar.gz file. This forms your complete backup image.

Here is the HOW on RESTORING:

Flash MIUI (or whatever your OS is), with matching version as the one in your backup image. i.e. you have a vanilla enviroment that is the same version as the one in your backup.

If for some reason you can't boot into your system afterwards, go into recovery (can be built in recovery, not necessarily TWRP), then wipe your DATA partition clean. Now you should be able to boot.

Boot up the vanilla system. When the first spalsh screen is shown ("welcome to the OS, let's start setting things up", or something like that) just long press the power button to shutdown or restart your phone. Then go into bootloader (lower volume + power buttons for my phone).

Install TWRP, reboot to recovery / TWRP, then do your TWRP restore. Don't forget to mount your otg-usb first ! if the folder structure is there in your usb, or even if you just have that one folder with the backup files underneath, the restore function will show that folder for you to pick. Just pick it then proceed with the restore.

in TWRP (while your USB is still mounted), go to Advanced, Terminal, then issue this command: "rm -r /data/media/0/*" (this is why step 3 was necessary)

if there are other folders underneath /data/media, repeat the rm command once for each of these folders

"cd /"

"tar -xvzf /usb_otg/data_media_backup.tar.gz" <-- this will restore your data !!

Reboot. Voala you have your backed-up system in action once again. Nothing in your phone is different than before the backup, except for the pattern password which is now gone. And you better set it up again right away.

Hope this helps !!