-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
165 lines (137 loc) · 6.23 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
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
#!/sbin/bash
############################################
# Backup script on Recovery Mode #
# By: SteveZMTstudios #
############################################
# WARNING: #
# This script is only for emergency use. #
# The backup data may not be complete. #
# We not guarantee the backup data. #
# Use at your own risk. #
# You have been warned! #
############################################
DEBUG=0
# verbose when "-v" is passed as an argument
if [[ $1 == "-v" ]] || [ $DEBUG == "1" ]; then
set -x
alias cp='cp -v'
alias tar='tar -v'
alias rm='rm -v'
alias mkdir='mkdir -v'
fi
ui_print() {
echo "$1"
# echo "ui_print $1" > /proc/self/fd/0
# echo "ui_print $1" > /proc/self/fd/1
}
error_handler() {
ui_print "Error occurred at line $1."
ui_print "Exiting the script."
# Perform any necessary cleanup here
rm -rf /tmp/backup_*
exit 1
}
ui_print "Emergency Backup Script"
ui_print "By: SteveZMTstudios"
ui_print "================================"
ui_print "This script is only for emergency use."
ui_print "The backup data may not be complete."
ui_print "We not guarantee the backup data."
ui_print "Use at your own risk."
ui_print "You have been warned!"
ui_print "================================"
# make backup dir
mkdir -p /sdcard/EmergencyBak/
ui_print "- Backup dir created."
num=0
# read packages.xml
packages_file="/data/system/packages.xml"
ui_print "- Backing Up packages.xml."
grep '<package name=' $packages_file | while read -r line; do
# get package name and code path
package_name=$(echo $line | sed -n 's/.*name="\([^"]*\)".*/\1/p')
code_path=$(echo $line | sed -n 's/.*codePath="\([^"]*\)".*/\1/p')
#user_id=$(echo $line | sed -n 's/.*userId="\([^"]*\)".*/\1/p')
ui_print "- Found ${package_name}"
# edit here to backup specific users app
user_id=0
# only backup user app
if [[ $code_path == /data/app* ]]; then
num=$((num + 1))
ui_print "- Selected ${package_name}, the ${num} one."
# create temp backup dir
temp_backup_dir="/tmp/backup_$package_name"
mkdir -p $temp_backup_dir
rm -rf $temp_backup_dir/*
# backup APK files, excluding oat files
ui_print "- Collecting APK files."
find $code_path -type f ! -name '*.oat' ! -name '*.art' ! -name '*.odex' ! -name '*.vdex' ! -path '*lib*' -exec cp --parents {} $temp_backup_dir/ \;
# backup app data, excluding folders or files containing 'cache', 'temp', or 'tmp'
ui_print "- Collecting /data/data/$package_name."
mkdir -p $temp_backup_dir/data_data/
# find "/data/data/$package_name/" -type f -exec cp --parents {} $temp_backup_dir/data_data/files \;
# mkdir -p $temp_backup_dir/data_data/databases
find "/data/data/$package_name" -type f ! -path '*cache*' ! -path '*temp*' ! -path '*tmp*' -exec cp --parents "{}" $temp_backup_dir/data_data/ \;
ui_print "- Collecting /data/user/$user_id/$package_name."
mkdir -p $temp_backup_dir/data_user
find "/data/user/$user_id/$package_name" -type f ! -path '*cache*' ! -path '*temp*' ! -path '*tmp*' -exec cp --parents "{}" $temp_backup_dir/data_user/ \;
ui_print "- Not found is normal."
ui_print "- Collecting /data/user_de/$user_id/$package_name."
mkdir -p $temp_backup_dir/data_user_de
find "/data/user_de/$user_id/$package_name" -type f ! -path '*cache*' ! -path '*temp*' ! -path '*tmp*' -exec cp --parents "{}" $temp_backup_dir/data_user_de/ \;
ui_print "- /data/user_de/${package_name} collected. Not found is normal."
ui_print "- Collecting /data/media/obb/$package_name."
mkdir -p $temp_backup_dir/media_obb
find "/data/media/obb/$package_name" -type f ! -path '*cache*' ! -path '*temp*' ! -path '*tmp*' -exec cp --parents "{}" $temp_backup_dir/media_obb/ \;
ui_print "- Not found is normal."
ui_print "- Collecting /data/media/Android/data/$package_name."
mkdir -p $temp_backup_dir/media_data
find "/data/media/$user_id/Android/data/$package_name" -type f ! -path '*cache*' ! -path '*temp*' ! -path '*tmp*' -exec cp --parents "{}" $temp_backup_dir/media_data/ \;
ui_print "- /data/media/Android/data${package_name} collected. "
ui_print "- ${package_name} is packing... Please wait."
ui_print "[i] Although it seems to be stuck, it is actually working."
# zip backup data
if ! tar -czf /sdcard/EmergencyBak/${package_name}_backup.tar.gz -C $temp_backup_dir .; then
ui_print "! Failed to create tar.gz for ${package_name}."
error_handler $LINENO
fi
# delete temp_backup_dir
rm -rf $temp_backup_dir
ui_print "- ${package_name} is backuped."
fi
done
# Backup DCIM and Pictures folders
ui_print "- Backing up DCIM and Pictures folders."
# Create directories for DCIM and Pictures
mkdir -p /sdcard/EmergencyBak/DCIM
mkdir -p /sdcard/EmergencyBak/Pictures
# Copy DCIM folder
ui_print "- Collecting DCIM folder."
if ! cp -r /sdcard/DCIM/* /sdcard/EmergencyBak/DCIM/; then
ui_print "! Failed to backup DCIM folder."
error_handler $LINENO
else
ui_print "- DCIM folder backed up successfully."
fi
# Copy Pictures folder
ui_print "- Collecting Pictures folder."
if ! cp -r /sdcard/Pictures/* /sdcard/EmergencyBak/Pictures/; then
ui_print "! Failed to backup Pictures folder."
error_handler $LINENO
else
ui_print "- Pictures folder backed up successfully."
fi
# zip the entire backup directory
ui_print "- Zipping the entire backup directory."
ui_print "[i] Although it seems to be stuck, it is actually working."
ui_print "[i] it will take a long time, please be patient."
ui_print "<!> DO NOT EXIT OR REBOOT THE DEVICE <!>"
if ! tar -czvf /sdcard/complete_backup.tar.gz -C /sdcard EmergencyBak; then
ui_print "! Failed to create complete_backup.tar.gz."
error_handler $LINENO
else
ui_print "- The entire backup directory is zipped successfully."
fi
ui_print "Backup completed. Total zipped ${num} apps."
ui_print "Please copy /sdcard/complete_backup.tar.gz to your PC."
ui_print "The detailed backup data is in /sdcard/EmergencyBak/."