-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·177 lines (140 loc) · 4.96 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
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
# Copyright (c) 2017 Marnix Kaart
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# comment these next two lines when you have modified CRUFT
echo "[!] make sure to modify CRUFT variable to your needs before first run!"
exit
# stuff that may be removed from backups
CRUFT=("AppDomain/nl.omroep.radio4"
"AppDomain/nl.omroep.uitzendinggemist"
"CameraRollDomain/Media/PhotoData/Thumbnails"
"AppDomain/com.apple.calculator"
"AppDomain/nl.sanomadigital.nuhd"
"AppDomain/nl.tmobile.mytmobile"
"AppDomain/com.surfcheck.hetweer"
"AppDomain/nl.rtl.buienradar"
"AppDomain/nl.anwb.iphone.onderweg"
"HomeDomain/Library/SpringBoard"
"HomeDomain/Library/Cookies"
)
# the backup directory
BACKUPDIR=$1
if [ ! -e "$BACKUPDIR" ]; then
echo "[!] $BACKUPDIR does not exist, abort!"
exit
fi
# operate from the backupdir
pushd "$BACKUPDIR" >/dev/null 2>&1
if [ -e previous_backup ]; then
echo "[!] previous backup still exists, abort!"
exit
fi
if [ -e previous.deviceinfo ]; then
echo "[!] previous deviceinfo still exists, abort!"
exit
fi
echo "[x] idevicepair pair"
idevicepair pair
err=$?
if [ ! $err -eq 0 ]; then
echo "[!] error pairing to device, abort!"
exit
fi
if [ -e current.deviceinfo ]; then
echo "[x] mv current.deviceinfo previous.deviceinfo"
mv current.deviceinfo previous.deviceinfo
fi
echo "[x] ideviceinfo > current.deviceinfo"
ideviceinfo > current.deviceinfo
err=$?
if [ ! $err -eq 0 ]; then
echo "[!] error connecting to device, abort!"
exit
fi
uniqueid=`grep UniqueDeviceID current.deviceinfo | awk '{print $2}'`
if [ -e current_backup ]; then
echo "[x] previous backup exists"
if [ ! -e current_backup/$uniqueid ]; then
echo "[!] previous backup is probably not from current device, abort!"
echo " [x] mv current.deviceinfo failed.deviceinfo"
mv current.deviceinfo failed.deviceinfo
echo " [x] mv previous.deviceinfo current.deviceinfo"
mv previous.deviceinfo current.deviceinfo
echo " [!] you may remove or keep failed.deviceinfo"
exit
fi
echo "[x] cp -al current_backup previous_backup"
cp -al current_backup previous_backup
fi
if [ ! -e current_backup ]; then
echo "[x] mkdir current_backup"
mkdir current_backup
fi
echo "[x] idevicebackup2 backup --full current_backup"
idevicebackup2 backup --full current_backup
# sometimes first attempt does not finish succesfully
echo "[x] idevicebackup2 backup --full current_backup (2nd)"
idevicebackup2 backup --full current_backup
err=$?
if [ ! $err -eq 0 ]; then
echo "[!] error making backup"
exit
fi
if [ ! -e previous_backup ]; then
echo "[x] no previous backup found, we are done!"
echo " [!] latest backup is in $BACKUPDIR/current_backup"
exit
fi
# determine date of previous backup to use in name of preserved files
DATE=`pearback info -D previous_backup/*`
if [ ! -e preserved ]; then
echo "[x] mkdir preserved"
mkdir preserved
fi
if [ -e preserved/$DATE ]; then
echo "[!] directory preserved/$DATE already exists, abort!"
exit
fi
echo "[x] mkdir preserved/$DATE"
mkdir preserved/$DATE
echo "[x] mv previous.deviceinfo preserved/$DATE.deviceinfo"
mv previous.deviceinfo preserved/$DATE.deviceinfo
echo "[x] extracting changed or removed files into preserved/$DATE"
pearback diff -e preserved/$DATE -l previous_backup/* current_backup/*
# remove previous backup
echo "[x] rm -rf previous_backup"
rm -rf previous_backup
echo "[!] latest backup is in $BACKUPDIR/current_backup"
echo "[!] files that where removed between $DATE and today are in $BACKUPDIR/preserved/$DATE"
# make sure user is aware that some stuff will be removed
while true; do
read -p "[>] remove cruft from $BACKUPDIR/preserved/$DATE? (y/n/?) " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
[?]* ) echo " [!] following paths are on cruftlist:";
for c in "${CRUFT[@]}"; do echo " $c" ; done;;
* ) echo "Please answer y or n.";
esac
done
for c in "${CRUFT[@]}"; do
echo " [x] rm -rf preserved/$DATE/$c"
rm -rf "preserved/$DATE/$c"
done
echo "[!] done"
popd > /dev/null 2>&2