forked from IzzySoft/Adebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adebar-cli
executable file
·400 lines (365 loc) · 12.4 KB
/
adebar-cli
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
# Adebar
# (Android DEvice Backup And Restore)
# Creating scripts to backup and restore your apps, settings, and more
# © 2014 by Andreas Itzchak Rehberg
# Licensed using GPLv2 (see the file LICENSE which should have shipped with this)
# #################################[ Configuration ]###
# ------------------------=[ directories ]=--
#OUTDIR='.' # OUTDIR is specified via command line (mandatory parameter)
STORAGE_BASE=
USERDIR="userApps"
SYSDIR="sysApps"
PARTBACKUPDIR="images"
DOCDIR="docs"
CONFDIR="conf"
CACHEDIR=""
TRANSFER_DIR=""
# -------------------=[ device specifics ]=--
SERIAL=""
DEVICE_NAME="MyDroid"
BACKUP_PASSWORD=""
# ---------------------=[ TiBu specifics ]=--
DEVICE_IP=
TIBU_PORT="8080"
TIBU_SDINT="/storage/INTERNAL/Storage-ALL.zip"
TIBU_SDEXT="/storage/SAMSUNG_EXT_SD_CARD/Storage-ALL.zip"
TIBU_BACKUPS="/TitaniumBackup-ALL.zip"
# ---------------------------=[ Features ]=--
MK_APPDISABLE=1
MK_USERBACKUP=1
MK_SYSBACKUP=1
MK_APPRESTORE_DELAY=p
MK_XPRIVACY_EXPORT=0
MK_XPRIVACY_PULL=0
PULL_SETTINGS=1
MK_TIBU=0
MK_USERAPPS=1
MK_SYSAPPS=1
MK_SYSAPPS_RETRIEVE_NAMES=0
MK_INSTALLLOC=1
MK_DEVICEINFO=1
MK_DEVICEINFO_SENSORS=1
MK_DEVICEINFO_PMLISTFEATURES=1
MK_DEVICEINFO_STATUS=1
MK_PARTINFO=1
MK_PARTBACKUP=0
PARTITION_SRC="auto"
# -------------------=[ UserApp Specials ]=--
declare -A APP_INSTALL_SRC
declare -A APP_MARKET_URL
APP_INSTALL_SRC[org.fdroid.fdroid]="F-Droid"
APP_INSTALL_SRC[cm.aptoide.pt]="Aptoide"
APP_INSTALL_SRC[com.android.vending]="Google Play"
APP_INSTALL_SRC[com.google.android.feedback]="Google Play (Feedback)"
APP_INSTALL_SRC[de.robv.android.xposed.installer]="Xposed"
APP_MARKET_URL[org.fdroid.fdroid]="https://f-droid.org/repository/browse/?fdid=%s"
APP_MARKET_URL[cm.aptoide.pt]=""
APP_MARKET_URL[com.android.vending]="https://play.google.com/store/apps/details?id=%s"
APP_MARKET_URL[com.google.android.feedback]="https://play.google.com/store/apps/details?id=%s"
APP_MARKET_URL[de.robv.android.xposed.installer]="http://repo.xposed.info/module/%s"
# Misc
PROGRESS=1
USE_ANSI=1
TIMESTAMPED_SUBDIRS=0
LINK_LATEST_SUBDIR=0
KEEP_SUBDIR_GENERATIONS=0
POSTRUN_CMD=""
APPNAME_CMD=""
ROOT_COMPAT=0
AUTO_CONFIRM=0
AUTO_UNLOCK=0
# Internal use / debugging
_OOPS_LEVEL_ADJUST=0 # 0=no_adjust; increase to "hide" oopses, decrease to force them to be revealed even on lower levels
_OOPS_REPEAT=0 # whether to show the same "oops'd line" multiple times
declare -A OOPSES # array to store which lines where already reported
############################################[ Init ]###
BINDIR="${0%/*}"
# check parameters
if [[ -z "$1" || "$1" = "-h" || "$1" = "--help" ]]; then
echo
echo "Syntax: $0 <config|target_directory> [suffix]"
echo
if [[ ! -d "${BINDIR}/config" || "$1" = "-h" || "$1" = "--help" ]]; then
echo "There are no more command-line parameters currently, everything is"
echo "controlled via config files. For details, please see the project's"
echo "wiki at https://github.com/IzzySoft/Adebar/wiki/Configuration"
echo
fi
if [[ -d "${BINDIR}/config" ]]; then
echo "Available config files:"
for con in "${BINDIR}"/config/*; do
[[ -f "${con}" ]] && echo "- $(basename ${con})"
done
fi
echo
exit 1
else
OUTDIR="$1"
fi
# Checking for config file and sourcing it, if exists
if [ -d "${BINDIR}/config" ]; then
if [ -f "${BINDIR}/config/$OUTDIR" ]; then # device-specific config
. "${BINDIR}/config/$OUTDIR"
elif [ -f "${BINDIR}/config/default" ]; then # default config
. "${BINDIR}/config/default"
fi
elif [ -f "${BINDIR}/config" ]; then # default config as of v3
. "${BINDIR}/config"
fi
# check whether output directory shall have a suffix
if [ -n "$2" ]; then
OUTDIR="${OUTDIR}${2}"
TIMESTAMPED_SUBDIRS=0
elif [[ $TIMESTAMPED_SUBDIRS -gt 0 ]]; then
OUTDIR="${OUTDIR}/$(date +"%Y%m%d%H%M")"
fi
# Check whether a device is connected at all and, if configured, the serial matches
# No device connected:
ADBOPTS=""
if [ -z "$(adb devices|egrep "^[0-9A-Za-z]+\s+device$"|awk '{print $1}')" ]; then
echo "No device found. Make sure you have connected your device with"
echo "USB debugging enabled, and try again."
echo
exit 2
fi
serials=($(adb devices|egrep "^[0-9A-Za-z]+\s+device$"|awk '{print $1}'))
# Multiple devices connected but no serial defined:
if [ -z "${SERIAL}" -a ${#serials[*]} -ne 1 ]; then
echo "There are currently multiple devices connected, and we don't know"
echo "which one to connect to. Please either disconnect all but the device"
echo "you wish to retrieve data from, or specify its serial in your"
echo "Configuration. Then try again."
echo
exit 3
fi
# SERIAL specified:
if [ -n "${SERIAL}" ]; then
if [ ${#serials[*]} -eq 1 -a "${serials[0]}" != "${SERIAL}" ]; then
echo "Your configuration specifies a serial of '${SERIAL}',"
echo "but the connected device presents '${serials[0]}'."
echo "Please check if you have the correct device connected, or might have"
echo "specified the wrong parameter to the script."
echo ""
exit 4
fi
if [ ${#serials[*]} -gt 1 ]; then
typeset -i ser=0
for d in ${serials[*]}; do
[ "$d" = "${SERIAL}" ] && {
ser=1
break
}
done
if [ $ser -eq 0 ]; then
echo "Your configuration specifies a device serial of '${SERIAL}'."
echo "Though multiple devices seem to be connected, that is not one"
echo "of them. Please check and try again."
echo ""
exit 4
fi
fi
ADBOPTS="-s ${SERIAL}"
fi
# Check output directory and create it if it does not exist
if [ -n "${STORAGE_BASE}" ]; then
OUTDIR="${STORAGE_BASE}/${OUTDIR}"
fi
DOCDIR="${OUTDIR}/${DOCDIR}"
CONFDIR="${OUTDIR}/${CONFDIR}"
PKGXML="${CONFDIR}/packages.xml"
BUILDPROP="${CONFDIR}/build.prop"
if [ ! -d "${DOCDIR}" ]; then
mkdir -p "${DOCDIR}" || {
echo "Output directory does not exist, and I cannot create it. Sorry."
echo
exit 5
}
mkdir -p "${CONFDIR}"
fi
# Load libraries if needed
[[ $((${MK_PARTINFO} + ${MK_PARTBACKUP})) -gt 0 ]] && . "${BINDIR}/lib/partitions.lib"
[[ $(($PULL_SETTINGS + $MK_XPRIVACY_EXPORT + $MK_XPRIVACY_PULL)) -gt 0 ]] && . "${BINDIR}/lib/pull_config.lib"
[[ $MK_TIBU -eq 1 ]] && . "${BINDIR}/lib/tibu.lib"
[[ $((${MK_APPDISABLE} + ${MK_USERBACKUP} + ${MK_SYSBACKUP} + ${MK_INSTALLLOC})) -gt 0 ]] && . "${BINDIR}/lib/scriptgen.lib"
[[ ${MK_DEVICEINFO} -gt 0 ]] && . "${BINDIR}/lib/deviceinfo.lib"
[[ $((${MK_USERAPPS} + ${MK_SYSAPPS})) -ne 0 ]] && . "${BINDIR}/lib/packagedata.lib"
[[ -n "${TRANSFER_DIR}" ]] && . "${BINDIR}/lib/transfer.lib"
declare -a userApps # list of package names
declare -a sysApps
declare -a dApps
#########################################[ Helpers ]###
# Log progress (if enabled)
# Call: doProgress "what to show" [level]"
# level defaults to 1
doProgress() {
[[ $PROGRESS -eq 0 ]] && return
if [[ -z "$2" ]]; then
local level=1
else
local level=$2
fi
if [[ $PROGRESS -ge $level ]]; then
echo -e "$1"
fi
}
# Report "oopses"
# params: in_function, in_line, msg [, log_level]
# example call: oops "pkg" "${LINENO}" "Unexpected: '$zeile'" 3
# level will be "adjusted" according to _OOPS_LEVEL_ADJUST (for debug purposes or to "hide" oopses)
# if not specified, level defaults to 2 (as "oopses" are usually informal only)
oops() {
local level=$4
[[ -z "$level" ]] && level=2
[ -n "${OOPSES[$2]}" -a ${_OOPS_REPEAT} -eq 0 ] && return
OOPSES[$2]=1
local txt="Oops in $1 at line $2: $3"
if [[ $level -lt 2 ]]; then
txt=$(ansi_code "${txt}" "red")
else
txt=$(ansi_code "${txt}" "yellow")
fi
level=$(($_OOPS_LEVEL_ADJUST + $level))
doProgress "${txt}" $level
}
# Helper: Check whether $needle is contained in $haystack[]
# Call it like: in_array $tmpstring "${P_SKIP[@]}"
in_array() {
local hay needle=$1
shift
for hay; do
[[ $hay == $needle ]] && return 0
done
return 1
}
#
# Wrap a string in ANSI codes
#
ansi_code() {
if [[ USE_ANSI -eq 0 ]]; then
echo "$1"
return
fi
local txt="$1"
shift
while [[ -n "$1" ]]; do
case "$1" in
"bold") txt="\033[1;37m${txt}" ;; # BOLD: \033[1;37m // NORMAL: \033[0;37m // # RED: \033[1;31m
"red") txt="\033[1;31m${txt}" ;; # more: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
"yellow") txt="\033[1;33m${txt}" ;;
"green") txt="\033[1;32m${txt}" ;;
esac
shift
done
echo "${txt}\033[0m"
}
#
# Gather lists of installed apps
#
initAppLists() {
[[ $(($MK_USERBACKUP + $MK_SYSBACKUP)) -eq 0 ]] && return
doProgress "Gathering lists of installed apps"
doProgress "- userApps" 2
for app in $(adb ${ADBOPTS} shell pm list packages -3 2>/dev/null); do
if [[ "${app}" =~ ^package: ]]; then
app=${app//[$'\t\r\n']} # remove trailing CR (^M)
userApps+=(${app##*:})
fi
done
doProgress "- systemApps" 2
for app in $(adb ${ADBOPTS} shell pm list packages -s 2>/dev/null); do
if [[ "${app}" =~ ^package: ]]; then
app=${app//[$'\t\r\n']}
sysApps+=(${app##*:})
fi
done
doProgress "- checking for uninstalled apps remembered by the system" 2
for app in $(adb ${ADBOPTS} shell pm list packages -u 2>/dev/null); do
if [[ "${app}" =~ ^package: ]]; then
app=${app//[$'\t\r\n']}
in_array "${app##*:}" ${sysApps[@]} && continue
in_array "${app##*:}" ${userApps[@]} && continue
dApps+=($app)
doProgress " + ${app##*:}" 4
fi
done
}
#
# Post processing
#
postProcess() {
doProgress "PostProcessing and Cleanup"
if [[ $TIMESTAMPED_SUBDIRS -gt 0 ]]; then
if [[ $LINK_LATEST_SUBDIR -gt 0 ]]; then
doProgress "- symlink latest generation" 2
local LINK_NAME="$(dirname "${OUTDIR}")/latest"
if [ -L "${LINK_NAME}" -o ! -e "${LINK_NAME}" ]; then
rm -f "${LINK_NAME}" > /dev/null 2>&1
ln -sf "$(basename "${OUTDIR}")" "${LINK_NAME}"
else
doProgress "$(ansi_code "! Cannot symlink latest generation subdir: some file/directory already uses its name" "red")"
fi
if [[ ${KEEP_SUBDIR_GENERATIONS} -gt 0 ]]; then
doProgress "- remove outaged generations" 2
declare -a GENS
cd "$(dirname ${OUTDIR})"
for d in $(ls -dpX [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]); do
[[ ${#d} -ne 13 ]] && continue
GENS+=($d)
done
declare -i counter=0
if [[ ${#GENS[@]} -gt ${KEEP_SUBDIR_GENERATIONS} ]]; then
local HAVE_BACKUPS
local files
while [[ ${#GENS[@]} -gt ${KEEP_SUBDIR_GENERATIONS} ]]; do
# We do not want to delete real backups, so make sure there are none
HAVE_BACKUPS=0
files=(${GENS[${counter}]:0:12}/${USERDIR}/*) # UserApps
[[ -e "${files[0]}" ]] && HAVE_BACKUPS=1
files=(${GENS[${counter}]:0:12}/${SYSDIR}/*) # SysApps
[[ -e "${files[0]}" ]] && HAVE_BACKUPS=1
files=(${GENS[${counter}]:0:12}/*.ab) # Any ADB backups
[[ -e "${files[0]}" ]] && HAVE_BACKUPS=1
files=(${GENS[${counter}]:0:12}/*.gz) # Any .gz archives, e.g. converted by ab2tar
[[ -e "${files[0]}" ]] && HAVE_BACKUPS=1
if [[ $HAVE_BACKUPS -eq 0 ]]; then
doProgress " + removing '${GENS[${counter}]:0:12}'" 3
rm -rf "${GENS[${counter}]:0:12}"
else
warning=" + '${GENS[${counter}]:0:12}' seems to contain backups. Renaming it to '${GENS[${counter}]:0:12}.Backup'"
echo -e "$(ansi_code "$warning" "red")"
mv "${GENS[${counter}]:0:12}" "${GENS[${counter}]:0:12}.Backup"
fi
unset GENS[${counter}]
counter+=1
done
fi
cd - >/dev/null
fi
fi
fi
if [[ -n "${POSTRUN_CMD}" ]]; then
doProgress "- executing post-run command" 2
$(${POSTRUN_CMD})
fi
}
############################################[ Main ]###
echo
doProgress "$(ansi_code "Adebar running:" "bold")"
initAppLists
[[ ${MK_APPDISABLE} -gt 0 ]] && getDisabled
[[ ${MK_USERBACKUP} -gt 0 ]] && getUserAppBackup
[[ ${MK_SYSBACKUP} -gt 0 ]] && getSystemAppBackup
[[ ${MK_INSTALLLOC} -gt 0 ]] && getInstallLoc
[[ $((${MK_PARTINFO} + ${MK_PARTBACKUP})) -gt 0 ]] && getPartInfo
[[ ${MK_PARTBACKUP} -gt 0 ]] && writePartDumpScript
[[ ${MK_DEVICEINFO} -gt 0 ]] && getDeviceInfo
[[ $PULL_SETTINGS -eq 1 ]] && getSettings
[[ $(($MK_XPRIVACY_EXPORT + $MK_XPRIVACY_PULL)) -gt 0 ]] && getXPrivacy
[[ $MK_TIBU -eq 1 ]] && getTibu
[[ -n "${TRANSFER_DIR}" ]] && doTransfer
[[ $((${MK_USERAPPS} + ${MK_SYSAPPS})) -ne 0 ]] && getAppDetails
postProcess
doProgress "$(ansi_code "Adebar done." "bold")"
echo
exit 0