-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·194 lines (155 loc) · 7.38 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/sh
## General variables. Modify them as you need
chatid="XXXXXXXX" #chatid is telegram chatid to send notifications
api="XXXXXX:XXXXXXXXX" #api is telegram bot's token.
healthcheck="XXXXXXXXXXXX" #Healthcheck is healthchecks.io's API.
log="/etc/borgmatic.d/overkiller-qnap-borg.log" #Log file to store Borg output in the current device
logstorage="/remote_logs" #Where final log will be copied in the remote device
# export BORG_BASE_DIR=/mnt/user/borgdir #Location of Borg Backup base directory (cache and temp files storage). Only use if you are not using default path
export BORG_REPO="/bkp-to" #Repository location
export BORG_PASSPHRASE='XXXXXXXXXXXXXXXXXXXX' #Repository Password
# export BORG_PASSCOMMAND='pass show backup' # or this to ask an external program to supply the passphrase:
## Set current Date
datelog=`date +%Y-%m-%d`
echo "iniciando backup con fecha: $datelog" >> $log
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
info "Starting backup"
# Time count start
timestart=`date +%s`
# Notification to Telegram (Start Backup)
curl -s \
--data parse_mode=HTML \
--data chat_id=$chatid \
--data text="<b>Borg Backup</b>%0A <i>Repo:</i> Overkiller->UnQNAP%0A <i>Tarea:</i> <b>Backup</b>%0A <i>Estado:</i> Iniciando Backup" \
"https://api.telegram.org/bot$api/sendMessage"
# Healthcheck start hook
curl -m 10 --retry 5 https://hc-ping.com/$healthcheck/start
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--files-cache ctime,size \
--exclude-caches \
--exclude '/home/*/.cache/*' \
--exclude '/var/cache/*' \
--exclude '/var/tmp/*' \
--exclude '*@Recycle/*' \
--exclude '*@Recently-Snapshot/*' \
--exclude '*.@__thumb/*' \
\
::'Overkiller-{now:%Y-%m-%d}' \
/mnt/remotes/ \
2>> $log
backup_exit=$?
if [ $backup_exit -eq 0 ]; then backup_re="Backup correcto"
elif [ $backup_exit -eq 1 ]; then backup_re="Backup completado pero con advertencias"
else backup_re="ERROR EN BACKUP"
fi
info "Pruning repository"
# Use the `prune` subcommand to maintain last 8, 12 weekly and 24 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
borg prune \
--list \
--show-rc \
--keep-last 8 \
--keep-weekly 12 \
--keep-monthly 24 \
2>> $log
prune_exit=$?
if [ $prune_exit -eq 0 ]; then prune_re="Prune correcto"
elif [ $prune_exit -eq 1 ]; then prune_re="Prune completado pero con advertencias"
else prune_re="ERROR EN PRUNE"
fi
# Run Borg Compact
borg compact
compact_exit=$?
if [ $compact_exit -eq 0 ]; then compact_re="Compact correcto"
elif [ $compact_exit -eq 1 ]; then compact_re="Compact completado pero con advertencias"
else compact_re="ERROR EN COMPACT"
fi
# 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 and Prune finished successfully" >> $log
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings" >> $log
else
info "Backup and/or Prune finished with errors" >> $log
fi
#capturing log to send using telegram bot
telegramlog="/etc/borgmatic.d/backup$datelog.log"
grep -B 1 -A 100 "Archive name: Overkiller-$datelog" $log > $telegramlog
# Time count stop
timestop=`date +%s`
# Total execution time (in seconds)
runtime=$(( timestop - timestart ))
# Total execution time conversion
D=$((runtime/60/60/24))
H=$((runtime/60/60%24))
M=$((runtime/60%60))
S=$((runtime%60))
totaltime="${D}d, ${H}h, ${M}m, ${S}s"
# Notification to Telegram (End Backup + Send Log)
curl -s \
--data parse_mode=HTML \
--data chat_id=$chatid \
--data text="<b>Borg Backup</b>%0A <i>Repo:</i> Overkiller->QNAP%0A <i>Tarea:</i> <b>Backup</b>%0A <i>Tiempo total:</i>$totaltime%0A <i>Estado:</i> $backup_re , $prune_re , $compact_re" \
"https://api.telegram.org/bot$api/sendMessage"
curl -v -4 -F \
"chat_id=$chatid" \
-F document=@$telegramlog \
-F caption="Log: $datelog.log" \
https://api.telegram.org/bot$api/sendDocument 2> /dev/null
# Deleteting temporal log file
rm $telegramlog
# Healthcheck end hook
curl -m 10 --retry 5 https://hc-ping.com/$healthcheck
# Checks if it's the first week of month, and perform repository check if so.
dia=`date +%d`
if [ "$dia" -ge 1 ] && [ "$dia" -le 7 ]; then # Si el numero del dia esta entre 1 y 7 (primera semana)
curl -s \
--data parse_mode=HTML \
--data chat_id=$chatid \
--data text="<b>Borg Backup</b>%0A <i>Repo:</i> Overkiller->UnQNAP%0A <i>Tarea:</i> <b>Check</b>%0A <i>Estado:</i> Iniciando Check del Repositorio" \
"https://api.telegram.org/bot$api/sendMessage"
echo "================= Primera semana del mes. Iniciando Check =================" >> $log
borg check \
-v \
-p \
--show-rc \
2>> $log
check_exit=$?
if [ $check_exit -eq 0 ]; then check_re="Check correcto"
elif [ $check_exit -eq 1 ]; then check_re="Check completado pero con advertencias"
else check_re="ERROR EN CHECK"
fi
# Notification to Telegram (End Check)
curl -s \
--data parse_mode=HTML \
--data chat_id=$chatid \
--data text="<b>Borg Backup</b>%0A <i>Repo:</i> Overkiller->QNAP%0A <i>Tarea:</i> <b>Check del repositorio</b>%0A <i>Estado:</i> $check_re" \
"https://api.telegram.org/bot$api/sendMessage"
echo "=========================== FINALIZANDO Y APAGANDO UnQNAP ===========================" >> $log
# Copy log to remote device and shutdown local device until next Friday 23:35h
sleep 5
cp $log $logstorage/overkiller-qnap-borg.log
sleep 5
# rtcwake -m off -l -t $(date +%s -d "next friday 23:25") #Uncomment this line if you want rtcwake to sleep your device. Only works if compatible
exit 0
fi
# If it's not the first week of month, don't perform repository check.
echo "=========================== FINALIZANDO Y APAGANDO UnQNAP ===========================" >> $log
# Copy log to remote device and shutdown local device until next Friday 23:35h
cp $log $logstorage/overkiller-qnap-borg.log
sleep 5
# rtcwake -m off -l -t $(date +%s -d "next friday 23:25") #Uncomment this line if you want rtcwake to sleep your device. Only works if compatible
exit ${global_exit}