-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmrilab_borg_backup_sesamo8.sh
executable file
·131 lines (100 loc) · 3.66 KB
/
fmrilab_borg_backup_sesamo8.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
#!/bin/bash
# Only root can run this script
u=$(whoami)
if [[ ! "${u}" == "root" ]]
then
echo "ERROR. Only root can run this script."
exit 0
fi
# https://borgbackup.readthedocs.io/en/stable/quickstart.html
echo "start of script"
export SECONDS=0
date
# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=admin@sesamo8:/volume1/NetBackup/borg_repo
#export BORG_PASSPHRASE='borgensesamo8'
export BORG_PASSPHRASE=$(cat `dirname $0`/private/borg_passphrase_sesamo8)
export BORG_EXCLUDEFILE='/home/inb/soporte/admin_tools/fmrilab_borg_exclude.txt'
#export RSYNC_EXCLUDEFILE='/home/inb/soporte/admin_tools/fmrilab_backup_exclude.txt'
#export BORG_EXCLUDEFILE=/tmp/borg.exclude
#chmod 777 $BORG_EXCLUDEFILE
#sed 's/- /\*\//g' $RSYNC_EXCLUDEFILE > $BORG_EXCLUDEFILE
## Tis is how I initialized the repo:
# root@mansfield$ borg init --encryption=repokey admin@sesamo8:/volume1/NetBackup/borg_repo --remote-path=/usr/local/bin/borg
# admin@sesamo8:/volume1/NetBackup/$ borg config borg_repo additional_free_space 2G
## And I created an rsh key for user root in mansfield, which I then copied to sesamo
# root@mansfield:~# ssh-keygen
# ssh-copy-id admin@sesamo8
# some helpers and error handling:
echo() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
echo "Starting backup"
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
PATHS_TO_BACKUP=/misc/${HOSTNAME}*
# wake up the autofs
echo "INFO. Waking up the partitions to back up"
ls /misc/${HOSTNAME}*/.testDir/.testFile
isOK=1
for d in /misc/${HOSTNAME}*
do
if [ ! -f ${d}/.testDir/.testFile ]
then
echo "ERROR. Cannot find ${d}/.testDir/.testFile"
isOK=0
else
echo "INFO. Found ${d}/.testDir/.testFile"
cat ${d}/.testDir/.testFile
fi
done
if [ $isOK -eq 0 ]
then
echo "ERROR. Cannot continue"
exit 2
fi
borg create \
--remote-path=/usr/local/bin/borg \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
--one-file-system \
--exclude-from=$BORG_EXCLUDEFILE \
::'{hostname}-{now}' \
$PATHS_TO_BACKUP
backup_exit=$?
echo "Pruning repository"
# Use the `prune` subcommand to maintain 3 daily, 1 weekly and 2 monthly
# archives of THIS machine. The '{hostname}-*' matching is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
borg prune \
--remote-path=/usr/local/bin/borg \
--list \
--glob-archives '{hostname}-*' \
--show-rc \
--keep-daily 3 \
--keep-weekly 1 \
--keep-monthly 2
prune_exit=$?
# actually free repo disk space by compacting segments
echo "Compacting repository"
borg compact --remote-path=/usr/local/bin/borg
compact_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
if [ ${global_exit} -eq 0 ]; then
echo "Backup, Prune, and Compact finished successfully"
elif [ ${global_exit} -eq 1 ]; then
echo "Backup, Prune, and/or Compact finished with warnings"
else
echo "Backup, Prune, and/or Compact finished with errors"
fi
echo "End of script"
echo "INFO. Execution time: $SECONDS seconds."
date
exit ${global_exit}