forked from jortan/borgsnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborgsnap_ng.sh
175 lines (149 loc) · 5.19 KB
/
borgsnap_ng.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
169
170
171
172
173
174
175
#!/bin/sh
# borgsnap_ng - licensed under GPLv3. See the LICENSE file for additional
# details.
#
# Perform a ZFS snapshot and rolling backups using borg.
# On the first of the month, a snapshot is done labeled "monthly-".
# Otherwise every Sunday, a snapshot is done is done labeled "weekly-".
# Otherwise every day, a snapshot is done labeled "daily-".
# If no monthly- or weekly- snapshots already exist, these will be done even
# if it is not the first of the month or Sunday.
#
# Keep up to MONTHLY_KEEP monthly backups, WEEKLY_KEEP weekly backups, and
# DAILY_KEEP daily backups.
#
# Usage: borgsnap <command> <config_file> [<args>]
#
# Configuration file documentation:
#
# The configuration file is blindly and dumbly sourced as shell variables,
# hence do not do things such as add whitespace around the "=". There are no
# defaults, all options must be specified. See the example configuration files
# to use as a template.
#
# FS - List ZFS filesystems to backup.
# Example: FS="zroot/root zroot/home zdata/data"
#
# LOCAL - If specified (not ""), directory for local borgbackups. Backups
# will be stored in subdirectories of pool and filesystem, for example
# "/backup/borg/zroot/root". This directory must be created prior to
# running borgsnap.
# Example: LOCAL="/backup/borg"
#
# LOCAL_READABLE_BY_OTHERS - Make borg repo readable by non-root
# Example: LOCAL_READABLE_BY_OTHERS=true
#
# SKIPLOCAL - If specified, borgsnap will skip local backup destinations and
# only issue backup commands to REMOTE destination
#
# RECURSIVE - Create recursive ZFS snapshots for all child filsystems beneath
# all filesystems specified in "FS". All child filesystems will
# be mounted for borgbackup.
# Example: RECURSIVE=true
# or
# RECURSIVE=false
#
# COMPRESS - Choose compression algorithm for Borg backups. Default for borgbackup
# is lz4, default here is zstd (which applies zstd,3)
#
# REMOTE - If specified (not ""), remote connect string and directory. Only
# rsync.net has been tested. The remote directory (myhost in the
# example) will be created if it does not exist.
# Example: REMOTE=""
# Example: REMOTE="XXXX@YYYY.rsync.net:myhost"
#
# PASS - Path to a file containing a single line with the passphrase for borg
# encryption. I generate these with "pwgen 128 1 >/my/path/myhost.key".
# Example: PASS="/path/to/my/super/secret/myhost.key"
#
# MONTHLY_KEEP - Number of monthly backups to keep.
# Example: MONTHLY_KEEP=1
#
# WEEKLY_KEEP - Number of weekly backups to keep.
# Example: WEEKLY_KEEP=4
#
# DAILY_KEEP - Number of daily backups to keep.
# Example: DAILY_KEEP=7
#
# Note that semantics for lifecycles differ for local ZFS snapshots,
# local borg, and remote borg backups. For ZFS snapshots, we delete all but
# the last N snapshots matching the monthly-, weekly-, or daily- labels. For borg,
# this uses "borg prune" rather than "borg delete".
set -u
if [ -z "${LASTFUNC+x}" ]; then
export LASTFUNC=""
fi
export PATH="/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/sbin"
export BINDDIR="/run/borgsnap"
####################################################################################
# control script messaging/ debugging and error handling
####################################################################################
export MSG_DEFINED
export MSG_LEVEL=1
export ERR_HDLR_DEFINED
export BORG_EXIT_CODES=modern
. ./common/msg_and_err_hdlr.sh
if [ -z "${ERR_HDLR_DEFINED+x}" ]; then
die() {
echo "$0: $*" >&2
exit 1
}
echo "$0 - No external Error handler found - using simple internal one!"
ERR_HDLR_DEFINED=1
fi
if [ -z "${MSG_DEFINED+x}" ]; then
msg() {
#########################
# disable messaging
#########################
return 0
}
echo "$0 - No external message handler script defined - Messaging and Debug messages are disabled"
export MSG_DEFINED=1
fi
####################################################################################
. ./common/dir_functions.sh
. ./filesystem/zfs_hdlr.sh
. ./filesystem/zfs_snap_mount.sh
. ./backup/bckp_hdlr.sh
. ./borg/borg_hdlr.sh
. ./cfg_file_hdlr.sh
msg "DEBUG" "$PATH"
usage() {
cat << EOF
usage: $(basename "$0") <command> <config_file> [<args>]
commands:
run Run backup lifecycle.
usage: $(basename "$0") run <config_file>
snap Run backup for specific snapshot.
usage: $(basename "$0") snap <config_file> <snapshot-name>
tidy Unmount and remove snapshots/local backups for today
usage: $(basename "$0") tidy <config_file>
EOF
exit 1
}
# Main script execution
if [ "$#" -eq 0 ]; then
usage
# shellcheck disable=SC2317
exit
fi
case "$1" in
run)
shift # Remove the first argument
readconfigfile "$@"
startBackupMachine "$FS" "$REPOLIST" "$RETENTIONPERIOD" "" "" "";;
#runBackup "$@";;
snap)
shift # Remove the first argument
backupSnapshot "$@";;
tidy)
shift # Remove the first argument
tidyUp "$@";;
-h)
usage;;
*)
echo "$1 is an unknown command!" && usage;;
esac
exit
# [ ] TODO: #23 readconfigfile before backup run?