-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathraspiBackupRestore2Image.sh
More file actions
executable file
·294 lines (236 loc) · 7.61 KB
/
raspiBackupRestore2Image.sh
File metadata and controls
executable file
·294 lines (236 loc) · 7.61 KB
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
#!/bin/bash
#
#######################################################################################################################
#
# Either create an image file in backupdirectory with extension .dd which can be restored with dd or win32diskimager
# from a tar or rsync backup created by raspiBackup
# or restore the backup to another device, e.g. SD card or USB flashdrive to have a cold SD backup
#
# Visit http://www.linux-tips-and-tricks.de/raspiBackup to get more details about raspiBackup
#
# NOTE: This is sample code how to extend functionality of raspiBackup and is provided as is with no support.
#
#######################################################################################################################
#
# Copyright (c) 2017-2024 framp at linux-tips-and-tricks dot de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Kudos for kmbach who suggested to create this helper and who helped to improve it
#
#######################################################################################################################
MYSELF=${0##*/}
MYNAME=${MYSELF%.*}
VERSION="v0.2.1"
function usage() {
echo "Syntax: $MYSELF <BackupDirectory> [ <restoredevice> (e.g. /dev/sda, /dev/mmcblk1, /dev/nvme0n1) ]"
}
function cleanup() {
local rc=$?
echo "--- Cleaning up"
if (( CREATE_DD_BACKUP )); then
(( $rc )) && rm $IMAGE_FILENAME &>/dev/null
if losetup $RBRI_RESTOREDEVICE &>/dev/null; then
losetup -d "$RBRI_RESTOREDEVICE"
fi
fi
rm -f $MSG_FILE &>/dev/null
}
function calcSumSizeFromSFDISK() { # sfdisk filename
local file="$1"
local partitionregex="/dev/.*[p]?([0-9]+)[^=]+=[^0-9]*([0-9]+)[^=]+=[^0-9]*([0-9]+)[^=]+=[^0-9a-z]*([0-9a-z]+)"
local lineNo=0
local sumSize=0
while IFS="" read line; do
(( lineNo++ ))
if [[ -z $line ]]; then
continue
fi
if [[ $line =~ $partitionregex ]]; then
local p=${BASH_REMATCH[1]}
local start=${BASH_REMATCH[2]}
local size=${BASH_REMATCH[3]}
local id=${BASH_REMATCH[4]}
if [[ $id == 85 || $id == 5 ]]; then
continue
fi
if [[ $sumSize == 0 ]]; then
sumSize=$((start+size))
else
(( sumSize+=size ))
fi
fi
done < "$file"
(( sumSize = ((sumSize - 1)/8 + 1)*4096 )) # align on 4096 boundary to speedup pishrink, kudos for kmbach
echo "$sumSize"
}
# add pathes if not already set (usually not set in crontab)
if [[ -e /bin/grep ]]; then
PATHES="/bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin"
for p in $PATHES; do
if ! /bin/grep -E -q "[\^:]$p[:$]" <<< $PATH; then
[[ -z $PATH ]] && export PATH=$p || export PATH="$p:$PATH"
fi
done
fi
NL=$'\n'
MAIL_EXTENSION_AVAILABLE=0
[[ $(which raspiImageMail.sh) ]] && MAIL_EXTENSION_AVAILABLE=1
if (( $MAIL_EXTENSION_AVAILABLE )); then
# output redirection for email
MSG_FILE=/tmp/msg$$.txt
exec 1> >(stdbuf -i0 -o0 -e0 tee -a "$MSG_FILE" >&1)
exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$MSG_FILE" >&2)
fi
GIT_CODEVERSION="$MYSELF $VERSION"
echo "$GIT_CODEVERSION"
if (( $UID != 0 )); then
echo "$MYSELF has to be invoked via sudo"
exit
fi
# query invocation parms
if (( $# < 1 )); then
echo "??? Missing parameter Backupdirectory"
usage
exit 1
fi
BACKUP_DIRECTORY="$1"
if [[ ! -d $BACKUP_DIRECTORY ]]; then
echo "??? Backupdirectory $BACKUP_DIRECTORY not found"
usage
exit 1
fi
SFDISK_FILE="$(ls $BACKUP_DIRECTORY/*.sfdisk 2>/dev/null)"
if [[ -z "$SFDISK_FILE" ]]; then
echo "??? Incorrect backup path. .sfdisk file of backup not found"
usage
exit 1
fi
if (( $# < 2 )); then
CREATE_DD_BACKUP=1
IMAGE_FILENAME="${SFDISK_FILE%.*}.dd"
RBRI_RESTOREDEVICE=$(losetup -f)
else
CREATE_DD_BACKUP=0
RBRI_RESTOREDEVICE="$2"
if [[ ! -b $RBRI_RESTOREDEVICE ]]; then
echo "??? Incorrect restore device"
usage
exit 1
fi
fi
# check for prerequisites
if [[ ! $(which raspiBackup.sh) ]]; then
echo "raspiBackup.sh not found"
exit 1
fi
if [[ ! $(which pishrink.sh) ]]; then
echo "pishrink.sh not found"
exit 1
fi
if (( CREATE_DD_BACKUP )); then
# wheezy does not discover more than one partition as default
if grep -q "wheezy" /etc/os-release; then
if ! grep -q "RBRI_RESTOREDEVICE.max_part=" /boot/cmdline.txt; then
echo "Add 'RBRI_RESTOREDEVICE.max_part=15' in /boot/cmndline.txt first and reboot"
exit 1
fi
fi
fi
# cleanup
trap cleanup SIGINT SIGTERM EXIT
if (( CREATE_DD_BACKUP )); then
rm "$IMAGE_FILENAME" &>/dev/null
# calculate required image dis size
SOURCE_DISK_SIZE=$(calcSumSizeFromSFDISK $SFDISK_FILE)
mb=$(( $SOURCE_DISK_SIZE / 1024 / 1024 )) # calc MB
echo "===> Backup source disk size: $mb (MiB)"
# create image file
dd if=/dev/zero of="$IMAGE_FILENAME" bs=1024k seek=$(( $mb )) count=0
losetup $RBRI_RESTOREDEVICE $IMAGE_FILENAME
fi
# restore backup now
if (( CREATE_DD_BACKUP )); then
echo "===> Restoring backup into $IMAGE_FILENAME"
else
echo "===> Restoring backup into $RBRI_RESTOREDEVICE"
fi
# prime partitions
sfdisk -uSL -f $RBRI_RESTOREDEVICE < "$SFDISK_FILE"
echo "===> Reloading new partition table"
partprobe $RBRI_RESTOREDEVICE
udevadm settle
sleep 3
f=$(mktemp)
echo 'DEFAULT_YES_NO_RESTORE_DEVICE=""' > $f
raspiBackup.sh -1 -Y -d $RBRI_RESTOREDEVICE -f $f "$BACKUP_DIRECTORY"
RC=$?
rm $f
if (( CREATE_DD_BACKUP )); then
# The disk identifier is the Partition UUID (PTUUID displayed in blkid) and is stored just prior to the partition table in the MBR
# The PARTUUID's aren't actually stored anywhere, they're simply PTUUID-01 for partition 1 and PTUUID-02 for partition 2
# You can change PTUUID on a live system with fdisk
# Extract from https://www.raspberrypi.org/forums/viewtopic.php?t=191775
mkdir -p /mnt1
mount ${RBRI_RESTOREDEVICE}p2 /mnt1
PTUUID=$(grep -E "^[^#]+\s(/)\s.*" /mnt1/etc/fstab | cut -f 1 -d ' ' | sed 's/PARTUUID=//;s/\-.\+//')
umount /mnt1
losetup -d $RBRI_RESTOREDEVICE
if [[ -z $PTUUID ]]; then
echo "??? Unrecoverable error. Unable to find PARTUUID of / in image"
RC=1
fi
# now shrink image
if (( ! $RC )); then
echo "===> PARTUUID to patch into image after pishrink: $PTUUID"
echo
echo "===> Shrinking Image $IMAGE_FILENAME"
pishrink.sh "$IMAGE_FILENAME"
RC=$?
if (( $RC )); then
echo "??? Error $RC received from piShrink"
RC=1
echo "Program ends wihn error 42"
fi
else
echo "??? Error $RC received"
RC=1
fi
# pishrink destroyes PARTUUID with resizsefs, restore original PTUUID now
if (( ! $RC )); then
RBRI_RESTOREDEVICE=$(losetup -f)
echo "===> Patching image PARTUUID with $PTUUID"
losetup -P $RBRI_RESTOREDEVICE $IMAGE_FILENAME
printf "x\ni\n0x$PTUUID\nr\nw\nq\n" | fdisk $RBRI_RESTOREDEVICE
partprobe $RBRI_RESTOREDEVICE
udevadm settle
sleep 3
fi
fi
if (( $MAIL_EXTENSION_AVAILABLE )); then
IMAGE_FILENAME=${IMAGE_FILENAME##*/}
HOST_NAME=${IMAGE_FILENAME%%-*}
if (( $RC )); then
status="with errors finished"
else
status="finished successfully"
fi
BODY="raspiBackupRestore2Image.sh $IMAGE_FILENAME$NL$NL$(echo -e "$(cat $MSG_FILE)")"
raspiImageMail.sh "$HOSTNAME - Restore $status" "$BODY"
if [[ $? = 0 ]]; then
echo "-- Send email succeeded!"
RC=0
fi
fi
exit $RC