-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup-apps.sh
executable file
·140 lines (107 loc) · 2.33 KB
/
backup-apps.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
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
usage() {
echo "$0 [-h | --help] [-l | --list-only] <target directory>" 1>&2
echo " target directory Target directory where apk files are backed up to. Defaults to the current directory." 1>&2
echo " -h Shows this help message" 1>&2
echo " --help" 1>&2
echo " -f Only outputs the list of file names that would be pulled with adb"
echo " --file-list-only"
echo " -l Only outputs the list of package names that would be backed up"
echo " --list-only"
}
main() {
local targetdirectory="$1"
local line
if ! [ -d "$1" ]
then
echo "target directory does not exist: $targetdirectory" 1>&2
usage
return 1
fi
list-thirdparty-packages | while read line
do
local package="$( parse-package-name "$line" )"
local apkfile="$( parse-apk-filename "$line" )"
backup-apk-of-package "$apkfile" "$package" "$1"
done
}
main-list-only() {
local line
list-thirdparty-packages | while read line
do
local package="$( parse-package-name "$line" )"
echo "$package"
done
}
main-filelist-only() {
local line
list-thirdparty-packages | while read line
do
local apkfile="$( parse-apk-filename "$line" )"
echo "$apkfile"
done
}
backup-apk-of-package() {
local apkfile="$1"
local package="$2"
local targetdirectory="$3"
local targetfile="${package}.apk"
if [ ${#targetdirectory} -gt 0 ]
then
targetfile="${targetdirectory}/$targetfile"
fi
adb pull "$apkfile" "$targetfile"
}
list-thirdparty-packages() {
adb shell -n cmd package list packages -f -3
}
parse-apk-filename() {
local line="$1"
line="${line#package:}"
line="${line/.apk=*/}.apk"
echo -n "$line"
}
parse-package-name() {
local line="$1"
line="${line#package:}"
line="${line/*=/}"
echo -n "$line"
}
args=( "$0" "$@" )
flag_listonly=0
flag_filelistonly=0
targetdirectory=""
while [ $OPTIND -le $# ]
do
case "${args[$OPTIND]}" in
-h | --help)
usage
exit 1
;;
-f | --file-list-only)
flag_filelistonly=1
;;
-l | --list-only)
flag_listonly=1
;;
*)
targetdirectory="${args[$OPTIND]}"
;;
esac
OPTIND=$(( $OPTIND + 1))
done
if [ ${flag_filelistonly} -eq 1 ] && [ ${flag_listonly} -eq 1 ]
then
echo "-f and -l parameters may not be combined" >&2
usage
exit 1
fi
if [ ${flag_listonly} -eq 1 ]
then
main-list-only
elif [ ${flag_filelistonly} -eq 1 ]
then
main-filelist-only
else
main "${targetdirectory}"
fi