-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackup.sh
More file actions
109 lines (81 loc) · 2.78 KB
/
Backup.sh
File metadata and controls
109 lines (81 loc) · 2.78 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Make sure the script exits when any command fails
set -Eeuo pipefail
SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
CONFIG="$SCRIPT_DIR/BackupRestore.conf"
# Check if config file exists
if [ ! -f "$CONFIG" ]; then
echo "ERROR: Configuration file $CONFIG cannot be found!"
echo "Please make sure that a configuration file '$CONFIG' is present in the main directory of the scripts."
echo "This file can be created automatically using the setup.sh script."
exit 1
fi
source "$CONFIG"
# Create a log file to record command outputs
touch "$LogFile"
exec > >(tee -a "$LogFile")
exec 2>&1
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
## ---------------------------------- TESTS ------------------------------ #
# Check if the script is being executed by root or with sudo
if [ $EUID -ne 0 ]; then
echo "========== This script needs to be executed as root or with sudo. =========="
exit 1
fi
## -------------------------- MAIN SCRIPT -------------------------- #
# Function to backup
backup() {
BORG_OPTS="--verbose --filter AME --list --progress --stats --show-rc --compression lz4 --exclude-caches"
# Filters for Inclusion Exclusion Borg
BorgFilters="./patterns.lst"
# Create a file with the delete standards Borg Inclusion
tee -a "$BorgFilters" <<EOF
P sh
R /
# DO NOT LOOK IN THESE FOLDERS
! proc
# DIRECTORIES TO BE EXCLUDED FROM BACKUP
- /dev
- /sys
- /tmp
- /run
- /mnt
- /media
- /lost+found
+ /home/*
+ /root/*
# DO NOT INCLUDE ANY MORE FILES
- **
EOF
echo "========== Backing up $( date )... =========="
echo ""
# Start Rclone Mount
systemctl start borgbackup.service
# Backup
borg create $BORG_OPTS --patterns-from $exclude ::'Full-{now:%Y%m%d-%H%M}'
backup_exit=$?
info "Pruning repository"
# Use the subcoming `prune` to keep 7 days, 4 per week and 6 per month
# files of this machine.The prefix '{hostname}-' is very important for
# limits PLA's operation to files in this machine and does not apply to
# Files of other machines too:
borg prune --list --progress --show-rc --keep-daily 7 --keep-weekly 4 --keep-monthly 6
prune_exit=$?
# Sleep for 3 hours before unmounting the drive
sleep 10800
# Stop Rclone Mount
systemctl stop borgbackup.service
}
# Call the backup function
backup
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup, Prune finished successfully" 2>&1 | tee -a
elif [ ${global_exit} -eq 1 ]; then
info "Backup, Prune finished with warnings" 2>&1 | tee -a
else
info "Backup, Prune finished with errors" 2>&1 | tee -a
fi