-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·49 lines (38 loc) · 1.88 KB
/
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
#!/bin/bash
# Snapshot backup script
# https://github.com/gregrs-uk/snapshot-backup/
# backup location on remote server
# NB This path should not contain spaces, even if they are escaped
DESTINATION="/mnt/Data/backup"
# ssh login to remote server
LOGIN="[email protected]"
# exclude file on local machine
EXCLUDE="/root/snapshot-backup/exclude.txt"
# log dir on local machine
LOGROOT="/var/log/snapshot-backup"
# directories to backup, separated by spaces
DIRS="/home/ /etc/ /root/ /var/log/"
# ------ END OF CONFIGURATION VARIABLES ------
# the following two variables should not need modification
DATETIME=`date +%Y%m%d-%H%M:%S`
DATE=`date +%Y%m%d`
# check directories exist and are accessible
ssh $LOGIN "test -e $DESTINATION" || { echo "Destination directory does not exist"; exit 1; }
# make directory for this snapshot
ssh $LOGIN "mkdir $DESTINATION/$DATETIME-incomplete" || { echo "Could not create snapshot directory"; exit 1; }
# do the rsync
rsync -azR \
--link-dest=$DESTINATION/current/ \
--log-file=$LOGROOT/$DATETIME.log \
--delete --delete-excluded \
--exclude-from $EXCLUDE $DIRS \
$LOGIN:$DESTINATION/$DATETIME-incomplete/
# change name of directory once rsync is complete
ssh $LOGIN "mv $DESTINATION/$DATETIME-incomplete $DESTINATION/$DATETIME" || { echo "Could not rename directory after rsync"; exit 1; }
# link current to this backup
ssh $LOGIN "rm -f $DESTINATION/current" || { echo "Could not remove current backup link"; exit 1; }
ssh $LOGIN "ln -s $DESTINATION/$DATETIME $DESTINATION/current" || { echo "Could not create current backup link"; exit 1; }
# remove backups older than 31 days
ssh $LOGIN "find $DESTINATION/* -maxdepth 0 -type d -mtime +31 -exec rm -r {} \;" || { echo "Could not remove old backups"; exit 1; }
# remove local log files older than 31 days
find $LOGROOT/* -maxdepth 0 -type f -mtime +31 -exec rm {} \; || { echo "Could not remove old log files"; exit 1; }