Android: manually restoring apps from a TWRP backup

Since an issue with the keyboard freezing for Android disk encryption was fixed in the Omnirom code base, I was able to upgrade my system again. Unfortunately, the system didn't boot anymore without a factory reset. Therefore, I finally found out to manually restore individual apps from a system backup performed with TWRP.

The backup option of TWRP produces different files for the different partitions of your device. These files are (for backups without compression) simple tar archives of the data on your phone. In case of large volumes, the archives might be split into multi-part tar archives. A listing of the backup directory might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
boot.emmc.win
boot.emmc.win.md5
data.ext4.win000
data.ext4.win000.md5
data.ext4.win001
data.ext4.win001.md5
data.ext4.win002
data.ext4.win002.md5
data.info
recovery.log
system.ext4.win
system.ext4.win.md5
system.info

The app data is stored inside the data partition. So the first step is to extract this partition:

1
tar -xvf data.ext4.win000

This will result in a folder called data which will eventually contain the application data in the subfolder data (yes, same name). In order to push data from individual application to the phone we first need to restart adb on the phone with root permissions:

1
adb root

Afterwards, individual application data folders can be pushed to the telephone, e.g.:

1
adb push data/data/com.example.app /data/data/com.example.app

Now the harder part begins. Android uses a single Unix user per application. First, the pushed files need to become owned bu the user of the application.

For this purpose, first the user id of the application needs to be found out. For this purpose the application needs to be installed already. On the phone, e.g. through adb shell do:

1
dumpsys package com.example.app | grep userId

This will print out the user id of the app, with which the files can be changed:

1
chown -R $id:$id /data/data/com.example.app

This is still not sufficient for the app to function properly again. If you try to launch the app now, chances are high that it complains about SQLite databases not being readable. This seems to be caused by SELinux, which is used by Android. The SELinux attributes of the files need to be restore, as described here:

1
restorecon -Rv /data/data/com.example.app

Finally, the app should be restored.