-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_files.sh
executable file
·70 lines (60 loc) · 1.56 KB
/
backup_files.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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Run settings file
eval $($DIR/backup_files_settings.sh)
# Check for lock file
if [ -e $TMPDIR/backup_files.lock ]; then
echo "***** Lock file $TMPDIR/backup_files.lock exists, bailing out." >&2
exit 1
fi
# Create lock file
touch $TMPDIR/backup_files.lock
# Delete target directory
ssh $TARGET rm -fr $REMOTE_PATH
if [ $? -eq 0 ]; then
echo "***** Target directory deleted successfully."
done=1
else
echo "***** Delete of target directory failed, bailing out." >&2
exit 2
fi
# Do the backup
done=0
until [ $done -eq 1 ]; do
rsync -rav --delete --exclude-from=$DIR/backup_files.exclude --link-dest=../LATEST/ --timeout=30 $BACKUP_THIS $TARGET:$REMOTE_PATH/
if [ $? -eq 0 ]; then
echo "***** Backup completed!"
done=1
else
echo "***** Backup failed, sleeping 15 seconds before retrying." >&2
sleep 15
fi
done
# Delete the LATEST link
ssh $TARGET rm -fr $REMOTE_LINK
if [ $? -eq 0 ]; then
echo "***** Delete of the link LATEST completed successfully."
done=1
else
echo "***** Delete of the link LATEST failed, bailing out."
exit 2
fi
# Update link to latest backup
ssh $TARGET cp -al $REMOTE_PATH $REMOTE_LINK
if [ $? -eq 0 ]; then
echo "***** cp -al $REMOTE_PATH $REMOTE_LINK completed successfully."
done=1
else
echo "***** cp -al $REMOTE_PATH $REMOTE_LINK failed, bailing out." >&2
exit 2
fi
# Delete lock file
rm $TMPDIR/backup_files.lock
if [ $? -eq 0 ]; then
echo "***** Lock file deleted successfully."
else
echo "***** Failed to delete lock file." >&2
exit 6
fi
# Clean exit
exit 0