-
Notifications
You must be signed in to change notification settings - Fork 238
/
dt-s3-backup.sh
executable file
·457 lines (399 loc) · 14.3 KB
/
dt-s3-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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/bin/bash
#
# Copyright (c) 2008-2010 Damon Timm.
# Copyright (c) 2010 Mario Santagiuliana.
#
# 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/>.
#
# MORE ABOUT THIS SCRIPT AVAILABLE IN THE README AND AT:
#
# http://damontimm.com/code/dt-s3-backup
#
# ---------------------------------------------------------------------------- #
# AMAZON S3 INFORMATION
export AWS_ACCESS_KEY_ID="foobar_aws_key_id"
export AWS_SECRET_ACCESS_KEY="foobar_aws_access_key"
# If you aren't running this from a cron, comment this line out
# and duplicity should prompt you for your password.
export PASSPHRASE="foobar_gpg_passphrase"
# Specify which GPG key you would like to use (even if you have only one).
GPG_KEY="foobar_gpg_key"
# The ROOT of your backup (where you want the backup to start);
# This can be / or somwhere else -- I use /home/ because all the
# directories start with /home/ that I want to backup.
ROOT="/home/"
# BACKUP DESTINATION INFORMATION
# In my case, I use Amazon S3 use this - so I made up a unique
# bucket name (you don't have to have one created, it will do it
# for you). If you don't want to use Amazon S3, you can backup
# to a file or any of duplicity's supported outputs.
#
# NOTE: You do need to keep the "s3+http://<your location>/" format
# even though duplicity supports "s3://<your location>/".
#DEST="s3+http://backup-bucket/backup-folder/"
DEST="file:///home/foobar_user_name/new-backup-test/"
# INCLUDE LIST OF DIRECTORIES
# Here is a list of directories to include; if you want to include
# everything that is in root, you could leave this list empty (I think).
#
# Here is an example with multiple locations:
#INCLIST=( "/home/*/Documents" \
# "/home/*/Projects" \
# "/home/*/logs" \
# "/home/www/mysql-backups" \
# )
#
# Simpler example with one location:
INCLIST=( "/home/foobar_user_name/Documents/Prose/" )
# EXCLUDE LIST OF DIRECTORIES
# Even though I am being specific about what I want to include,
# there is still a lot of stuff I don't need.
EXCLIST=( "/home/*/Trash" \
"/home/*/Projects/Completed" \
"/**.DS_Store" "/**Icon?" "/**.AppleDouble" \
)
# STATIC BACKUP OPTIONS
# Here you can define the static backup options that you want to run with
# duplicity. I use both the `--full-if-older-than` option plus the
# `--s3-use-new-style` option (for European buckets). Be sure to separate your
# options with appropriate spacing.
STATIC_OPTIONS="--full-if-older-than 14D --s3-use-new-style"
# FULL BACKUP & REMOVE OLDER THAN SETTINGS
# Because duplicity will continue to add to each backup as you go,
# it will eventually create a very large set of files. Also, incremental
# backups leave room for problems in the chain, so doing a "full"
# backup every so often isn't not a bad idea.
#
# You can either remove older than a specific time period:
#CLEAN_UP_TYPE="remove-older-than"
#CLEAN_UP_VARIABLE="31D"
# Or, If you would rather keep a certain (n) number of full backups (rather
# than removing the files based on their age), you can use what I use:
CLEAN_UP_TYPE="remove-all-but-n-full"
CLEAN_UP_VARIABLE="2"
# LOGFILE INFORMATION DIRECTORY
# Provide directory for logfile, ownership of logfile, and verbosity level.
# I run this script as root, but save the log files under my user name --
# just makes it easier for me to read them and delete them as needed.
LOGDIR="/home/foobar_user_name/logs/test2/"
LOG_FILE="duplicity-`date +%Y-%m-%d_%H-%M`.txt"
LOG_FILE_OWNER="foobar_user_name:foobar_user_name"
VERBOSITY="-v3"
# EMAIL ALERT (*thanks: rmarescu*)
# Provide an email address to receive the logfile by email. If no email
# address is provided, no alert will be sent.
# You can set a custom from email address and a custom subject (both optionally)
# If no value is provided for the subject, the following value will be
# used by default: "DT-S3 Alert ${LOG_FILE}"
# MTA used: mailx
#EMAIL="[email protected]"
EMAIL_TO=
EMAIL_FROM=
EMAIL_SUBJECT=
# TROUBLESHOOTING: If you are having any problems running this script it is
# helpful to see the command output that is being generated to determine if the
# script is causing a problem or if it is an issue with duplicity (or your
# setup). Simply uncomment the ECHO line below and the commands will be
# printed to the logfile. This way, you can see if the problem is with the
# script or with duplicity.
#ECHO=$(which echo)
##############################################################
# Script Happens Below This Line - Shouldn't Require Editing #
##############################################################
LOGFILE="${LOGDIR}${LOG_FILE}"
DUPLICITY="$(which duplicity)"
S3CMD="$(which s3cmd)"
MAIL="$(which mailx)"
NO_S3CMD="WARNING: s3cmd is not installed, remote file \
size information unavailable."
NO_S3CMD_CFG="WARNING: s3cmd is not configured, run 's3cmd --configure' \
in order to retrieve remote file size information. Remote file \
size information unavailable."
README_TXT="In case you've long forgotten, this is a backup script that you used to backup some files (most likely remotely at Amazon S3). In order to restore these files, you first need to import your GPG private key (if you haven't already). The key is in this directory and the following command should do the trick:\n\ngpg --allow-secret-key-import --import s3-secret.key.txt\n\nAfter your key as been succesfully imported, you should be able to restore your files.\n\nGood luck!"
CONFIG_VAR_MSG="Oops!! ${0} was unable to run!\nWe are missing one or more important variables at the top of the script.\nCheck your configuration because it appears that something has not been set yet."
if [ ! -x "$DUPLICITY" ]; then
echo "ERROR: duplicity not installed, that's gotta happen first!" >&2
exit 1
elif [ `echo ${DEST} | cut -c 1,2` = "s3" ]; then
if [ ! -x "$S3CMD" ]; then
echo $NO_S3CMD; S3CMD_AVAIL=false
elif [ ! -f "${HOME}/.s3cfg" ]; then
echo $NO_S3CMD_CFG; S3CMD_AVAIL=false
else
S3CMD_AVAIL=true
fi
fi
if [ ! -d ${LOGDIR} ]; then
echo "Attempting to create log directory ${LOGDIR} ..."
if ! mkdir -p ${LOGDIR}; then
echo "Log directory ${LOGDIR} could not be created by this user: ${USER}"
echo "Aborting..."
exit 1
else
echo "Directory ${LOGDIR} successfully created."
fi
elif [ ! -w ${LOGDIR} ]; then
echo "Log directory ${LOGDIR} is not writeable by this user: ${USER}"
echo "Aborting..."
exit 1
fi
get_source_file_size()
{
echo "---------[ Source File Size Information ]---------" >> ${LOGFILE}
for exclude in ${EXCLIST[@]}; do
DUEXCLIST="${DUEXCLIST}${exclude}\n"
done
for include in ${INCLIST[@]}
do
echo -e $DUEXCLIST | \
du -hs --exclude-from="-" ${include} | \
awk '{ print $2"\t"$1 }' \
>> ${LOGFILE}
done
echo >> ${LOGFILE}
}
get_remote_file_size()
{
echo "------[ Destination File Size Information ]------" >> ${LOGFILE}
if [ `echo ${DEST} | cut -c 1,2` = "fi" ]; then
TMPDEST=`echo ${DEST} | cut -c 6-`
SIZE=`du -hs ${TMPDEST} | awk '{print $1}'`
elif [ `echo ${DEST} | cut -c 1,2` = "s3" ] && $S3CMD_AVAIL ; then
TMPDEST=$(echo ${DEST} | cut -c 11-)
SIZE=`s3cmd du -H s3://${TMPDEST} | awk '{print $1}'`
else
SIZE="s3cmd not installed."
fi
echo "Current Remote Backup File Size: ${SIZE}" >> ${LOGFILE}
echo >> ${LOGFILE}
}
include_exclude()
{
for include in ${INCLIST[@]}
do
TMP=" --include="$include
INCLUDE=$INCLUDE$TMP
done
for exclude in ${EXCLIST[@]}
do
TMP=" --exclude "$exclude
EXCLUDE=$EXCLUDE$TMP
done
EXCLUDEROOT="--exclude=**"
}
duplicity_cleanup()
{
echo "-----------[ Duplicity Cleanup ]-----------" >> ${LOGFILE}
${ECHO} ${DUPLICITY} ${CLEAN_UP_TYPE} ${CLEAN_UP_VARIABLE} ${STATIC_OPTIONS} --force \
--encrypt-key=${GPG_KEY} \
--sign-key=${GPG_KEY} \
${DEST} >> ${LOGFILE}
echo >> ${LOGFILE}
}
duplicity_backup()
{
${ECHO} ${DUPLICITY} ${OPTION} ${VERBOSITY} ${STATIC_OPTIONS} \
--encrypt-key=${GPG_KEY} \
--sign-key=${GPG_KEY} \
${EXCLUDE} \
${INCLUDE} \
${EXCLUDEROOT} \
${ROOT} ${DEST} \
>> ${LOGFILE}
}
get_file_sizes()
{
get_source_file_size
get_remote_file_size
sed -i '/-------------------------------------------------/d' ${LOGFILE}
chown ${LOG_FILE_OWNER} ${LOGFILE}
}
backup_this_script()
{
if [ `echo ${0} | cut -c 1` = "." ]; then
SCRIPTFILE=$(echo ${0} | cut -c 2-)
SCRIPTPATH=$(pwd)${SCRIPTFILE}
else
SCRIPTPATH=$(which ${0})
fi
TMPDIR=dt-s3-backup-`date +%Y-%m-%d`
TMPFILENAME=${TMPDIR}.tar.gpg
README=${TMPDIR}/README
echo "You are backing up: "
echo " 1. ${SCRIPTPATH}"
echo " 2. GPG Secret Key: ${GPG_KEY}"
echo "Backup will be saved to: `pwd`/${TMPFILENAME}"
echo
echo ">> Are you sure you want to do that ('yes' to continue)?"
read ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "You said << ${ANSWER} >> so I am exiting now."
exit 1
fi
mkdir -p ${TMPDIR}
cp $SCRIPTPATH ${TMPDIR}/
gpg -a --export-secret-keys ${GPG_KEY} > ${TMPDIR}/s3-secret.key.txt
echo -e ${README_TXT} > ${README}
echo "Encrypting tarball, choose a password you'll remember..."
tar c ${TMPDIR} | gpg -aco ${TMPFILENAME}
rm -Rf ${TMPDIR}
echo -e "\nIMPORTANT!!"
echo ">> To restore these files, run the following (remember your password):"
echo "gpg -d ${TMPFILENAME} | tar x"
echo -e "\nYou may want to write the above down and save it with the file."
}
check_variables ()
{
if [[ ${ROOT} = "" || ${DEST} = "" || ${INCLIST} = "" || \
${AWS_ACCESS_KEY_ID} = "foobar_aws_key_id" || \
${AWS_SECRET_ACCESS_KEY} = "foobar_aws_access_key" || \
${GPG_KEY} = "foobar_gpg_key" || \
${PASSPHRASE} = "foobar_gpg_passphrase" ]]; then
echo -e ${CONFIG_VAR_MSG}
echo -e ${CONFIG_VAR_MSG}"\n-------- END --------" >> ${LOGFILE}
exit 1
fi
}
echo -e "-------- START DT-S3-BACKUP SCRIPT --------\n" >> ${LOGFILE}
if [ "$1" = "--backup-script" ]; then
backup_this_script
exit
elif [ "$1" = "--full" ]; then
check_variables
OPTION="full"
include_exclude
duplicity_backup
duplicity_cleanup
get_file_sizes
elif [ "$1" = "--verify" ]; then
check_variables
OLDROOT=${ROOT}
ROOT=${DEST}
DEST=${OLDROOT}
OPTION="verify"
echo -e "-------[ Verifying Source & Destination ]-------\n" >> ${LOGFILE}
include_exclude
duplicity_backup
OLDROOT=${ROOT}
ROOT=${DEST}
DEST=${OLDROOT}
get_file_sizes
echo -e "Verify complete. Check the log file for results:\n>> ${LOGFILE}"
elif [ "$1" = "--restore" ]; then
check_variables
ROOT=$DEST
OPTION="restore"
if [[ ! "$2" ]]; then
echo "Please provide a destination path (eg, /home/user/dir):"
read -e NEWDESTINATION
DEST=$NEWDESTINATION
echo ">> You will restore from ${ROOT} to ${DEST}"
echo "Are you sure you want to do that ('yes' to continue)?"
read ANSWER
if [[ "$ANSWER" != "yes" ]]; then
echo "You said << ${ANSWER} >> so I am exiting now."
echo -e "User aborted restore process ...\n" >> ${LOGFILE}
exit 1
fi
else
DEST=$2
fi
echo "Attempting to restore now ..."
duplicity_backup
elif [ "$1" = "--restore-file" ]; then
check_variables
ROOT=$DEST
INCLUDE=
EXCLUDE=
EXLUDEROOT=
OPTION=
if [[ ! "$2" ]]; then
echo "Which file do you want to restore (eg, mail/letter.txt):"
read -e FILE_TO_RESTORE
FILE_TO_RESTORE=$FILE_TO_RESTORE
echo
else
FILE_TO_RESTORE=$2
fi
if [[ "$3" ]]; then
DEST=$3
else
DEST=$(basename $FILE_TO_RESTORE)
fi
echo -e "YOU ARE ABOUT TO..."
echo -e ">> RESTORE: $FILE_TO_RESTORE"
echo -e ">> TO: ${DEST}"
echo -e "\nAre you sure you want to do that ('yes' to continue)?"
read ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "You said << ${ANSWER} >> so I am exiting now."
echo -e "-------- END --------\n" >> ${LOGFILE}
exit 1
fi
echo "Restoring now ..."
#use INCLUDE variable without create another one
INCLUDE="--file-to-restore ${FILE_TO_RESTORE}"
duplicity_backup
elif [ "$1" = "--list-current-files" ]; then
check_variables
OPTION="list-current-files"
${DUPLICITY} ${OPTION} ${VERBOSITY} ${STATIC_OPTIONS} \
--encrypt-key=${GPG_KEY} \
--sign-key=${GPG_KEY} \
${DEST}
echo -e "-------- END --------\n" >> ${LOGFILE}
elif [ "$1" = "--backup" ]; then
check_variables
include_exclude
duplicity_backup
duplicity_cleanup
get_file_sizes
else
echo -e "[Only show `basename $0` usage options]\n" >> ${LOGFILE}
echo " USAGE:
`basename $0` [options]
Options:
--backup: runs an incremental backup
--full: forces a full backup
--verify: verifies the backup
--restore [path]: restores the entire backup
--restore-file [file] [destination/filename]: restore a specific file
--list-current-files: lists the files currently backed up in the archive
--backup-script: automatically backup the script and secret key to the current working directory
CURRENT SCRIPT VARIABLES:
========================
DEST (backup destination) = ${DEST}
INCLIST (directories included) = ${INCLIST[@]:0}
EXCLIST (directories excluded) = ${EXCLIST[@]:0}
ROOT (root directory of backup) = ${ROOT}
"
fi
echo -e "-------- END DT-S3-BACKUP SCRIPT --------\n" >> ${LOGFILE}
if [ $EMAIL_TO ]; then
if [ ! -x "$MAIL" ]; then
echo -e "Email couldn't be sent. mailx not available." >> ${LOGFILE}
else
EMAIL_FROM=${EMAIL_FROM:+"-r ${EMAIL_FROM}"}
EMAIL_SUBJECT=${EMAIL_SUBJECT:="DT-S3 Alert ${LOG_FILE}"}
cat ${LOGFILE} | ${MAIL} -s """${EMAIL_SUBJECT}""" $EMAIL_FROM ${EMAIL_TO}
echo -e "Email alert sent to ${EMAIL_TO} using ${MAIL}" >> ${LOGFILE}
fi
fi
if [ ${ECHO} ]; then
echo "TEST RUN ONLY: Check the logfile for command output."
fi
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset PASSPHRASE
# vim: set tabstop=2 shiftwidth=2 sts=2 autoindent smartindent: