-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup
More file actions
executable file
·86 lines (73 loc) · 2.03 KB
/
backup
File metadata and controls
executable file
·86 lines (73 loc) · 2.03 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
#!/bin/bash
set -eo pipefail
# # example /etc/backup.env
#
# # restic variables
# export RESTIC_REPOSITORY=
# export RESTIC_PASSWORD_FILE=
# # restic account info for backblaze b2
# export B2_ACCOUNT_ID=
# export B2_ACCOUNT_KEY=
#
# # config files
# export BACKUP_INCLUDE_FILE=/etc/include
# export BACKUP_EXCLUDE_FILE=/etc/exclude
# export BACKUP_RUNPARTS=/etc/backup.d/
#
[ -z "$BACKUP_CONFIG_FILE" ] && BACKUP_CONFIG_FILE="$HOME/.config/backup/backup.env"
forget() {
restic forget \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 12 \
--keep-yearly 2 \
--group-by host \
--verbose \
--prune
}
restic_backup() {
local backup_name="$1"
restic backup \
--verbose \
--tag "${backup_name}" \
--exclude-caches \
--exclude '/home/*/.cache' \
--exclude '/root/.cache' \
--exclude-file "$BACKUP_EXCLUDE_FILE" \
--files-from "$BACKUP_INCLUDE_FILE"
}
runparts() {
local mode="$1"
[ -d "$BACKUP_RUNPARTS" ] \
&& run-parts -v --umask 077 --arg "$mode" "$BACKUP_RUNPARTS"
}
backup() {
local backup_name="$(hostname)-$(date -Id)"
runparts pre || { echo "Runparts pre failed"; exit 1; }
restic_backup || { echo "Backup failed"; exit 2; }
runparts post || { echo "Runparts post failed"; exit 4; }
forget || { echo "Forget failed"; exit 8; }
restic check || { echo "Check failed"; exit 16; }
}
help() {
cat <<EOF
usage: ${0##*/} backup|check|forget|help|init|snapshots|with-env
backup - Run backup
check - Execute restic check
forget - Prune repositry according to policy
help - Print this help
init - Init repository (if it does not already exist)
snapshots - List repository snapshots
with-env - start $SHELL with restic env-vars set
EOF
}
source "$BACKUP_CONFIG_FILE"
cmd=${1:-backup}
case "$cmd" in
init) restic init;;
check) restic check;;
snapshots) restic snapshots;;
with-env) exec $SHELL;;
backup|forget|help) "$cmd";;
*) help; exit 1;;
esac