forked from frankfanslc/pine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
executable file
·76 lines (65 loc) · 2.66 KB
/
backup
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
#!/bin/sh -l
## configure threads
threads=$(nproc || cat /proc/cpuinfo | grep -c 'core id')
[ -z "$threads" ] && threads=2
## load variables
. /etc/profile
set -a; . /etc/conf.d/dup 2>/dev/null; set +a
## checks
set -e # stop errors after sourcing dup config which can have unset vars
type dup 1>/dev/null || { echo "error: backup tool not found on system!"; exit 1; }
cd /opt/dup/repo || { echo "error: backup repository not found!"; exit 1; }
# clear known_hosts since it causes connections timeouts
[ -e .duplicacy/known_hosts ] && rm .duplicacy/known_hosts
max_runtime=${DUP_MAX_RUNTIME:-3600} # in seconds
storage=${STORAGE_NAME:-default}
dup_prune_args="-keep 0:7 -threads $threads -storage $storage"
with_timeout() {
timeout --foreground $max_runtime $@
}
consul_check() {
which consul &> /dev/null || { echo "consul not found."; false; }
}
## first prune
with_timeout dup prune $dup_prune_args
with_timeout dup check -fossils -resurrect -persist -threads $threads -storage $storage
# Do exclusive pruning to permanently delete chunks from remote
# It should only be enabled on very few hosts
if [ -n "$DUP_PRUNE_EXCL" ]; then
if consul_check; then
consul lock -shell backup '{
set -e
get_running() { running=$(consul kv get -recurse backup/running); n_running=$(echo -n "$running" | wc -l); }
get_running
while true; do
[ "$n_running" -eq 0 ] && break
now=$(date +%s)
for b in $running; do
id=${b/:*}
start=${b/*:}
# this clears stale backups according to defined max_runtime
runtime=$((now-start))
echo "backup from ${id/backup\/running\/} running for $runtime seconds."
if [ $runtime -gt '"$max_runtime"' ]; then
consul kv delete $id
fi
done
get_running
echo "waiting for $n_running backups to complete."
sleep 5
done
# once no backups are running, execute exclusive pruning
dup prune '"$dup_prune_args"' -a -exhaustive -exclusive
}'
fi
fi
# This prevents running backups during exclusive pruning
# set $DUP_NO_LOCKING to disable it
if [ -z "$DUP_NO_LOCKING" ] && consul_check; then
id=${LOCA:-${IPv4:-$(ip a | grep inet | grep global | awk "{print \$2}")}}
now=$(date +%s)
[ -z "$id" ] && id="$(hostname)-$now"
trap "consul kv delete backup/running/$id" EXIT TERM
consul lock backup consul kv put backup/running/$id $now
fi
with_timeout dup backup -stats -threads $threads -storage $storage