forked from seamusdemora/RonR-RPi-image-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-backup
505 lines (483 loc) · 13.2 KB
/
image-backup
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/bin/bash
trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
MNTPATH="/tmp/img-backup-mnt"
ONEMB=$((1024 * 1024))
mkloop()
{
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
}
rmloop()
{
if [ "${LOOP}" != "" ]; then
losetup -d "${LOOP}"
LOOP=""
fi
}
mntimg()
{
MNTED=TRUE
mkloop
if [ ! -d "${MNTPATH}/" ]; then
mkdir "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to make ROOT partition mount point"
fi
fi
mount "${LOOP}p2" "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image ROOT partition"
fi
if [ ! -d "${MNTPATH}/boot/" ]; then
mkdir -p "${MNTPATH}/boot/"
if [ $? -ne 0 ]; then
errexit "Unable to make BOOT partition mount point"
fi
fi
mount "${LOOP}p1" "${MNTPATH}/boot/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image BOOT partition"
fi
}
umntimg()
{
umount "${MNTPATH}/boot/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image BOOT partition"
fi
umount "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image ROOT partition"
fi
rmloop
rm -r "${MNTPATH}/"
MNTED=FALSE
}
errexit()
{
echo ""
echo -e "$1"
echo ""
if [ "${MNTED}" = "TRUE" ]; then
umount "${MNTPATH}/boot/" &> /dev/null
umount "${MNTPATH}/" &> /dev/null
rm -rf "${MNTPATH}/" &> /dev/null
fi
rmloop
exit 1
}
fsckerr()
{
errexit "Filesystem appears corrupted "$1" resize2fs"
}
usage()
{
errexit "Usage: $0 [options] [pathto/imagefile for incremental backup]\n\
-h,--help This usage description\n\
-i,--initial pathto/filename of image file [,inital size MB [,added space for incremental MB]]\n\
-n,--noexpand Do not expand filesystem on first run after restore\n\
-o,--options Additional rsync options (comma separated)\n\
-x,--extract Extract image from NOOBS (force BOOT partition to -01 / ROOT partition to -02)"
}
backup()
{
mntimg
sync
rsync -aDH --partial --numeric-ids --delete --force --exclude "${MNTPATH}" --exclude '/dev' --exclude '/lost+found' --exclude '/media' --exclude '/mnt' \
--exclude '/proc' --exclude '/run' --exclude '/sys' --exclude '/tmp' --exclude '/var/swap' --exclude '/etc/udev/rules.d/70-persistent-net.rules' \
--exclude '/var/lib/asterisk/astdb.sqlite3-journal' "${OPTIONS[@]}" / "${MNTPATH}/"
if [[ $? -ne 0 && $? -ne 24 ]]; then
errexit "Unable to create backup"
fi
mkdir -p "${MNTPATH}/dev/" "${MNTPATH}/lost+found/" "${MNTPATH}/media/" "${MNTPATH}/mnt/" "${MNTPATH}/proc/" "${MNTPATH}/run/" "${MNTPATH}/sys/" "${MNTPATH}/tmp/"
if [ $? -ne 0 ]; then
errexit "Unable to create image directories"
fi
chmod a+rwxt "${MNTPATH}/tmp/"
if [ "${EXTRACT}" = "TRUE" ]; then
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PTUUID}-02\2|" "${MNTPATH}/boot/cmdline.txt"
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\s\+.*\)$|PARTUUID=${PTUUID}-01\1|" "${MNTPATH}/etc/fstab"
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/\s\+.*\)$|PARTUUID=${PTUUID}-02\1|" "${MNTPATH}/etc/fstab"
fi
if [[ "${NOEXPAND}" = "FALSE" && "${ROOT_TYPE}" != "f2fs" && $(grep -v '^[[:space:]]*#' "${MNTPATH}/etc/rc.local" | grep -c 'resize-root-fs') -eq 0 ]]; then
cat <<\EOF1 > "${MNTPATH}/etc/resize-root-fs"
#!/bin/bash
ROOT_PART="$(mount | sed -n 's|^\(/dev/.*\) on / .*|\1|p')"
ROOT_DEV="$(sed 's/[0-9]\+$//' <<< "${ROOT_PART}")"
if [ "${ROOT_DEV}" = "/dev/mmcblk0p" ]; then
ROOT_DEV="${ROOT_DEV:0:(${#ROOT_DEV} - 1)}"
fi
START=$(sfdisk -d "${ROOT_DEV}" | sed -n "s|^${ROOT_PART}\s\+:.*start=\s*\([0-9]\+\).*$|\1|p")
PTTYPE="$(blkid "${ROOT_DEV}" | sed -n 's|^.*PTTYPE="\(\S\+\)".*|\1|p')"
if [ "${PTTYPE}" = "dos" ]; then
sfdisk --delete "${ROOT_DEV}" 2 > /dev/null
echo "${START},+" | sfdisk --no-reread --no-tell-kernel -N2 "${ROOT_DEV}" &> /dev/null
else
PARTUUID="$(blkid "${ROOT_PART}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
sgdisk -d 2 "${ROOT_DEV}" > /dev/null
sgdisk -n 2:${START}:0 "${ROOT_DEV}" > /dev/null
sgdisk -u 2:${PARTUUID} "${ROOT_DEV}" > /dev/null
fi
cat <<\EOF2 > /etc/init.d/resize_root_fs
#!/bin/sh
### BEGIN INIT INFO
# Provides: resize_root_fs
# Required-Start:
# Required-Stop:
# Default-Start: 3
# Default-Stop:
# Short-Description: Resize root filesystem
# Description:
### END INIT INFO
case "$1" in
start)
resize2fs "$(mount | sed -n 's|^\(/dev/.*\) on / .*|\1|p')"
update-rc.d resize_root_fs remove
rm /etc/init.d/resize_root_fs
;;
*)
exit 3
;;
esac
EOF2
chmod +x /etc/init.d/resize_root_fs
update-rc.d resize_root_fs defaults
sed -i '/resize-root-fs/d' /etc/rc.local
rm /etc/resize-root-fs
shutdown --no-wall -r now
EOF1
chmod +x "${MNTPATH}/etc/resize-root-fs"
sed -i 's|^exit 0$|/etc/resize-root-fs\nexit 0|' "${MNTPATH}/etc/rc.local"
fi
sync
umntimg
}
LOOP=""
MNTED=FALSE
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [ $(id -u) -ne 0 ]; then
errexit "$0 must be run as root user"
fi
PGMNAME="$(basename $0)"
for PID in $(pidof -x -o %PPID "${PGMNAME}"); do
if [ ${PID} -ne $$ ]; then
errexit "${PGMNAME} is already running"
fi
done
gdisk -l "${DEVICE}" &> /dev/null
if [ $? -eq 127 ]; then
echo ""
echo "gdisk not installed"
echo ""
echo -n "Ok to install gdisk (y/n)? "
while read -r -n 1 -s answer; do
if [[ "${answer}" = [yYnN] ]]; then
echo "${answer}"
if [[ "${answer}" = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
echo ""
echo "Installing gdisk"
echo ""
apt-get update
apt-get install gdisk
fi
rsync --version &> /dev/null
if [ $? -ne 0 ]; then
errexit "rsync not installed (run: apt-get install rsync)"
fi
ROOT_PART="$(mount | sed -n 's|^\(/dev/.*\) on / .*|\1|p')"
ROOT_DEV_P="$(sed 's/[0-9]\+$//' <<< "${ROOT_PART}")"
ROOT_DEV="${ROOT_DEV_P}"
if [ "${ROOT_DEV}" = "/dev/mmcblk0p" ]; then
ROOT_DEV="${ROOT_DEV:0:(${#ROOT_DEV} - 1)}"
fi
ROOT_TYPE=$(blkid "${ROOT_PART}" | sed -n 's|^.*TYPE="\(\S\+\)".*|\1|p')
PTTYPE="$(blkid "${ROOT_DEV}" | sed -n 's|^.*PTTYPE="\(\S\+\)".*|\1|p')"
if [[ "${PTTYPE}" != "dos" && "${PTTYPE}" != "gpt" ]]; then
errexit "Unsupported partition table type: ${PTTYPE}"
fi
PARTUUID_B="$(blkid "${ROOT_DEV_P}1" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
PARTUUID_R="$(blkid "${ROOT_PART}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
PTUUID="$(blkid "${ROOT_DEV}" | sed -n 's|^.*PTUUID="\(\S\+\)".*|\1|p')"
DFKFREE=$(df | grep "% /$" | awk '{print $3}')
INISIZE=$(((${DFKFREE} + (${DFKFREE} / 5)) / 1024))
IMGNAME=""
IMGSIZE=""
IMGINCR=""
IMGFILE=""
OPTIONS=()
EXTRACT=FALSE
NOEXPAND=FALSE
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage
;;
-i|--initial)
OIFS=${IFS}
IFS=','
INITIAL=($2)
IFS=${OIFS}
IMGNAME=${INITIAL[0]}
IMGSIZE=${INITIAL[1]}
IMGINCR=${INITIAL[2]}
shift 2
;;
-n|--noexpand)
NOEXPAND=TRUE
shift
;;
-o|--options)
OIFS=${IFS}
IFS=','
OPTIONS=($2)
IFS=${OIFS}
shift 2
;;
-x|--extract)
EXTRACT=TRUE
shift
;;
-*|--*)
usage
;;
*)
IMGFILE="$1"
shift
;;
esac
done
if [ "${IMGFILE}" = "" ]; then
if [ "${IMGNAME}" != "" ]; then
IMGFILE="${IMGNAME}"
if [[ ! "${IMGFILE}" =~ ^/mnt/.*$ && ! "${IMGFILE}" =~ ^/media/.*$ ]]; then
errexit "${IMGFILE} does not begin with /mnt/ or /media/"
fi
if [ -d "${IMGFILE}" ]; then
errexit "${IMGFILE} is a directory"
fi
if [ "${IMGSIZE}" = "" ]; then
IMGSIZE=${INISIZE}
fi
IRFSSIZE=${IMGSIZE}
if [ "${IMGINCR}" = "" ]; then
IMGINCR=0
fi
ADDMB=${IMGINCR}
else
while :
do
echo ""
read -r -e -i "${IMGFILE}" -p "Image file to create? " IMGFILE
if [ "${IMGFILE}" = "" ]; then
continue
elif [[ ! "${IMGFILE}" =~ ^/mnt/.*$ && ! "${IMGFILE}" =~ ^/media/.*$ ]]; then
echo ""
echo "${IMGFILE} does not begin with /mnt/ or /media/"
continue
fi
if [ -d "${IMGFILE}" ]; then
echo ""
echo "${IMGFILE} is a directory"
elif [ -f "${IMGFILE}" ]; then
echo ""
echo -n "${IMGFILE} already exists, Ok to delete (y/n)? "
while read -r -n 1 -s answer; do
if [[ "${answer}" = [yYnN] ]]; then
echo "${answer}"
if [[ "${answer}" = [yY] ]]; then
break 2
else
break 1
fi
fi
done
else
break
fi
done
echo ""
read -r -e -p "Initial image file ROOT filesystem size (MB) [${INISIZE}]? " IRFSSIZE
if [ "${IRFSSIZE}" = "" ]; then
IRFSSIZE=${INISIZE}
fi
if [ "${ROOT_TYPE}" != "f2fs" ]; then
echo ""
read -r -e -p "Added space for incremental updates after shrinking (MB) [0]? " ADDMB
if [ "${ADDMB}" = "" ]; then
ADDMB=0
fi
fi
echo ""
echo -n "Create ${IMGFILE} (y/n)? "
while read -r -n 1 -s answer; do
if [[ "${answer}" = [yYnN] ]]; then
echo "${answer}"
if [[ "${answer}" = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
fi
if [ -f "${IMGFILE}" ]; then
rm "${IMGFILE}"
if [ $? -ne 0 ]; then
errexit "Unable to delete existing image file"
fi
fi
truncate -s $(((${IRFSSIZE} + 256) * ${ONEMB})) "${IMGFILE}"
if [ $? -ne 0 ]; then
errexit "Unable to create image file"
fi
if [ "${PTTYPE}" = "dos" ]; then
echo "label: dos" | sfdisk "${IMGFILE}" > /dev/null
sfdisk "${IMGFILE}" <<EOF > /dev/null
,256MiB,c
,+,83
EOF
else
sgdisk -Z "${IMGFILE}" &> /dev/null
sgdisk -n 1:0:+256M "${IMGFILE}" &> /dev/null
sgdisk -t 1:0700 "${IMGFILE}" > /dev/null
sgdisk -n 2:0:0 "${IMGFILE}" &> /dev/null
sgdisk -t 2:8300 "${IMGFILE}" > /dev/null
gdisk "${IMGFILE}" <<EOF > /dev/null
r
h
1
n
0c
n
n
w
y
EOF
fi
mkloop
mkfs.vfat -F 32 "${LOOP}p1" > /dev/null
if [ $? -ne 0 ]; then
errexit "Unable to create image BOOT filesystem"
fi
dosfsck "${LOOP}p1" > /dev/null
if [ $? -ne 0 ]; then
errexit "Image BOOT filesystem appears corrupted"
fi
if [ "${ROOT_TYPE}" = "f2fs" ]; then
mkfs.f2fs "${LOOP}p2" > /dev/null
else
mkfs.ext4 -q -b 4096 "${LOOP}p2" > /dev/null
fi
if [ $? -ne 0 ]; then
errexit "Unable to create image ROOT filesystem"
fi
rmloop
echo ""
echo "Starting full backup (for incremental backups, run: $0 ${IMGFILE})"
backup
if [ "${ROOT_TYPE}" != "f2fs" ]; then
echo ""
mkloop
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "before"
fi
echo ""
resize2fs -f -M "${LOOP}p2"
resize2fs -f -M "${LOOP}p2"
resize2fs -f -M "${LOOP}p2"
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "after"
fi
INFO="$(tune2fs -l "${LOOP}p2" 2>/dev/null)"
rmloop
NEWSIZE=$(sed -n 's|^Block count:\s*\(.*\)|\1|p' <<< "${INFO}")
BLKSIZE=$(sed -n 's|^Block size:\s*\(.*\)|\1|p' <<< "${INFO}")
IMGFILE_P="${IMGFILE}"
if [[ "${IMGFILE_P: -1}" =~ ^[[:digit:]]$ ]]; then
IMGFILE_P+='p'
fi
START=$(sfdisk -d ${IMGFILE} | sed -n "s|^${IMGFILE_P}2\s\+:.*start=\s*\([0-9]\+\).*$|\1|p")
NEWEND=$((${START} + (${NEWSIZE} * (${BLKSIZE} / 512)) + ((${ADDMB} * ${ONEMB}) / 512) - 1))
if [ "${PTTYPE}" = "gpt" ]; then
((NEWEND += 33))
fi
truncate -s $(((${NEWEND} + 1) * 512)) "${IMGFILE}"
if [ "${PTTYPE}" = "dos" ]; then
sfdisk --delete "${IMGFILE}" 2 > /dev/null
echo "${START},+" | sfdisk -N2 "${IMGFILE}" &> /dev/null
fdisk "${IMGFILE}" <<EOF > /dev/null
x
i
0x${PTUUID}
r
w
EOF
else
sgdisk -Z "${IMGFILE}" &> /dev/null
sgdisk -n 1:0:+256M "${IMGFILE}" &> /dev/null
sgdisk -t 1:0700 "${IMGFILE}" > /dev/null
sgdisk -n 2:0:0 "${IMGFILE}" &> /dev/null
sgdisk -t 2:8300 "${IMGFILE}" > /dev/null
sgdisk -u 1:"${PARTUUID_B}" "${IMGFILE}" > /dev/null
sgdisk -u 2:"${PARTUUID_R}" "${IMGFILE}" > /dev/null
sgdisk -U "${PTUUID}" "${IMGFILE}" > /dev/null
gdisk "${IMGFILE}" <<EOF > /dev/null
r
h
1
n
0c
n
n
w
y
EOF
fi
if [ ${ADDMB} -ne 0 ]; then
echo ""
mkloop
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "before"
fi
echo ""
resize2fs -f "${LOOP}p2"
e2fsck -f -n "${LOOP}p2"
if [ $? -ne 0 ]; then
fsckerr "after"
fi
rmloop
fi
fi
mntimg
while read DIR
do
touch -r "${DIR}" "${MNTPATH}${DIR}"
done <<< "$(echo "$(rsync -aDH --dry-run --itemize-changes --partial --numeric-ids --delete --force --exclude "${MNTPATH}" --exclude '/dev' \
--exclude '/lost+found' --exclude '/media' --exclude '/mnt' --exclude '/proc' --exclude '/run' --exclude '/sys' --exclude '/tmp' --exclude '/var/swap' \
--exclude '/etc/udev/rules.d/70-persistent-net.rules' --exclude '/var/lib/asterisk/astdb.sqlite3-journal' "${OPTIONS[@]}" / "${MNTPATH}/")" | \
sed -n "s|^\.d\.\.t\.\.\.\.\.\.\s\+\(.*$\)|/\1|p")"
umntimg
echo ""
else
if [[ ! "${IMGFILE}" =~ ^/mnt/.*$ && ! "${IMGFILE}" =~ ^/media/.*$ ]]; then
errexit "${IMGFILE} does not begin with /mnt/ or /media/"
fi
if [ -d "${IMGFILE}" ]; then
errexit "${IMGFILE} is a directory"
elif [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
fi
backup
fi
sync