-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·72 lines (61 loc) · 1.76 KB
/
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
#!/usr/bin/env bash
set -euo pipefail
scriptdir="$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")"
source "${scriptdir}/common.sh"
dataset="${1}"
repo="$(dataset_to_repo_name "${dataset}")"
if [ -f "${scriptdir}/ignored.txt" ]; then
process_dataset() {
grep -q -v -x -E -f "${scriptdir}/ignored.txt" <<< "${1}"
return "${?}"
}
else
process_dataset() {
return 0
}
fi
if ! dataset_exists "${dataset}"; then
echo ">>> No such dataset '${dataset}'"
exit 1
fi
tz=UTC
borg=(
"${scriptdir}/with_repo.sh"
"${repo}"
chrt -i 0
borg
)
decho ">>> Getting existing archives..."
declare -A known_archives=()
for archive in $("${scriptdir}/list.sh" "${repo}" | jq -r '.archives[] | .archive'); do
known_archives["${archive}"]="1"
done
decho ">>> Done"
# Ensure that repository exists
"${scriptdir}/with_repo.sh" "${repo}" :
zfs list -H -t snapshot -o name,creation -s creation -p "${dataset}" | while read -r -a target; do
_snapname="${target[0]}"
ts="${target[1]}"
_dataset="$(cut -d@ -f1 <<< "${_snapname}")"
snapname="$(cut -d@ -f2- <<< "${_snapname}")"
if ! process_dataset "${snapname}"; then
continue
fi
snapdate="$(env TZ="${tz}" date --date="@${ts}" "+%Y-%m-%d_%H:%M:%S")"
name="files-${snapdate}"
if ! [[ -v known_archives["${name}"] ]]; then
echo ">>> Going for '${snapname}' -> '${name}'"
comment_args=(
--arg ds "${_dataset}"
--arg s "${snapname}"
--arg ts "${ts}"
)
comment="json:$(jq "${comment_args[@]}" -c -r '{ dataset: $ds, snapshot: $s, ts: $ts }' <<< "{}" | base64 -w 0)"
"${scriptdir}/mount_remap.sh" "${_snapname}" "${borg[@]}" create --progress --comment "${comment}" --compression zstd ::"${name}" "./" || {
echo ">>> Failed to backup '${name}'"
exit 1
}
else
decho ">>> Seems like '${_snapname}' is already archived"
fi
done