Project

General

Profile

RestoreApplicationInternalData » History » Version 19

Denis 'GNUtoo' Carikli, 10/30/2020 02:43 PM
Improve backup

1 1 Denis 'GNUtoo' Carikli
h1. RestoreApplicationInternalData
2
3
{{toc}}
4
5
h2. /!\ Warning: Draft
6
7
This article is in draft form and is being written:
8
* Everybody is welcome to contribute
9
* Some things might not be accurate yet, so beware before using the information contained in it.
10 2 Denis 'GNUtoo' Carikli
11 15 Denis 'GNUtoo' Carikli
h2. Rationale
12 2 Denis 'GNUtoo' Carikli
13
In some case, it is useful to be able to restore internal applications data:
14 9 Denis 'GNUtoo' Carikli
15
For instance you might need to move the data of an application from a device to another if you want to switch device.
16
17
Another use case is if /data/system/packages.xml or /data/system/appops.xml get corrupted, applications can loose access to their data. This can make the launcher and other applications crash. 
18
19
So while it is possible to recover from that by wiping the data partition in the recovery, sometimes it's very impractical to do that because you might have important data like silence encryption keys and established sessions that you don't want to loose.
20 3 Denis 'GNUtoo' Carikli
21 16 Denis 'GNUtoo' Carikli
h2. Goals
22 1 Denis 'GNUtoo' Carikli
23 13 Denis 'GNUtoo' Carikli
This howto will explain how to move silence data from a device to another.
24 1 Denis 'GNUtoo' Carikli
25
For instance you could want to move from a Galaxy SIII (GT-I9300) to a Galaxy SII (GT-I9100) or vice versa, and you might not want to recreate keys, sessions, etc when moving device.
26
27 16 Denis 'GNUtoo' Carikli
This could also be adapted to restore application data from a block device level backup of the internal eMMC or the internal data partition.
28
29 14 Denis 'GNUtoo' Carikli
h2. Silence
30 1 Denis 'GNUtoo' Carikli
31 14 Denis 'GNUtoo' Carikli
Silence has been chosen as an example for this tutorial because:
32
* It's an application commonly used
33
* Loosing its data (key, sessions) can be painful
34
35 1 Denis 'GNUtoo' Carikli
Silence stores its data in the internal application storage. As far as I know it's not supposed to store any data on the microSD or user storage beside potential backups.
36
37
It might be interesting to make additional tutorial for other cases. For instance for:
38
* Applications that also require data to be on the microSD or user storage.
39
* System applications that have their data in a database.
40 10 Denis 'GNUtoo' Carikli
41 14 Denis 'GNUtoo' Carikli
h2. Requirements 
42
43
This howto assumes that the data partition is unencrypted. If you know how to open encrypted data partition in the recovery, or in a GNU/Linux distribution, it would be great to either modify this tutorial to add information on how to do it, or contact us on the mailing list or through the bugreporting system about it.
44
45
h2. TODO
46
47 5 Denis 'GNUtoo' Carikli
* Explain why we need the uid/gid
48 1 Denis 'GNUtoo' Carikli
* Explain why the ls -ld gives the application uid/gid
49
* Point to how to get root
50
* Explain how to handle a corrupted /data/system/packages.xml and /data/system/appops.xml
51
* Explain how to mount a full backup, and why not to restore full backup completely
52
* Explain how and why create a tarball of the application data
53
54
Once we have a tarball backup of the application data we need to reboot in the recovery to avoid any writes to the data filesystem.
55 14 Denis 'GNUtoo' Carikli
56 1 Denis 'GNUtoo' Carikli
h2. Backuping Silence's data from the old device
57
58 19 Denis 'GNUtoo' Carikli
To correctly backup the application data, you need to make sure that the application isn't writing to its data while you are doing the backup and that the data is consistent.
59
60
To make sure of that the easiest way to do that is to go in the recovery: this way the application will not be started automatically and the data is consistent. You will also need a root shell there, so make sure to setup that beforehand.
61
62
If everything is setup correctly, running @adb shell@ from your computer should result in a shell that now looks like this:
63 16 Denis 'GNUtoo' Carikli
<pre>
64 19 Denis 'GNUtoo' Carikli
root@m0:/ #
65 1 Denis 'GNUtoo' Carikli
</pre>
66
67 19 Denis 'GNUtoo' Carikli
Once that you have a shell in the recovery, you need to mount the data partition in /data from within that shell.
68
69
For the Galaxy SIII (GT-I9300), this can be done from your computer with this command:
70 16 Denis 'GNUtoo' Carikli
<pre>
71 19 Denis 'GNUtoo' Carikli
mount /dev/block/platform/*/by-name/USERDATA /data
72 1 Denis 'GNUtoo' Carikli
</pre>
73
74 19 Denis 'GNUtoo' Carikli
We will then create an archive of the silence data. 
75
76
This makes things much easier because with an archive:
77
* We can easily store and move that data.
78
* The archive preserves all its permissions, and it's best to keep them as applications might not work correctly otherwise. For instance in GNU/Linux, the 'sshd' program will refuse to work if some of its configuration files have the wrong permissions.
79
80
The silence data is in @/data/data/org.smssecure.smssecure/@. To make sure that the archive is created correctly and that once decompressed it will create the @org.smssecure.smssecure@ directory (with all the silence data in it) in the current directory, we need to go to @/data/data@ first.
81
82
To do that, we can go in /data/data with the following command:
83 1 Denis 'GNUtoo' Carikli
<pre>
84 19 Denis 'GNUtoo' Carikli
cd /data/data
85 17 Denis 'GNUtoo' Carikli
</pre>
86 16 Denis 'GNUtoo' Carikli
87 19 Denis 'GNUtoo' Carikli
We can then create the archive in the recovery with that command:
88 1 Denis 'GNUtoo' Carikli
<pre>
89
root@m0:/data/data # tar cvpf /org.smssecure.smssecure.tar org.smssecure.smssecure/
90
</pre>
91
92 19 Denis 'GNUtoo' Carikli
This will create the @/org.smssecure.smssecure.tar@ file which is our archive.
93
94
We will then exit the recovery shell as we will then need to type commands on your computer.
95
96
You can exit the recovery shell with the following command:
97 1 Denis 'GNUtoo' Carikli
<pre>
98
root@m0:/ # exit
99
</pre>
100
101 19 Denis 'GNUtoo' Carikli
We're now back on your computer.
102
103
As for now, we've created the archive but it's still in the recovery, so we need to copy it to your computer. 
104
105
This can be done with the following command:
106 1 Denis 'GNUtoo' Carikli
<pre>
107
$ adb pull /org.smssecure.smssecure.tar ./
108
</pre>
109
110 19 Denis 'GNUtoo' Carikli
It will create the @org.smssecure.smssecure.tar@ file in the current directory.
111
112
We will then need to verify that the archive was created correctly. 
113
More precisely we need to check that it will be extracted in the @org.smssecure.smssecure/@ directory, in the current directory, otherwise it could mess up the data partition when it's being uncompressed.
114
115
To do that we can list its content with the following command:
116 1 Denis 'GNUtoo' Carikli
<pre>
117 16 Denis 'GNUtoo' Carikli
$ tar tvf org.smssecure.smssecure.tar
118
</pre>
119 1 Denis 'GNUtoo' Carikli
120 19 Denis 'GNUtoo' Carikli
It should output something that will look more or less like that:
121 17 Denis 'GNUtoo' Carikli
<pre>
122 1 Denis 'GNUtoo' Carikli
drwxr-x--x u0_a61/u0_a61     0 2020-10-28 18:58 org.smssecure.smssecure/
123
lrwxrwxrwx root/root         0 2020-10-28 18:58 org.smssecure.smssecure/lib -> /data/app/org.smssecure.smssecure-1/lib/arm
124
[...]
125 17 Denis 'GNUtoo' Carikli
</pre>
126 19 Denis 'GNUtoo' Carikli
What is important to check is that the files and directories starts with @org/smssecure.smssecure/@ or @./org.smssecure.smssecure/@. If not something went wrong during the creation of the backup (for instance you might have forgetten to go in the @/data/data/@ recovery, or this tutorial could contain mistakes or be out of date).
127
128
When all that is done, you can power off the device by going going in @Advanced@ in the recovery menu and then selecting @>Power off@. If you want to reboot to Replicant instead you could select @Reboot system now@ in the recovery menu instead.
129 16 Denis 'GNUtoo' Carikli
130 14 Denis 'GNUtoo' Carikli
h2. Restoring Silence's data to the new device
131
132
Once the backup is done you need to mount the data partition. 
133 5 Denis 'GNUtoo' Carikli
134
For the Galaxy SIII (GT-I9300), this can be done from your computer with this command:
135 3 Denis 'GNUtoo' Carikli
<pre>
136
$ adb shell "mount /dev/block/platform/dw_mmc/by-name/USERDATA /data"
137
</pre>
138
139 5 Denis 'GNUtoo' Carikli
Then we need to get a root shell inside the recovery. This can be done with the @adb shell@ command:
140 3 Denis 'GNUtoo' Carikli
<pre>
141
$ adb shell
142
</pre>
143
144 5 Denis 'GNUtoo' Carikli
Then we assume that you are in /data/data to simplify this tutorial.
145
You will need to remember adjust all other commands if you are not in this directory.
146
To go in /data/data, you can use the following command:
147 3 Denis 'GNUtoo' Carikli
<pre>
148
root@m0:/ # cd /data/data/                                                     
149
root@m0:/data/data # 
150
</pre>
151
152 5 Denis 'GNUtoo' Carikli
As applications are sandboxed, and that as part of that sandboxing, they have their own usersname, we need to retrieve this username.
153
To do that we can just use @ls -ld@ on the directory holding the application internal data.
154
155
The directory has the internal name of the application.
156
157
Here are some well known name correspondances:
158
159 7 Denis 'GNUtoo' Carikli
| Internal name           | Application                                 |
160
| org.smssecure.smssecure | Silence                                     |
161
| com.android.dialer      | Dialer (Android's stock dialer application) |
162 8 Denis 'GNUtoo' Carikli
| fil.libre.repwifiapp    | RepWiFi                                     |
163 5 Denis 'GNUtoo' Carikli
164
For pakcages comming from f-droid, the f-droid website can find the correspondance.
165
166
For instance the "Silence page":https://f-droid.org/en/packages/org.smssecure.smssecure/ has @org.smssecure.smssecure@ in its URL and inside the page.
167
168
So with @ls -ld@ we can find the application username in this way:
169 1 Denis 'GNUtoo' Carikli
<pre>
170 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # ls -ld org.smssecure.smssecure
171 3 Denis 'GNUtoo' Carikli
__bionic_open_tzdata: couldn't find any tzdata when looking for localtime!
172
__bionic_open_tzdata: couldn't find any tzdata when looking for GMT!
173
__bionic_open_tzdata: couldn't find any tzdata when looking for posixrules!
174
drwxr-x--x 2 u0_a61 u0_a61 4096 2012-01-01 00:01 org.smssecure.smssecure
175
</pre>
176
177
Here the users and groups are @u0_a61@.
178 1 Denis 'GNUtoo' Carikli
179 5 Denis 'GNUtoo' Carikli
We will then need this information later on to restore the silence data from the other device: If we restore that data as-is it will most likely have wrong permissions: when the the silence application was installed on the older device, it was assigned an username. As this username depends on the number of applications that were installed before it, we cannot expect it to always be the same between the two devices.
180
181
It's also best to move or delete the data we don't want:
182 3 Denis 'GNUtoo' Carikli
<pre>
183
root@m0:/data/data # mv org.smssecure.smssecure org.smssecure.smssecure.delme
184
</pre>
185 1 Denis 'GNUtoo' Carikli
186 5 Denis 'GNUtoo' Carikli
Moving it has several advantages:
187
* We can still verify the username later on to see if it matches with the backup we restored.
188
* We can interrupt this tutorial more easily if something goes wrong.
189
190
Here we need to verify that the archive will extract its files in the @org.smssecure.smssecure@ directory and not in the current directory which is @/data/data@:
191 3 Denis 'GNUtoo' Carikli
<pre>
192
root@m0:/data/data # tar tf /org.smssecure.smssecure.tar
193
./org.smssecure.smssecure/
194
./org.smssecure.smssecure/lib -> /data/app/org.smssecure.smssecure-1/lib/arm
195
[...]
196 1 Denis 'GNUtoo' Carikli
</pre>
197 5 Denis 'GNUtoo' Carikli
Here we see that everything starts with @./org.smssecure.smssecure/@ (or @org.smssecure.smssecure/@) so it's good.
198 1 Denis 'GNUtoo' Carikli
199 5 Denis 'GNUtoo' Carikli
TODO: move this part earlier
200
201
If we had something like that instead:
202 1 Denis 'GNUtoo' Carikli
<pre>
203 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar tf /org.smssecure.smssecure.tar
204
./lib -> /data/app/org.smssecure.smssecure-1/lib/arm
205
[...]
206
</pre>
207
208
Then it's best to recreate the archive.
209
210
If you need more time you could also move back org.smssecure.smsecure.delme to org.smssecure.smssecure if needed.
211
212
We can then proceed to extract the application data (with the username from the old device):
213
<pre>
214 3 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar xpf /org.smssecure.smssecure.tar --numeric-owner
215
</pre>
216 1 Denis 'GNUtoo' Carikli
217 5 Denis 'GNUtoo' Carikli
Here we can see that the username differs from the one we need:
218 3 Denis 'GNUtoo' Carikli
<pre>
219
root@m0:/data/data # ls -ld org.smssecure.smssecure 
220
__bionic_open_tzdata: couldn't find any tzdata when looking for localtime!
221
__bionic_open_tzdata: couldn't find any tzdata when looking for GMT!
222
__bionic_open_tzdata: couldn't find any tzdata when looking for posixrules!
223
drwxr-x--x 9 u0_a63 u0_a63 4096 2012-01-01 00:21 org.smssecure.smssecure
224 1 Denis 'GNUtoo' Carikli
</pre>
225 5 Denis 'GNUtoo' Carikli
We have @u0_a63@ instead of @u0_a61@.
226 1 Denis 'GNUtoo' Carikli
227 5 Denis 'GNUtoo' Carikli
So we need to fix it. This can be done with the @chown@ command, like that:
228 3 Denis 'GNUtoo' Carikli
<pre>
229
root@m0:/data/data # chown u0_a61:u0_a61 -R org.smssecure.smssecure            
230
root@m0:/data/data # 
231
</pre>
232 1 Denis 'GNUtoo' Carikli
233 5 Denis 'GNUtoo' Carikli
At this point, we don't need the @org.smssecure.smssecure.delme@ directory anymore, and it's best to remove it not to create any issues later on.
234
This can be done with the following command:
235 3 Denis 'GNUtoo' Carikli
<pre>
236
root@m0:/data/data # rm -rf org.smssecure.smssecure.delme
237
root@m0:/data/data # 
238
</pre>
239 1 Denis 'GNUtoo' Carikli
240 5 Denis 'GNUtoo' Carikli
In addition to the standard unix permissions, Android also uses selinux, so we also need to fixup the selinux permissions.
241
242
The restorecon command can be used for that. Here's its help: 
243 3 Denis 'GNUtoo' Carikli
<pre>
244
root@m0:/data/data # restorecon                                                
245
usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...
246
247
Restores the default security contexts for the given files.
248
249
-D	apply to /data/data too
250
-F	force reset
251
-R	recurse into directories
252
-n	don't make any changes; useful with -v to see what would change
253
-v	verbose: show any changes
254
255
restorecon: Needs 1 argument
256 1 Denis 'GNUtoo' Carikli
</pre>
257 5 Denis 'GNUtoo' Carikli
258 1 Denis 'GNUtoo' Carikli
So to use it to fixup the selinux permissions, we can use the following command:
259 5 Denis 'GNUtoo' Carikli
<pre>
260
root@m0:/data/data # restorecon -D -F -R -v /data/
261
</pre>
262 6 Denis 'GNUtoo' Carikli
263
The order of the arguments (-D, -F, etc) seem to be important here as the wrong order might result in nothing being done.
264
Without the @-v@ argument and with the wrong order of argument, it might make you think that it did its job while it did nothing.
265 5 Denis 'GNUtoo' Carikli
266
It will then print something that looks like that:
267 3 Denis 'GNUtoo' Carikli
<pre>
268
SELinux: Loaded file_contexts contexts from /file_contexts.
269
[...]
270
SELinux:  Relabeling /data/data/org.smssecure.smssecure from u:object_r:system_data_file:s0 to u:object_r:app_data_file:s0:c512,c768.
271
SELinux:  Relabeling /data/data/org.smssecure.smssecure/lib from u:object_r:system_data_file:s0 to u:object_r:app_data_file:s0:c512,c768.
272 1 Denis 'GNUtoo' Carikli
[...]
273
</pre>
274 5 Denis 'GNUtoo' Carikli
275
The premissions fixing is now done.
276
277
So we can then umount the data partition and reboot.
278 1 Denis 'GNUtoo' Carikli
279 5 Denis 'GNUtoo' Carikli
To do that first we need to go outside of data, else the mount will fail:
280 1 Denis 'GNUtoo' Carikli
<pre>
281 16 Denis 'GNUtoo' Carikli
root@m0:/data/data # cd /For instance you could want to move from a Galaxy SIII (GT-I9300) to a Galaxy SII (GT-I9100) or vice versa, and you might not want to recreate keys, sessions, etc when moving device.
282 5 Denis 'GNUtoo' Carikli
root@m0:/ # 
283
</pre>
284
285
Then we can simply unmount it with this command:
286
<pre>
287 3 Denis 'GNUtoo' Carikli
root@m0:/ # umount  /data/                                                     
288 1 Denis 'GNUtoo' Carikli
root@m0:/ # 
289 3 Denis 'GNUtoo' Carikli
</pre>
290 5 Denis 'GNUtoo' Carikli
291
Then it's a good practice to make sure that everything is written to the data partition before rebooting.
292 1 Denis 'GNUtoo' Carikli
We can do that with the @sync@ command:
293
<pre>
294 5 Denis 'GNUtoo' Carikli
root@m0:/ # sync
295
</pre>
296
297
And we can finally reboot:
298 1 Denis 'GNUtoo' Carikli
<pre>
299
root@m0:/ # reboot
300
</pre>
301 5 Denis 'GNUtoo' Carikli
302
After rebooting, silence still refused to start.
303
304 1 Denis 'GNUtoo' Carikli
So I looked at the logs from my laptop with this command:
305 5 Denis 'GNUtoo' Carikli
<pre>
306
$ adb logcat -b main
307
</pre>
308
309
I waited until no more new logs were printed.
310
311 1 Denis 'GNUtoo' Carikli
I then press enter multiple times to create a separation with many new lines to be able to get back to the begining of the new logs easily.
312 5 Denis 'GNUtoo' Carikli
Then I launched silence and started pressing enter multiple times again to mark the end of the silence related logs.
313
314
I then had that in these new logs:
315 16 Denis 'GNUtoo' Carikli
<pre>For instance you could want to move from a Galaxy SIII (GT-I9300) to a Galaxy SII (GT-I9100) or vice versa, and you might not want to recreate keys, sessions, etc when moving device.
316 4 Denis 'GNUtoo' Carikli
01-01 01:27:48.260  4126  4126 D AndroidRuntime: Shutting down VM
317
01-01 01:27:48.265  4126  4126 E AndroidRuntime: FATAL EXCEPTION: main
318
01-01 01:27:48.265  4126  4126 E AndroidRuntime: Process: org.smssecure.smssecure, PID: 4126
319
01-01 01:27:48.265  4126  4126 E AndroidRuntime: Theme: themes:{}
320
01-01 01:27:48.265  4126  4126 E AndroidRuntime: java.lang.RuntimeException: Unable to create application org.smssecure.smssecure.ApplicationContext: java.lang.SecurityException: getActiveSubscriptionInfoList: Neither user 10061 nor current process has android.permission.READ_PHONE_STATE.
321 1 Denis 'GNUtoo' Carikli
01-01 01:27:48.265  4126  4126 E AndroidRuntime: 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4754)
322
[...]
323
</pre>
324
325 5 Denis 'GNUtoo' Carikli
So to fix it I went in @Settings->Apps->Silence->Permissions@ and gave it all the permissions it needed.
326
327
I had this issue because I didn't even launch silence after installing it, so it cound't ask me for the permissions it needed.
328
And the silence of the former device probably wrote in its data that it already asked the permissions.