-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageBackupRestore.sh
More file actions
executable file
·93 lines (86 loc) · 2.88 KB
/
ImageBackupRestore.sh
File metadata and controls
executable file
·93 lines (86 loc) · 2.88 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
#!/bin/bash
hdd="/dev/sda"
imagefile_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )""/images"
imagefile_prefix="dekanat_laptop_image"
imagefile_suffix=".img.gz"
choice1="Backup: Save image of laptop's hard disk on stick."
choice2="Restore: Write saved image back to the laptop (WARNING: will overwrite hard disk!)."
choice3="Abort: Do nothing."
echo "=== Laptop Image: Backup & Restore ==="
echo ""
if [[ "$EUID" != 0 ]]; then
echo "ERROR: Script needs to be run with root privileges."
echo "Aborting ..."
exit 1
fi
cd $imagefile_directory 2> /dev/null
if [[ $? != 0 ]]; then
echo "ERROR: Cannot go to the directory where the images are: $imagefile_directory."
echo "Aborting ..."
exit 1
fi
echo "What do you want to do (choose number)?"
select choice in "$choice1" "$choice2" "$choice3"
do
echo ""
case $choice in
$choice1)
# BACKUP
imagename=$imagefile_prefix'_'$(date +"%F_%H-%M-%S")$imagefile_suffix
echo "A new backup with the name $imagename will be created."
old_images=$(ls *$imagefile_suffix 2> /dev/null)
num_old_images=$(echo $old_images | wc -w)
if [[ $num_old_images != 0 ]]; then
echo -n "The following $num_old_images old images will be removed to save space: "
echo $old_images
fi
echo -n "Press any key to continue or Ctrl+C to abort:"
read -n 1
echo ""
rm -rf $old_images
dd if=$hdd bs=8M status=progress | pigz -p8 -c > $imagename
if [[ ${PIPESTATUS[0]} != 0 || ${PIPESTATUS[1]} != 0 ]]; then
echo ""
echo "ERROR: An error occured during creation of the backup."
echo "Aborting ..."
exit 1
fi
echo ""
echo "Please wait until all data is written..."
sync
echo "Backup successfully created."
break
;;
$choice2)
# RESTORE
image=$(ls -1r *$imagefile_suffix 2> /dev/null | head -n 1)
if [[ "$image" == "" ]]; then
echo "ERROR: Cannot find any $imagefile_suffix files in $imagefile_directory."
echo "Aborting ..."
exit 1
fi
echo "The image $image will be written to disk."
echo "WARNING: This will overwrite the entire hard disk!"
echo -n "Press any key to continue or Ctrl+C to abort:"
read -n 1
echo ""
unpigz -p8 -c $image | dd of=$hdd bs=8M status=progress
if [[ ${PIPESTATUS[0]} != 0 || ${PIPESTATUS[1]} != 0 ]]; then
echo ""
echo "ERROR: An error occured while restoring the image."
echo "Aborting ..."
exit 1
fi
echo ""
echo "Please wait until all data is written..."
sync
echo "Image successfully restored."
break
;;
*)
# ABORT
echo "Aborting ..."
break
;;
esac
done