-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspi-img
executable file
·225 lines (209 loc) · 9.65 KB
/
raspi-img
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
#!/bin/bash -ex
#------------------------------------------------------------------------------
# June 2016 raspi-img
#
# Script to backup and restore Raspberry Pi SD card images on a computer
# running GNU/Linux. DO NOT run this script on the Pi itself!
#
# Backup:
# Calculates the minimum size of the sd card, and then resizes filesystem
# and partition.
# Subsequently creates an image of the relevant partitions space, compressing
# it on the fly using xz compression algorithms.
#
# Restore:
# Decompresses .img, .xz, .gz and .zip files and copies them to the SD card.
# The file system should be expanded using raspi-config.
#
# Initial script, backup part and all math magic by abracadabricx
# (https://bobbyromeo.com/raspberry-pi/backup-clone-raspberry-pi-sd-card/#comment-4233),
# restore part and compression support by Kalle Friedrich
#------------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <kalle@friedrich.xyz> and <abracadabricx> wrote this file. As long as you
# retain this notice you can do whatever you want with this stuff. If we meet
# some day, and you think this stuff is worth it, you can buy us a beer in
# return.
#------------------------------------------------------------------------------
# variables
#------------------------------------------------------------------------------
BACKUP_PATH=~/Downloads/rpi # Where do your backups go?
COMPRESSION=6 # Accepts values between 0 (fast) and 9 (low filesize)
DEFAULT_BLK=sdb # Where does your SD-Card usually show up?
# Number of colums to be dis
#------------------------------------------------------------------------------
# script begins here
#------------------------------------------------------------------------------
function calcMiB()
{
# Calculate size in MiB, for easier reading.
local partsizeblocks=$1
local KBperblock=$2
local partsizeKiB=$(( ${partsizeblocks} * ${KBperblock} ))
local quotientMiB="$(( (${partsizeblocks} * ${KBperblock}) / 1024 ))"
local modulusMiB=$(( ($partsizeKiB - ($quotientMiB * 1024))*1000/1024))
local modulusMiB2="$(( (${partsizeblocks} % ${KBperblock}) / 1024))"
echo "${quotientMiB},${modulusMiB} MiB."
}
#------------------------------------------------------------------------------
# ensure that xz is installed
#------------------------------------------------------------------------------
echo "Raspberry Pi Backup/Restore Script"
echo ""
if ! type "xz"; then
read -e -p "This script requires xz compression utilities. Would you like to install [1] or abort [2]? " -i "1" yninstall
if [[ "$yninstall" == "1" ]]; then
if type "apt-get"; then
sudo apt-get install xz-utils
elif type "pacman"; then
sudo pacman -S xz
elif type "zypper"; then
sudo zypper install xz
elif type "yum"; then
sudo yum install xz
else
printf "Can't detect your package manager. Please install the xz package manually and run this script again."
exit
fi
fi
fi
#------------------------------------------------------------------------------
# restore
#------------------------------------------------------------------------------
read -e -p "Would you like to backup [1] or restore [2] your SD card? " -i "1" backuprestore
# since the default is '1', an input of '12' means the user's intent is probably to restore rather than backup
if [[ "$backuprestore" == "2" ]] || [[ "$backuprestore" == "12" ]]; then
echo -e "The following filesystems have been found:"
lsblk
echo "Please select the SD card to restore to."
read -e -p "Which blockdevice: " -i "${DEFAULT_BLK}" myblkdev
echo -e "\nThe following backups have been found in $BACKUP_PATH"
PS3='Please enter a number: '
COLUMNS=12
select file in $(find $BACKUP_PATH -type f | sort | sed "s#$BACKUP_PATH/##g"); do
echo -e "\nYou have selected \e[1m$file\e[21m"
read -e -p "Continue [1] or go back [2]: " -i "1" correct
if [[ "$correct" == "1" ]]; then
# unmount partitions
if grep -s "${myblkdev}" /proc/mounts; then
echo -e "\nStart unmounting partitions"
sudo umount -v "/dev/${myblkdev}"? # Questionmark is wildcard.
fi
# execute action based on file extension
exttest=$(basename "$file")
ext="${exttest##*.}"
# raw image files get dd'ed directly
if [[ "$ext" == "img" ]]; then
sudo dd status=progress bs=4M if="$BACKUP_PATH/$file" of="/dev/$myblkdev"
sync
echo -e "\nAll done!"
break
# xz files
elif [[ "$ext" == "xz" ]]; then
xz --verbose --decompress --threads=0 --stdout "$BACKUP_PATH/$file" | sudo dd status=none bs=4M of="/dev/$myblkdev"
echo -e "\nWriting cache to device, please wait..."
sync
echo -e "\nAll done!"
break
# zip files
elif [[ "$ext" == "zip" ]]; then
unzip -vp "$BACKUP_PATH/$file" | sudo dd status=progress bs=4M of="/dev/$myblkdev"
echo -e "\nWriting cache to device, please wait..."
sync
echo -e "\nAll done!"
break
# gzip files
elif [[ "$ext" == "gz" ]]; then
gunzip --keep --stdout "$BACKUP_PATH/$file" | sudo dd status=progress bs=4M of="/dev/$myblkdev"
echo -e "\nWriting cache to device, please wait..."
sync
echo -e "\nAll done!"
break
# wicked stuff
else
echo "Cannot recognize file format. Please provide a file ending in .xz, .img, .zip or .gz in "$BACKUP_PATH""
exit
fi
fi
done
echo -e "\nBoot your Pi and run \e[1mraspi-config\e[21m to expand your file system again."
exit
fi
#------------------------------------------------------------------------------
# resize
#------------------------------------------------------------------------------
# check which drive is the sd card:
echo "The following filesystems have been found:"
lsblk
echo "Please select the SD card to backup from."
read -e -p "Which blockdevice: " -i "${DEFAULT_BLK}" myblkdev
read -e -p "Which partition do you want to shrink: " -i "2" targetpartnr
targetpart="${myblkdev}${targetpartnr}"
# Unmount directories, otherwise online shrinking from resize2fs would be
# required but this throws an error.
if grep -s "${myblkdev}" /proc/mounts
then
echo "Start unmounting partitions"
sudo umount -v "/dev/${myblkdev}"? # Questionmark is wildcard.
fi
# Check the filesystem/partition.
sudo e2fsck -fy "/dev/${targetpart}"
myblockcount=$(sudo tune2fs -l "/dev/${targetpart}" | grep 'Block count' | awk '{print $3}')
myfreeblocks=$(sudo tune2fs -l "/dev/${targetpart}" | grep 'Free blocks' | awk '{print $3}')
myblocksize=$(sudo tune2fs -l "/dev/${targetpart}" | grep 'Block size' | awk '{print $3}')
mysectorsize=$(sudo sfdisk -l "/dev/${myblkdev}" | grep Units | awk '{print $8}')
mystartsector=$(sudo fdisk -l "/dev/${myblkdev}" | grep "${targetpart}" | awk '{print $2}')
# Calculate the smallest partition size, in blocks.
myusedblocks=$(( $myblockcount - $myfreeblocks ))
# Calculate target partion size, adding a bit of margin, about 8%.
mytargetblocks=$(( $myusedblocks + ($myusedblocks * 2 / 25) ))
# Calculate KiB per block to aid further calculation in KiB's:
if (( "${myblocksize}" >= "1024" ))
then
KBperblock=$(( $myblocksize/1024 ))
else
echo "Blocksize is awkward: ${myblocksize}. Not sure what to do, stopping."
exit
fi
# Round up new part size to multiple of blocksize to facilitate creation.
mynewpartsize=$(( (($mytargetblocks + $KBperblock-1) / $KBperblock) * $KBperblock ))
# Calculate and print the existing data and target partition size.
echo ""
echo "The size of the data on partion /dev/${targetpart} is \
$( calcMiB ${myusedblocks} ${KBperblock} )"
echo ""
echo "The new size of partion /dev/${targetpart} will be \
$( calcMiB ${mytargetblocks} ${KBperblock} )"
echo ""
# Calculate multiplier from sector size to block size
sectorsperblock=$(( $myblocksize/$mysectorsize ))
# Calculate end point in sectors, that is what fdisk requires.
mynewendpoint=$(( $mystartsector + ($mynewpartsize * $sectorsperblock) ))
# Start execution:
# Resize the filesystem, values in 1024 bytes, see the "K" at the end.
# Example, if blocksize was 4096, nr of blocks x 4 is the resize value.
sudo resize2fs -fp "/dev/${targetpart}" $(( $mynewpartsize * $KBperblock ))K
# Resize partion, to make matters complicated values are in sector sizes.
# NB, the -s switch does not work, putting Yes after the command is the work around.
sudo parted "/dev/${myblkdev}" unit s resizepart "${targetpartnr}" "${mynewendpoint}" yes
sync
#------------------------------------------------------------------------------
# backup
#------------------------------------------------------------------------------
read -e -p "Backup the resized image? [Y/N] " -i "Y" backupchoice
if [[ "${backupchoice}" =~ ^([yY][eE][sS]|[yY])$ ]]
then
mkdir -p ${BACKUP_PATH}
echo ""
read -e -p "Filename :" -i "$(date +"%Y-%m-%d")_raspbianbackup" Pibackupname
echo ""
# sudo dd if="/dev/${myblkdev}" bs=512 iflag=fullblock count="${mynewendpoint}" | nice -n 10 xz -"${COMPRESSION}" --verbose --threads=0 > "${BACKUP_PATH}"/"${Pibackupname}".img.xz
# sudo dd if="/dev/${myblkdev}" bs=512 iflag=fullblock count="${mynewendpoint}" | pv -s $( (calcMiB ${mytargetblocks} ${KBperblock} | sed s/\ MiB// | sed s/,//) | nice -n 10 xz -"${COMPRESSION}" --verbose --threads=0 > "${BACKUP_PATH}"/"${Pibackupname}".img.xz
sudo dd if="/dev/${myblkdev}" bs=512 iflag=fullblock count="${mynewendpoint}" | pv --interval 1 -s $((${mynewendpoint} * 512)) | nice -n 10 xz -"${COMPRESSION}" --stdout --threads=0 > "${BACKUP_PATH}"/"${Pibackupname}".img.xz
echo ""
echo "Backup succesful."
echo ""
echo "$(xz --list "${BACKUP_PATH}"/"${Pibackupname}".img.xz)"
echo -e "\nBoot your Pi and run \e[1mraspi-config\e[21m to expand your file system again."
fi
exit