-
Notifications
You must be signed in to change notification settings - Fork 22
/
backup_method.j2
85 lines (71 loc) · 1.99 KB
/
backup_method.j2
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
#!/bin/bash
set -e
BORG_PASSPHRASE="{{ passphrase }}"
if ssh-keygen -F "{{ server }}" >/dev/null ; then
BORG_RSH="ssh -i /root/.ssh/id_{{ app }}_ed25519 -oStrictHostKeyChecking=yes "
else
BORG_RSH="ssh -i /root/.ssh/id_{{ app }}_ed25519 -oStrictHostKeyChecking=no "
fi
repo=ssh://{{ ssh_user }}@{{ server }}/~/backup #$4
do_need_mount() {
true
}
do_backup() {
export BORG_PASSPHRASE
export BORG_RSH
work_dir=$1
name=$2
repo=$3
size=$4
description=$5
LOGFILE=/var/log/backup_borg.log
ERRFILE=/var/log/backup_borg.err
current_date=$(date +"%d_%m_%y_%H:%M")
pushd $work_dir
set +e
if borg init -e repokey $repo >> $LOGFILE 2>> $ERRFILE; then
#human_size=`echo $size | awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }'`
# Speed in Kbps
#speed=1000
#evaluated_time=$(($size / ($speed * 1000 / 8) / 3600))
echo "Hello,
Your first backup on $repo is starting.
This is an automated message from your beloved YunoHost server." | /usr/bin/mail.mailutils -a "Content-Type: text/plain; charset=UTF-8" -s "[YNH] First backup is starting" "root"
fi
set -e
borg create $repo::${name}_${current_date} ./ >> $LOGFILE 2>> $ERRFILE
popd
borg prune $repo -P ${name} --keep-hourly 2 --keep-daily=7 --keep-weekly=8 --keep-monthly=12 >> $LOGFILE 2>> $ERRFILE
}
do_mount() {
export BORG_PASSPHRASE
export BORG_RSH
work_dir=$1
name=$2
repo=$3
size=$4
description=$5
LOGFILE=/var/log/backup_borg.log
ERRFILE=/var/log/backup_borg.err
borg mount $repo::$name $work_dir >> $LOGFILE 2>> $ERRFILE
}
work_dir=$2
name=$3
size=$5
description=$6
case "$1" in
need_mount)
do_need_mount $work_dir $name $repo $size $description
;;
backup)
do_backup $work_dir $name $repo $size $description
;;
mount)
do_mount
;;
*)
echo "hook called with unknown argument \`$1'" >&2
exit 1
;;
esac
exit 0