blob: 095f6a687ba3a8a86088e505a96ffb6700cbd02d (
plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#!/bin/bash
#
# Borg script for home folder backups.
# Adapted from https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
#
# See also:
#
# https://borgbackup.readthedocs.io/en/stable/faq.html#if-a-backup-stops-mid-way-does-the-already-backed-up-data-stay-there
# https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-help-placeholders
# Parameters
FULLNAME="$0"
BASENAME="`basename $0`"
DESTINATION="$1"
OPTION="$2"
BASE_CONFIG="$HOME/.config/borger"
CONFIG="$BASE_CONFIG/$DESTINATION"
# Print info
function info {
printf "[$BASENAME] [%s] %s\n" "$(date)" "$*" >&2;
}
# Fata error
function fatal {
info [fatal] $*
exit 1;
}
# Display usage
function borger_usage {
echo "usage: $BASENAME <destination> [--continuous|--list|--check|--info]"
echo -n "available destinations from $BASE_CONFIG: "
ls $BASE_CONFIG
exit 1
}
# Check if in multiple mode
function borger_check_multiple {
if [ ! -e "$CONFIG" ]; then
fatal "No such config $CONFIG"
exit 1
elif [ -d "$CONFIG" ]; then
MULTIPLE="yes"
# Avoid too many loops
if [ "$OPTION" == "--continuous" ]; then
MULTIPLE_OPTION=""
else
MULTIPLE_OPTION="$OPTION"
fi
info "Multiple destination \"$DESTINATION\" found. Processing each subconfig..."
# Config is a folder, so we iterate over all items
# and call borger for each config in parallel
for config in `ls $CONFIG`; do
info "Calling borger for $DESTINATION/$config..."
( $FULLNAME $DESTINATION/$config $MULTIPLE_OPTION 2>&1 | sed -e "s/^\[borger\]/[borger] [$config]/" -e "s/^\([^\[]\)/[borger] [$config] \1/" ) &
done
# Since we dispatched everything to subprocesses,
# there's nothing to do here.
#exit
wait
fi
}
# Process configuration
function borger_config {
# If running on multiple mode, stop here
if [ "$MULTIPLE" == "yes" ]; then
exit
fi
# Ensure we have an username
if [ -z "$USER" ]; then
USER="`whoami`"
fi
# In case your home folder is a symlink
if [ ! -z "`readlink $HOME`" ]; then
ORIG="`readlink $HOME`"
else
ORIG="$HOME"
fi
# Default backup config
KEEPDAILY="7"
KEEPWEEKLY="4"
KEEPMONTHLY="6"
ENCRYPTION="keyfile"
PLACEHOLDER="{user}"
INTERVAL="1h"
source $CONFIG
# Setting this, so the repo does not need to be given on the commandline:
if [ -z "$BORG_REPO" ]; then
export BORG_REPO_DIR="/var/backups/users/$USER/borg"
export BORG_REPO="ssh://$SSH_SERVER:$SSH_PORT/$BORG_REPO_DIR"
fi
# Lockfile location
if [ "$OPTION" == "--continuous" ]; then
LOCKFILE="$TMP/$BASENAME/$DESTINATION-continuous.lock"
else
LOCKFILE="$TMP/$BASENAME/$DESTINATION.lock"
fi
}
# List
function borger_list {
borg list
exit $?
}
# Check
function borger_check {
borg check
exit $?
}
# Info
function borger_info {
borg info
exit $?
}
# Our trap
function borger_trap {
trap 'info Backup interrupted >&2; exit 2' INT TERM
}
# Initialize
function borger_init {
if [ ! -z "$SSH_SERVER" ]; then
# Remote backup over SSH
if ! ssh $SSH_SERVER -p $SSH_PORT test -f $BORG_REPO_DIR/config; then
info "Initializing borg repository at $BORG_REPO..."
borg init --encryption=$ENCRYPTION $BORG_REPO
init_exit=$?
if [ "$init_exit" != "0" ]; then
fatal "Error initializing repository"
fi
fi
else
# Local backup
if [ ! -f "$BORG_REPO/config" ]; then
info "Initializing borg repository at $BORG_REPO..."
borg init --encryption=$ENCRYPTION $BORG_REPO
init_exit=$?
if [ "$init_exit" != "0" ]; then
fatal "Error initializing repository"
fi
fi
fi
}
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
function borger_create {
info "Starting backup..."
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
::"${PLACEHOLDER}-{now}" \
$ORIG
backup_exit=$?
if [ "$backup_exit" != "0" ]; then
info "Error pruning repository"
info "Trying to break the lockfile..."
borg break-lock $BORG_REPO
fi
}
# Use the `prune` subcommand to maintain daily, weekly and monthly archives.
# The '${PLACEHOLDER}-' prefix is very important to limit prune's operation to
# one specific archive and not apply to archives also.
function borger_prune {
info "Pruning repository..."
borg prune \
--list \
--prefix "${PLACEHOLDER}-" \
--show-rc \
--keep-daily $KEEPDAILY \
--keep-weekly $KEEPWEEKLY \
--keep-monthly $KEEPMONTHLY \
prune_exit=$?
#if [ "$prune_exit" != "0" ]; then
# fatal "Error pruning repository"
#fi
}
# Create lockfile
function borger_set_lockfile {
if [ ! -z "$LOCKFILE" ]; then
mkdir -p `dirname $LOCKFILE`
if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then
trap 'borger_unset_lockfile' INT TERM EXIT
else
echo "Could not create lockfile $LOCKFILE, exiting"
exit 1
fi
fi
}
# Remove lockfile
function borger_unset_lockfile {
if [ ! -z "$LOCKFILE" ]; then
rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE"
fi
}
# Check lockfile
function borger_check_lockfile {
local pid process
if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then
pid="`cat $LOCKFILE`"
process="`ps --no-headers -o comm $pid`"
if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then
echo "Another program is running for $LOCKFILE, skipping run"
exit
else
echo "Found old lockfile $LOCKFILE, removing it"
borger_unset_lockfile
fi
fi
}
# Main backup procedure
function borger_run {
borger_check_multiple
borger_config
borger_check_lockfile
borger_set_lockfile
borger_trap
borger_init
borger_create
borger_prune
borger_exit
}
function borger_exit {
# Use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with a warning"
fi
if [ ${global_exit} -gt 1 ]; then
info "Backup and/or Prune finished with an error"
fi
exit ${global_exit}
}
# Ensure we have our base config folder
mkdir -p $BASE_CONFIG
# Dispatch
if [ -z "$DESTINATION" ]; then
borger_usage
elif [ -z "$OPTION" ]; then
borger_run
elif [ "$OPTION" == "--list" ]; then
borger_check_multiple
borger_config
borger_list
elif [ "$OPTION" == "--check" ]; then
borger_check_multiple
borger_config
borger_check
elif [ "$OPTION" == "--info" ]; then
borger_check_multiple
borger_config
borger_info
elif [ "$OPTION" == "--continuous" ]; then
borger_check_multiple
borger_config
borger_check_lockfile
borger_set_lockfile
# Convert the pass command to passphrase otherwise
# the user would be interrupted by a passphrase prompt
# at every iteration
if [ ! -z "$BORG_PASSCOMMAND" ]; then
export BORG_PASSPHRASE="`$BORG_PASSCOMMAND`"
export BORG_PASSCOMMAND=""
fi
while true; do
# Run as a subprocess so we do not exit on any fatal error
$FULLNAME $DESTINATION
info "Running on continous mode... sleeping $INTERVAL..."
sleep $INTERVAL
done
fi
|