forked from AndDiSa/android_backup_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
executable file
·220 lines (190 loc) · 4.45 KB
/
functions.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
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
#!/bin/bash
A="adb"
AMAGISK="adb shell su root " # -- needed for magisk rooted devices
AMAGISK2="adb shell su 0 -c " # -- needed for magisk rooted devices (depends on su version installed)
AMAGISK3="adb shell su -c " # -- needed for magisk rooted devices (depends on su version installed)
AROOT="adb shell "
function cleanup()
{
$AS "rm /dev/busybox"
}
function checkForCleanData()
{
if ! [ "$($AS ls /data/ | wc -l)" -gt 1 ]; then
$AS mount /data
fi
if ! [ "$($AS ls /data/ | wc -l)" -gt 4 ]; then
echo "It seems like /data is not in a sane state!"
$AS ls /data || :
$AS stat /data || :
exit 1
fi
}
function checkPrerequisites()
{
adb=`adb --version`
if [ $? -ne 0 ]; then
echo "adb not found, please install adb"
exit 1
fi
git=`git --version`
if [ $? -ne 0 ]; then
echo "git not found, please install git"
exit 1
fi
tar=`tar --help`
if [ $? -ne 0 ]; then
echo "tar not found, please install tar"
exit 1
fi
wc=`wc --help`
if [ $? -ne 0 ]; then
echo "wc not found, please install wc"
exit 1
fi
tr=`tr --help`
if [ $? -ne 0 ]; then
echo "tr not found, please install tr"
exit 1
fi
sed=`sed --help`
if [ $? -ne 0 ]; then
echo "sed not found, please install sed"
exit 1
fi
rev=`rev --help`
if [ $? -ne 0 ]; then
echo "rev not found, please install rev"
exit 1
fi
cut=`cut --help`
if [ $? -ne 0 ]; then
echo "cut not found, please install cut"
exit 1
fi
gzip=`gzip --help`
if [ $? -ne 0 ]; then
echo "gzip not found, please install gzip"
exit 1
fi
pv=`pv -V`
if [ $? -ne 0 ]; then
echo "pv not found, please install pv"
exit 1
else
v=`echo $pv | head -n 1 | cut -d " " -f2`
if [ "$v" \< "1.6.6" ]; then
echo "$v of pv is lower than required version: 1.6.6"
exit 1
fi
fi
}
function checkRootType()
{
echo "Checking for root access..."
echo "1) Requesting adbd as root..."
$A root
echo "Waiting for device..."
$A wait-for-any-device
result=`$AROOT whoami`
echo $result
if [[ "$result" == "root" ]]; then
AS=$AROOT
else
result=`$AMAGISK3 whoami`
echo $result
if [[ "$result" == "root" ]]; then
AS=$AMAGISK3
else
result=`$AMAGISK2 whoami`
echo $result
if [[ "$result" == "root" ]]; then
AS=$AMAGISK2
else
result=`$AMAGISK whoami`
echo $result
if [[ "$result" == "root" ]]; then
AS=$AMAGISK
else
echo "Fianlly root is not available for this device, exiting execution."
exit 1
fi
fi
fi
fi
}
function lookForAdbDevice()
{
echo "Waiting for device..."
$A wait-for-any-device
echo "Devices detected:"
$A devices
}
function mkBackupDir()
{
HW=`$AS getprop ro.hardware | tr -d '\r'`
BUILD=`$AS getprop ro.build.id | tr -d '\r'`
DATE=`date +%F`
DIR="${HW}_${DATE}_${BUILD}"
if test -d "$DIR"; then
echo "$DIR already exists, exiting"
exit 2
fi
echo "### Creating dir $DIR"
mkdir -p $DIR
}
function pushBusybox()
{
echo "Determining architecture..."
target_arch="$($AS uname -m)"
case $target_arch in
aarch64|arm64|armv8|armv8a)
target_arch=arm64
;;
aarch32|arm32|arm|armv7|armv7a|armv7l|armv8l|arm-neon|armv7a-neon|aarch|ARM)
target_arch=arm
;;
mips|MIPS|mips32|arch-mips32)
target_arch=mips
;;
mips64|MIPS64|arch-mips64)
target_arch=mips64
;;
x86|x86_32|IA32|ia32|intel32|i386|i486|i586|i686|intel)
target_arch=x86
;;
x86_64|x64|amd64|AMD64|amd)
target_arch=x86_64
;;
*)
echo "Unrecognized architecture $target_arch"
exit 1
;;
esac
echo "Pushing busybox to device..."
$A push busybox-ndk/busybox-$target_arch /sdcard/busybox
$AS "mv /sdcard/busybox /dev/busybox"
$AS "chmod +x /dev/busybox"
if ! $AS "/dev/busybox >/dev/null"; then
echo "Busybox doesn't work here!"
exit 1
fi
}
function stopRuntime()
{
echo "## Stop Runtime" && $AS stop
}
function startRuntime()
{
echo "## Restart Runtime" && $AS start
}
function updateBusybox()
{
if [ ! -d busybox-ndk ]; then
git clone https://github.com/Magisk-Modules-Repo/busybox-ndk
else
pushd busybox-ndk
git pull
popd
fi
}