-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.sh
More file actions
86 lines (68 loc) · 1.79 KB
/
Copy pathbackup.sh
File metadata and controls
86 lines (68 loc) · 1.79 KB
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
#!/bin/bash
set -eu
# Variables needed by borg
export BORG_REPO='ssh://user@host/path/to/backup/repo'
export BORG_PASSPHRASE=$(cat "$HOME/.borg-passphrase")
export BORG_REMOTE_PATH=/bin/borg
export BORG_RSH='ssh -oBatchMode=yes'
# Variables for the script itself
BORG_EXEC=/usr/sbin/borg
LOGPATH=$HOME/Desktop/logs/borg
LOGFILE=borg_`date +"%Y-%m-%d"`.log
OLDLOGFILE=dailybackup_`date -d"60 days ago" +"%Y-%m-%d"`.log
####
## Functions
####
errorcheck()
{
if [ "${1}" -ne "0" ]; then
echo "Returned ${1} : ${2}"
writelog "ERROR! Exiting! Returned ${1} : ${2}"
exit $1
fi
}
writelog()
{
echo `date +"%Y-%m-%d %H:%M:%S"` ": ${1}" >> $LOGPATH/$LOGFILE
}
####
## Script Body
####
writelog "Beginning Backup"
writelog "Cleaning up old log files"
# If the old local log file exists, delete it.
if [ -e $LOGPATH/$OLDLOGFILE ]; then
rm $LOGPATH/$OLDLOGFILE
errorcheck $? "Deleting $LOGPATH/$OLDLOGFILE"
writelog "Deleted $LOGPATH/$OLDLOGFILE"
fi
writelog "Old log file cleanup complete"
# Initialize the backup; not needed for routine work.
# $BORG_EXEC init --encryption=repokey-blake2
writelog "Beginning borg create"
# Do the backup.
# Add /var/www back in for production usage.
$BORG_EXEC \
create \
--verbose \
--stats \
--list \
--exclude '/home/*/.cache/*' \
::{hostname}-{now:%Y-%m-%dT%H:%M:%S} \
/home/$USER \
>> $LOGPATH/$LOGFILE 2>&1
errorcheck $? "Running borg create"
writelog "Completed borg create"
writelog "Beginning borg prune"
# Clean up the old backups.
$BORG_EXEC \
prune \
--stats \
--list \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=1 \
>> $LOGPATH/$LOGFILE 2>&1
errorcheck $? "Running borg prune"
writelog "Completed borg prune"
writelog "Backup Complete"