-
Notifications
You must be signed in to change notification settings - Fork 3
/
whole_system_backup.sh
executable file
·152 lines (128 loc) · 4.43 KB
/
whole_system_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
#!/bin/bash
# run this script with root
# these are the commands we actually need for the backup
command_list="echo tar hostname date split"
# verify that each command we use exists
for command in $command_list; do
path=`which $command | grep "no $command in"`
if [ ! -x `which $command` -a "$path" ]; then
echo -e "\n\nERROR: $command not found! Check your commands and/or your \$PATH"
exit -1
fi
done
# options for the tar command
tarOptions="--create --absolute-names --preserve-permissions --totals --ignore-failed-read --verbose --file"
# where to put the stage4
stage4Location=/mnt/backups/stage4
if [ ! -d $stage4Location ]; then
mkdir -p $stage4Location
fi
# name prefix
stage4prefix=$(hostname)-stage4-`date '+%Y%m%d%H%M%S'`
# these files/directories are always excluded
default_exclude_list="
--exclude=/tmp/*
--exclude=/lost+found/*
--exclude=/dev/*
--exclude=/proc/*
--exclude=/mnt/*
--exclude=/usr/src/*
--exclude=/usr/portage/distfiles/*
--exclude=/usr/portage/metadata/*
--exclude=/sys/*
--exclude=/var/*
--exclude=$stage4Location"
# depending on your choice these files or directories will additionally be excluded
custom_exclude_list="
--exclude=/home/*"
# check the folder/files stored in $default_exclude_list exist
for exclude in $default_exclude_list; do
if [ ! -e "`echo "$exclude" | cut -d'=' -f2 | cut -d'*' -f1`" ]; then
echo -e "\n\nERROR: `echo "$exclude" | cut -d'=' -f2` not found! Check your \$default_exclude_list"
fi
done
# check the folder/files stored in $custom_exclude_list exist
for exclude in $custom_exclude_list; do
if [ ! -e "`echo "$exclude" | cut -d'=' -f2 | cut -d'*' -f1`" ]; then
echo -e "\n\nERROR: `echo "$exclude" | cut -d'=' -f2` not found! Check your \$custom_exclude_list"
fi
done
# how do you want to backup?
echo -e "\nWhat do you want to do? (Use CONTROL-C to abort)\n
(1) Minimal backup
(2) Interactive backup"
while [ "$option" != '1' -a "$option" != '2' ]; do
echo -en "\nPlease enter your option: "
read option
done
case $option in
1)
stage4Name=$stage4Location/$stage4prefix-minimal
final_command="tar $default_exclude_list $custom_exclude_list $tarOptions $stage4Name.tar /"
;;
2)
for folder in $custom_exclude_list; do
echo -en "Do you want to backup" `echo "$folder" | cut -d'=' -f2`"? (y/n) "
read answer
while [ "$answer" != 'y' -a "$answer" != 'n' ]; do
echo "please enter y or n"
read answer
done
if [ "$answer" == 'n' ]; then
default_exclude_list="$default_exclude_list $folder"
fi
done
stage4Name=$stage4Location/$stage4prefix-custom
final_command="tar $default_exclude_list $tarOptions $stage4Name.tar /"
;;
esac
# show what will be done
echo -e "\n* creating the stage4 at $stage4Location with the following options:\n\n"$final_command
# everything is set, are you sure to continue?
echo -ne "\nDo you want to continue? (y/n) "
read answer
while [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; do
echo "please enter y or n"
read answer
done
if [ "$answer" == 'y' ]; then
# mount boot
echo -e "\n* mount boot"
mount /boot >/dev/null 2>&1
# if necessary, create the stage4Location
if [ ! -d "$stage4Location" ] ; then
echo "* creating directory $stage4Location"
mkdir -p $stage4Location
fi
# check whether the file already exists
if [ -a "$stage4Name.tar.bz2" ]; then
echo -en "\nDo you want to overwrite $stage4Name.tar.bz2? (y/n) "
read answer
while [ "$answer" != 'y' ] && [ "$answer" != 'n' ]; do
echo "please enter y or n"
read answer
done
if [ "$answer" == 'n' ]; then
echo -e "\n* There's nothing to do ... Exiting"
exit 0;
fi
fi
# do the backup
time $final_command
# append for portage
tar -rvf $stage4Name.tar /var/cache/eix/portage.eix
tar -rvf $stage4Name.tar /var/lib/portage
tar -rvf $stage4Name.tar /var/db
bzip2 $stage4Name.tar
# copy the current world file to the stage4 location
echo -e "\n* creating stage4 overview $stage4Name.txt"
#cp /var/lib/portage/world $stage4Name.txt >/dev/null 2>&1
# we finished, clean up
echo "* stage4 is done"
echo "* umounting boot"
umount /boot
else
echo -e "\n* There's nothing to do ... Exiting"
fi
#Uncomment the following command if you want to split the archive in cd size chunks:
#split --suffix-length=1 --bytes=670m $stage4Name.tar.bz2 "$stage4Name"_ && echo "* splitting is done"