[OnePlus 8 Pro Questions & Answers] Zweikeks:
Finally, I was able to do what the title says: Restore my backup on a virgin phone, keeping all data, apps and settings intact.

In short: package-restrictions.xml must not be replaced by the one in the backup. This prevents the bootloop.

See below for step by step instructions. I was able to restore a phone multiple times in this way. But I am still not 100% sure that it is working in any case.

It turns out that the many fatal errors and crashes I see in the logcat at first boot are normal. Even if the phone boots, there are tons of crashes.

In general, as described in various instructions, TWRP - since several years - no longer seems to be able to restore the OS in a bootable state. So first the OS is installed as a local update from a .zip and then the data partition is restored with TWRP.

This way OS and data do not match completely, metadata in the file system is not correct. The whole process relies on the OS to fix itself. During boot (extended) attributes are jerked back into place by various mechanisms.

Probably nobody looks under the hood to watch the drama - when in the end it boots.

(Important SELinux file contexts are not corrected by the OS / these mechanisms. Contrary to my earlier assumptions, TWRP includes the SELinux file contexts (and other stuff?) in the backup using its special built-in tar.)

Another drama of Linux in general is that no tool is able anymore to make a complete copy of objects in the file system - really complete with all metadata. cp, tar, various sync tools are all not working completely. File systems have evolved. Tools did not. Your best bet is mv (move) to preserve most of the stuff that is attached to file objects in a modern Linux. Yeah 'mv' moves, does not copy. So, as I said, there is no copy anymore...

I have not researched what exactly interferes with the OS when I restore the foreign package-restrictions.xml.

I assume that the package-restrictions.xml must match the file system, as it contains references to inodes. And I assume that (due to a bug or on purpose) the OS doesn't land on its feet at this point, as it does with so many other things on first boot.

Initial situation:

- Source: OnePlus 8 Pro, model IN2023, 128GB

- Target: OnePlus 8 Pro, model IN2023, 256GB

- OxygenOS / Android 13, IN2023_11_F.13

- I am using TWRP 3.7.0 (twrp_op8_erofs-3.7.0-b3.zip), temporarily running (fastboot boot recovery.img).

Backup of source:

- SIM card is in the slot.

- During the backup there is no password/pin/unlock pattern set (Settings, Password & security, all off or not enrolled).

- During the backup RAM Expansion has to be turned off on source devices with 256GB (Settings, About device, RAM, disable RAM expansion, reboot).

- Partition Data backed up with TWRP.

- Internal shared storage (/data/media) archived to .tar.gz with parameters -cpvzf.

Restore to target:

- No SIM card is in the slot.

- RAM expansion off or on, doesn't matter.

- Bootloader has been unlocked.

- If rooting is planned, it should already be done at this point (flash bootloader patched by magisk).

- On my phones I turned off verification. I'm not sure if this is necessary. (1)

- Format data. (2)

- Copy the backup of the data partition and the backup of the internal shared storage to the target. (3)

- Start TWRP. Move package-restrictions.xml to the root (ramdisk of TWRP).

Advanced, Terminal, mv /data/system/users/0/package-restrictions.xml /

- Restore the data partition with TWRP.

- Move back package-restrictions.xml, overwriting the one from the backup.

Advanced, Terminal, mv package-restrictions.xml /data/system/users/0

- Extract the backup of the internal shared storage to /data/media. (4)

- Remove an attached USB OTG stick. Pray and boot the phone.

The first boot takes quite a long time, about 1.5min. Then says 'turn off' at the bottom of the screen.

Twitches around funny with a blank screen. When I pressed Power, a very strange power menu appeared (the normal one briefly in the background).

According to logcat, the first boot is really scary. It is full of fatal error messages and crashes.

Press restart in the strange power menu. Now it boots normally quite fast. Logcat is ok. Everything works as usual.

Aftermath:

- You may want to disable the RAM Expansion

- You may want to disable apps/packages (5)

(1) fastboot --disable-verification flash vbmeta vbmeta.img. The vbmeta.img extracted from the local update .zip-package from which the OS has been installed.

(2) When the phone has booted to 'Hello' from the settings wizard, you can simply switch it off - press VolUp + Power simultaneously for about 12 seconds.

Format data of TWRP doesn't work. Leads to a bootloop. Use the stock recovery. If the touchscreen of the stock recovery doesn't work see here:

https://xdaforums.com/t/twrp-restore-not-working-after-formatting-data-solved.4615229/#post-88862585

The bootloader isn't touched by format data. The phone is still unlocked and the bootloader patched if it was before.

(3) The files for TWRP must reside in the typical folder structure TWRP/BACKUPS/<serial number of target>/<date-name>/data*

Instead of copying, a USB stick with an OTG cable can be used to hold the files. The stick must be removed before booting into the OS!

Strangely enough, the OS otherwise creates the directory structure of the internal shared storage on the stick.

(4) I have used tar with parameters to preserve DAC (Discretionary Access Control, the traditional rwx flags stuff, -cpvzf, -xpvzf). But all the rest of file systems metadata is not restored. This seems to be working, though.

You can delete the _contents_ of /data/media/0 before filling it again (cd /data/media/0, rm -r *). But do not remove/recreate the folder /data/media/0 itself.

Otherwise the OS throws away your data and generates a fresh new internal shared storage.

(5) Needed since package-restrictions.xml was not taken over from the backup. Settings, Apps, App management, <app>, Disable or pm uninstall/disable etc.

If you want to look into the package-restrictions.xml: https://github.com/cclgroupltd/android-bits/tree/main/ccl_abx

(The format is ABX, not AXML.)

To summarize, these directories/files are not replaced by the ones from the backup. Otherwise you get a bootloop:

Not included in the .win??? anyway:

/data/misc/keystore

/data/misc/vold

/data/unencrypted

This is included and requires measures as described above:

/data/system/users/0/package-restrictions.xml

Also note that TWRP never includes the internal shared storage /data/media/0 in the backup (the .win???).

I have not tested it the other way round, backup of a 256GB variant restored on the 128GB variant, but I see no reason why this should not work (except of course if there is not enough memory due to one's own data in the internal shared storage).

The OnePlus 8 Pro has this slot mimic. I never noticed that the OnePlus 8 Pro had changed slots. Or that I should have manually done so. Especially since with the method above the system parts with slots are altered using regular updates. (The data partition has no slots.)

Good Luck!

Zweikeks