-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_backup.sh
executable file
·216 lines (197 loc) · 5.83 KB
/
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
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
#!/bin/bash
#
# Author: Nate Levesque <[email protected]>
# Language: Shell
# Filename: system_backup.sh
#
# Description:
# Basic script that implements a couple of backups
# for user files and the OS. Might require a couple changes
# in the Basic Settings section depending on your setup.
#
# This is online mainly to give me incentive to clean it up
#
# TODO: clean up everything and generally improve the code a lot
########################################################################
# Basic settings. Set these before using the script #
########################################################################
ARCHIVE_TYPE=
FROM=
TO=
EXCLUDE_FILE=
LOG_FILE=
RESTORE=false
NONINVASIVE=false
usage()
{
cat <<HERETO
USAGE: backup --to TO --from FROM --type rsync|tgz
[--exclude EXCLUDE_FILE] [--log LOG_FILE] [--restore] [--safe]
--to Destination directory or archive filename
--from Source directory to back up
--type Type of archive. "tgz" "rsync" or "dry" for a dry run
--exclude List of locations to exclude from the backup. Defaults
to {FROM}/.exclude.
--restore Do a restore instead of a backup.
--safe Don't verwrite existing files and don't delete non-existing ones.
This pertains to both backups and restores. Defaults to
disabled for legacy reasons.
HERETO
}
checkForCommand()
{ # $command
# Checks if a piece of software exists on a system and
# if it doesn't, stops execution and exits with an error.
#
# Arguments:
# $1: a command to test
#
if [ ! `command -v $1` ]; then
echo "You need $1 installed to use this script, exiting..."
exit 1
fi
}
# Defining a few functions to use later
createInfoFile()
{ # $info_file
# Appends information about the backup to the backup file
# date, and a list of excluded files and folders
#
echo `date` > $1
echo "Excluded:" >> $1
[ -e "$EXCLUDE_FILE" ] && cat $EXCLUDE_FILE >> $1
}
logWrite()
{
if [ ! -d "$TO" ]; then
to_log="/dev/null"
else
to_log=$TO/$LOG_FILE
fi
if [ ! -d "$FROM" ]; then
from_log="/dev/null"
else
from_log=$FROM/$LOG_FILE
fi
echo "[$(date)]: $1" | tee -a "$to_log" | tee -a "$from_log"
}
doBackup()
{
logWrite "Starting backup, with options: ARCHIVE_TYPE=$ARCHIVE_TYPE, TO=$TO, FROM=$FROM, SAFE=$NONINVASIVE"
logWrite "Excluding files from $EXCLUDE_FILE"
case $ARCHIVE_TYPE in
dry)
logWrite "Dry run. No backup being done, but pretending we are."
;;
rsync)
if $NONINVASIVE; then
rsync -aL --ignore-errors \
--recursive "$FROM" "$TO" \
--exclude-from "$EXCLUDE_FILE" --info=progress2 \
&& logWrite "Backup completed successfully" || logWrite "Backup completed with errors"
else
rsync -aL --ignore-errors --delete --delete-after \
--recursive "$FROM" "$TO" \
--exclude-from "$EXCLUDE_FILE" --delete-excluded --info=progress2 \
&& logWrite "Backup completed successfully" || logWrite "Backup completed with errors"
fi
exit 0
;;
tgz)
ext=$(echo $TO | rev | cut -d"." -f1 | rev | awk '{print tolower($0)}')
[ "$ext" != "tgz" ] && [ "$ext" != "gz" ] && TO=$TO.
tar -zcf "$TO" -C "$FROM" . \
--exclude=/proc --exclude=/lost+found --exclude=/media \
--exclude=/run --exclude=/mnt --exclude=/sys \
--exclude=/dev/shm --exclude=/tmp --exclude=/home \
&& logWrite "Backup completed successfully" || logWrite "Backup completed with errors"
;;
*)
logWrite "Unsupported backup type $ARCHIVE_TYPE"
exit 1
;;
esac
}
doRestore()
{
logWrite "Starting restore, with options: ARCHIVE_TYPE=$ARCHIVE_TYPE, TO=$TO, FROM=$FROM, SAFE=$NONINVASIVE"
logWrite "Excluding files from $EXCLUDE_FILE"
case $ARCHIVE_TYPE in
dry)
logWrite "Dry run. No restore being done, but pretending we are."
;;
rsync)
if $NONINVASIVE; then
rsync -aL --ignore-errors \
--recursive "$FROM" "$TO" \
--exclude-from "$EXCLUDE_FILE" --info=progress2 \
&& logWrite "Restore completed successfully" || logWrite "Restore completed with errors"
else
rsync -aL --ignore-errors --delete --delete-after \
--recursive "$FROM" "$TO" \
--exclude-from "$EXCLUDE_FILE" --delete-excluded --info=progress2 \
&& logWrite "Restore completed successfully" || logWrite "Restore completed with errors"
fi
exit 0
;;
tgz)
mkdir -p "$TO"
if $NONINVASIVE; then
tar -xpzf "$FROM" -C "$TO" && logWrite "Restore completed successfully" || logWrite "Restore completed with errors"
else
tar -xpzf "$FROM" -C "$TO" --overwrite && logWrite "Restore completed successfully" || logWrite "Restore completed with errors"
fi
;;
*)
logWrite "Unsupported restore type $ARCHIVE_TYPE"
exit 1
;;
esac
}
while [[ $# != 0 ]]; do
case $1 in
--to)
TO=$2
shift
;;
--from)
FROM=$2
shift
;;
--type)
ARCHIVE_TYPE=$2
shift
;;
--exclude)
EXCLUDE_FILE=$2
shift
;;
--restore)
RESTORE=true
;;
--safe)
NONINVASIVE=true
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unrecognized option $1"
usage
exit 1
;;
esac
shift
done
[ -z $EXCLUDE_FILE ] && EXCLUDE_FILE=${FROM}/.exclude
[ ! -f $EXCLUDE_FILE ] && EXCLUDE_FILE=/dev/null
[ -z $LOG_FILE ] && LOG_FILE=".backuplog"
# Check that required software is installed
checkForCommand rsync
checkForCommand tar
if $RESTORE; then
doRestore
else
doBackup
fi