-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb-sdbak-zsh
executable file
·208 lines (179 loc) · 4.76 KB
/
adb-sdbak-zsh
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
#!/bin/zsh
zmodload zsh/mapfile;
source lib-colors;
source lib-exec-opts;
source lib-input;
source lib-fmt;
declare -r SCRIPT_PATH="$0";
declare -r SCRIPT_NAME="${SCRIPT_PATH##*/}";
declare -r SCRIPT_DIR="$(dirname "$0")";
declare -r TMP_FILE="${SCRIPT_DIR}/.${SCRIPT_NAME}.tmp";
# Set bool options.
declare -i DoForce;
# Set string options.
declare -r SRC="/sdcard";
declare -r DST="${HOME}/tmp/$(basename ${SRC})_$(date +%Y-%m-%d)";
declare -a DIRS;
declare -a INCOMPLETE;
_init() {
_getopts "$@";
_connect || exit $?;
_getDirs || exit $?;
}
_main() {
_q pushd "${DST}";
_backup;
_q popd;
_cleanUp;
}
# Print help and exit.
_help() {
local path="$0";
local script=${path##*/};
local fmt="$(date +%Y-%m-%d)";
local bin="${HOME}/bin"
cat << EOH >&2
${script}
DESCRIPTION:
Pull an Android devices ${SRC} contents to ${DST}.
USAGE:
${SCRIPT_NAME} [OPTIONS] [<remote-file-to-pull>] [<local-directory-to-save>]
OPTIONS:
r, resume, c, continue [default]
Resume a backup operation, if one did not complete earlier.
Useful when you need to cancel this tool and resume it later
or when it previously was cancelled by accident.
a, all, f, force, full
Force redo a a full backup, even if a previous one did not complete.
CAUTION: Any existing files will be overwritten.
DEFAULT:
${SCRIPT_NAME} -r ${SRC} ${DST}
NOTE:
Requires that the shell script "adb-pull" is in the '\$PATH'.
(E.g., place "${SCRIPT_NAME}" in "${HOME}/bin/" and
add 'export PATH=\$PATH:\$HOME/bin' to '~/.zshrc'.)
EOH
[ -n "${1}" ] &&
exit "${1}";
exit 1;
}
# Analyze user arguments.
_getopts() {
# Declare arrays for commandline bool switches and string options.
local -a boolOpts;
local -a stringOpts;
# Iterate over user arguments.
local -i i;
local flag;
for ((i=1;i<=$#;i++)); do
eval flag="\$$i";
case "${flag}" in
*(-)[hH]*([eE][lL][pP]) )
_help;
;;
*(-)[rR]*([eE][sS][uU][mM][eE]) )
boolOpts[0]=1;
;;
*(-)[cC]*([oO][nN][tT][iI][nN][uU][eE]) )
boolOpts[0]=1;
;;
*(-)[aA]*([lL]) )
boolOpts[1]=1;
;;
*(-)[fF]*([uU]+([lL])) )
boolOpts[1]=1;
;;
*(-)[fF]*([oO][rR][cC][eE]) )
boolOpts[1]=1;
;;
* )
stringOpts+=( "${flag}" );
;;
esac;
done;
# Set bool options.
DoForce="${boolOpts[1]}";
if [ "${DoForce}" = 1 ]; then
gvfs-trash "${TMP_FILE}";
fi;
# Set string options.
if [ -n "${stringOpts[0]}" ]; then
SRC="$(_fmtDir "${stringOpts[0]}")";
fi;
if [ -n "${stringOpts[1]}" ]; then
DST="$(_fmtDir "${stringOpts[1]}")";
fi;
mkdir -p "${DST}";
}
# Wait for Android device to connect.
_connect() {
local connected;
connected="$(adb devices | head -2 | tail -1)";
if [ "${connected}" ]; then
return 0;
fi;
echo "Waiting for Android device to connect...";
adb wait-for-device;
}
# Store the target directories into an array
# and save them in a temporary file.
_getDirs() {
if [ ! -e "${TMP_FILE}" ]; then
echo " " adb shell "ls --color=never -1A ${SRC}/" '>' "${TMP_FILE}";
adb shell "ls --color=never -1A ${SRC}/" > "${TMP_FILE}";
fi;
local -a tmpFileLines=( "${(f)mapfile[$TMP_FILE]}" );
local line;
for line in $tmpFileLines; do
DIRS+=( "${line:0:-1}" );
done;
# gvfs-trash "${TMP_FILE}";
# exit;
if [ -z "${DIRS[*]}" ]; then
_err "Failed to get directories under ${SRC}/.";
fi;
}
# Pull each file from the device.
_backup() {
local d;
local -i isFirst=0;
for d in "${DIRS[@]}"; do
[ -z "${d}" ] &&
continue;
if [ "${isFirst}" = 0 ]; then
isFirst=1;
else
echo;
fi;
_c green;
echo -n "Pull ${d}?";
_u;
(
_Yn 3 &&
adb-pull "${SRC}/${d}";
) || (
echo "${d}" | tee -a "${TMP_FILE}";
INCOMPLETE+=( "${d}" );
)
sed -i -e '1d' "${TMP_FILE}";
done;
}
# Manage temporary files and report any final errors.
_cleanUp() {
[ -z "$(cat "${TMP_FILE}")" ] &&
gvfs-trash "${TMP_FILE}";
[ -z "${INCOMPLETE[*]}" ] &&
return 0;
echo;
echo "Did not pull ${INCOMPLETE[*]}";
_c green "echo -n ==\> Pull them now?";
if _yN 10; then
"$0";
exit $?;
fi;
_c green "echo -n ==\> Remember to pull these files the next time this script is run?";
_Yn 10 ||
gvfs-trash "${TMP_FILE}";
}
_init "$@";
_main;