-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathborg-backup.sh
executable file
·168 lines (140 loc) · 4.08 KB
/
borg-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
#!/bin/bash
set -euo pipefail
function echoerr {
cat <<< "$@" 1>&2;
}
function quit {
if [ -n "${SSHFS:-}" ]; then
fusermount -u "$BORG_REPO"
fi
if [ -n "${1:-}" ]; then
exit "$1"
fi
exit 0
}
: "${DEBUG:=0}"
: "${LOGGING_LEVEL:=--info}"
if [ "${DEBUG}" -eq 1 ]; then
LOGGING_LEVEL='--debug'
set -x
fi
if [ "${SHOW_PROGRESS:=0}" -eq 1 ]; then
PROGRESS='--progress'
else
PROGRESS=''
fi
if [ -n "${SSHFS:-}" ]; then
if [ -n "${SSHFS_IDENTITY_FILE:-}" ]; then
if [ ! -f "$SSHFS_IDENTITY_FILE" ] && [ -n "${SSHFS_GEN_IDENTITY_FILE:-}" ]; then
ssh-keygen -t ed25519 -N '' -f "$SSHFS_IDENTITY_FILE"
cat "${SSHFS_IDENTITY_FILE}.pub"
exit 0
fi
SSHFS_IDENTITY_FILE="-o IdentityFile=${SSHFS_IDENTITY_FILE}"
else
SSHFS_IDENTITY_FILE=''
fi
if [ -n "${SSHFS_PASSWORD:-}" ]; then
SSHFS_PASSWORD="echo '${SSHFS_PASSWORD}' |"
SSHFS_PASSWORD_OPT='-o password_stdin'
else
SSHFS_PASSWORD=''
SSHFS_PASSWORD_OPT=''
fi
if [ "${DEBUG}" -eq 1 ]; then
SSHFS_DEBUG_OPT='--debug -o debug,sshfs_debug,loglevel=debug'
else
SSHFS_DEBUG_OPT=''
fi
mkdir -p /mnt/sshfs
eval "${SSHFS_PASSWORD} sshfs ${SSHFS_DEBUG_OPT} ${SSHFS} /mnt/sshfs ${SSHFS_IDENTITY_FILE} ${SSHFS_PASSWORD_OPT}"
BORG_REPO=/mnt/sshfs
fi
if [ -z "${BORG_REPO:-}" ]; then
# shellcheck disable=SC2016
echoerr 'Variable $BORG_REPO is required. Please set it to the repository location.'
quit 1
fi
if [ "${DEBUG}" -eq 1 ]; then
echoerr "BORG_REPO: $BORG_REPO"
fi
# Borg just needs this
export BORG_REPO
if [ -z "${BORG_PASSPHRASE:-}" ]; then
INIT_ENCRYPTION='--encryption=none'
# shellcheck disable=SC2016
echoerr 'Not using encryption. If you want to encrypt your files, set $BORG_PASSPHRASE variable.'
else
INIT_ENCRYPTION='--encryption=repokey'
fi
DEFAULT_ARCHIVE="${HOSTNAME}_$(date +%Y-%m-%d)"
ARCHIVE="${ARCHIVE:-$DEFAULT_ARCHIVE}"
if [ -n "${EXTRACT_TO:-}" ]; then
mkdir -p "$EXTRACT_TO"
cd "$EXTRACT_TO"
# shellcheck disable=SC2086
borg extract --list --show-rc $LOGGING_LEVEL $PROGRESS ::"$ARCHIVE" ${EXTRACT_WHAT:-}
quit
fi
if [ -n "${BORG_PARAMS:-}" ]; then
# shellcheck disable=SC2086
borg $LOGGING_LEVEL $PROGRESS $BORG_PARAMS
quit
fi
if [ -z "${BACKUP_DIRS:-}" ]; then
# shellcheck disable=SC2016
echoerr 'Variable $BACKUP_DIRS is required. Please fill it with directories you would like to backup.'
quit 1
fi
# If the $BORG_REPO is a local path and the directory is empty, init it
# shellcheck disable=SC2086
if [ "${BORG_REPO:0:1}" == '/' ] && [ ! "$(ls -A $BORG_REPO)" ]; then
INIT_REPO=1
fi
if [ -n "${INIT_REPO:-}" ]; then
# shellcheck disable=SC2086
borg init --show-rc $LOGGING_LEVEL $INIT_ENCRYPTION
fi
if [ -n "${COMPRESSION:-}" ]; then
COMPRESSION="--compression=${COMPRESSION}"
else
COMPRESSION=''
fi
if [ -n "${EXCLUDE:-}" ]; then
OLD_IFS=$IFS
IFS=';'
EXCLUDE_BORG=''
for i in $EXCLUDE; do
EXCLUDE_BORG="${EXCLUDE_BORG} --exclude ${i}"
done
IFS=$OLD_IFS
else
EXCLUDE_BORG=''
fi
# shellcheck disable=SC2086
borg create --stats --show-rc $LOGGING_LEVEL $PROGRESS $COMPRESSION $EXCLUDE_BORG ::"$ARCHIVE" $BACKUP_DIRS
if [ -n "${PRUNE:-}" ]; then
if [ -n "${PRUNE_PREFIX:-}" ]; then
PRUNE_PREFIX="--prefix=${PRUNE_PREFIX}"
else
PRUNE_PREFIX=''
fi
if [ -z "${KEEP_DAILY:-}" ]; then
KEEP_DAILY=7
fi
if [ -z "${KEEP_WEEKLY:-}" ]; then
KEEP_WEEKLY=4
fi
if [ -z "${KEEP_MONTHLY:-}" ]; then
KEEP_MONTHLY=6
fi
# shellcheck disable=SC2086
borg prune --stats --show-rc $LOGGING_LEVEL $PROGRESS $PRUNE_PREFIX --keep-daily=$KEEP_DAILY --keep-weekly=$KEEP_WEEKLY --keep-monthly=$KEEP_MONTHLY
# shellcheck disable=SC2086
borg compact --cleanup-commits --show-rc $LOGGING_LEVEL $PROGRESS
fi
if [ "${BORG_SKIP_CHECK:-}" != '1' ] && [ "${BORG_SKIP_CHECK:-}" != "true" ]; then
# shellcheck disable=SC2086
borg check --show-rc $LOGGING_LEVEL $PROGRESS
fi
quit