forked from cluk1/android-multiuser-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·129 lines (114 loc) · 3.66 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
. $(dirname $0)/config
function usage {
echo "usage: $0 DEVICE USER_ID|all PACKAGES...|all"
echo "exam1: $0 XXXXXX all all"
echo "exam2: $0 XXXXXX 0 com.google.android.youtube"
}
if [ ! "$1" ]; then
usage
$A0 devices
exit 1
fi
DEVICE=$1
shift
AUID=$1
shift
PKG=$1
A="$A0 -s $DEVICE"
AS="$A shell"
if [ -z "$AUID" -o -z "$PKG" ]; then
usage
exit 0
fi
mkdir -p $LBASE/{apks,data}
PKGS="$*"
AUIDS="$AUID"
# Backup all installed packages?
if [ "$PKG" = "all" ]; then
PKGS="$(cat $LBASE/pall.txt)"
fi
if [ "$AUID" = "all" ]; then
#PRE-REQUISITE BEFORE ENTER RECOVERY:
# adb shell "pm list users | grep 'UserInfo' | cut -f1 -d':' | cut -f2 -d'{' > $LBASE/alluser.txt"
AUIDS="$(cat $LBASE/alluser.txt)"
fi
if [ -z "$($AS mount | grep 'on /data')" ]; then
$AS mount /dev/block/by-name/userdata /data
fi
for AUID in $AUIDS; do
echo "Creating backup for AUID $AUID.."
UPKGS="$(cat $LBASE/puser_${AUID}.txt)"
mkdir -p $LBASE/data/$AUID
for PKG in $PKGS; do
if [ "$($AS [ -d /data/app/$PKG-* ] && echo 1)" ]; then
APKNUM=$($AS ls /data/app/$PKG-*/*apk | wc -l)
DIRNAME=$(dirname $($AS ls /data/app/$PKG-*/*apk | head -n1))
elif [ "$($AS [ -d /data/app/*/$PKG-* ] && echo 1)" ]; then #APP IN SUBDIR
APKNUM=$($AS ls /data/app/*/$PKG-*/*apk | wc -l)
DIRNAME=$(dirname $($AS ls /data/app/*/$PKG-*/*apk | head -n1))
else
APKNUM=0
fi
if [ $APKNUM -gt 0 ]; then
echo " Backing up package $PKG.."
#MULTIPLE APKS
if [ -d "$LBASE/apks/$PKG" ] || [ -f "$LBASE/apks/$PKG.apk" ]; then
echo " $PKG already backed up."
else #BACKUP APKS
printf " pulling apk.."
if [ $APKNUM -gt 1 ]; then #MULTIPLE APKS
mkdir -p "$LBASE/apks/$PKG"
for APKFILE in $( $AS ls $DIRNAME/*apk | tr -d '\r' ); do
$A pull $APKFILE $LBASE/apks/$PKG/
done
else #SINGLE APK
for APKFILE in "$( $AS ls $DIRNAME/base.apk | tr -d '\r' )"; do
$A pull $APKFILE $LBASE/apks/$PKG.apk
done
fi
echo " done."
fi
if [ $AUID -eq 0 ]; then
USERDATA=/data/data
else
USERDATA=/data/user/$AUID
fi
if [[ "$UPKGS" == *"$PKG"* ]]; then
if [ "$($AS [ -d $USERDATA/$PKG ] && echo 1)" ]; then
printf " backing up userdata.."
$AS "tar c -C $USERDATA -f /sdcard/$PKG-user.tar $PKG"
$A pull /sdcard/$PKG-user.tar $LBASE/data/$AUID/
$AS rm /sdcard/$PKG-user.tar
echo " done."
fi
if [ "$($AS [ -d /data/media/$AUID/Android/data/$PKG ] && echo 1)" ]; then
printf " backing up media.."
$AS "tar c -C /data/media/$AUID/Android/data -f /sdcard/$PKG-media.tar $PKG"
$A pull /sdcard/$PKG-media.tar $LBASE/data/$AUID/
$AS rm /sdcard/$PKG-media.tar
echo " done."
fi
else
echo " package $PKG is not enabled for user $AUID"
touch $LBASE/data/$AUID/$PKG.disabled
fi
echo " done."
fi
done
echo "done."
if [ "$($AS [ -f /data/system/users/$AUID/accounts.db ] && echo 1)" ] ; then
printf "Creating backup of accounts.db .."
$A pull /data/system/users/$AUID/accounts.db $LBASE/data/$AUID/
#RESTORE: $A push $LBASE/data/$AUID/accounts.db /data/system/users/$AUID/
echo "done."
fi
done
if [ "$($AS [ -f /data/system/sync/accounts.xml ] && echo 1)" ]; then
printf "Creating backup of accounts.xml .."
$A pull /data/system/sync/accounts.xml $LBASE/data/
#RESTORE: $A push $LBASE/data/accounts.xml /data/system/sync/
echo "done."
fi
find $LBASE
echo "Backup complete. Now, please reboot to normal (adb -s $DEVICE reboot)."