/
300_load_archives.sh
82 lines (68 loc) · 2.95 KB
/
300_load_archives.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
# This file is part of Relax-and-Recover, licensed under the GNU General
# Public License. Refer to the included COPYING for full text of license.
#
# 300_load_archives.sh
LogPrint "Starting Borg restore"
# Store number of lines in BORGBACKUP_ARCHIVE_CACHE file for later use.
archive_cache_lines=$( wc -l "$BORGBACKUP_ARCHIVE_CACHE" | awk '{print $1}' )
# This means empty repository.
if [ "$archive_cache_lines" -eq 0 ]; then
Error "Borg repository $BORGBACKUP_REPO on ${BORGBACKUP_HOST:-USB} is empty!"
fi
# Display list of archives in repository.
# Display header.
LogUserOutput "
=== Borg archives list ===
Location: ${BORGBACKUP_HOST:-USB}
Repository: $BORGBACKUP_REPO
Number of archives: $archive_cache_lines"
# Display BORGBACKUP_ARCHIVE_CACHE file content
# and prompt user for archive to restore.
# Always ask which archive to restore (even if there is only one).
# This gives possibility to abort restore if repository doesn't contain
# desired archive, hence saves some time.
# Pagination for selecting archives:
# Show BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER archives at a time, starting
# with the current ones.
# If no valid choice is given, cycle through older archives.
# Enabled by default (BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER=10).
# To disable pagination set BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER=0.
archive_cache_last_shown=0
# For timestamp output of Borg archives ISO 8601 format is used:
# YYYY-MM-DDThh:mm:ss, e.g.: 2020-05-26T00:25:00
# When pagination is disabled by the user, show everything
[[ $BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER -eq 0 ]] \
&& BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER=$archive_cache_lines
while true ; do
UserOutput ""
LogUserOutput "$( cat -n "$BORGBACKUP_ARCHIVE_CACHE" \
| awk '{print "["$1"]", $4 "T" $5, $2 }' \
| head -n $(( archive_cache_lines - archive_cache_last_shown )) \
| tail -n "$BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER" )"
(( archive_cache_last_shown += BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER ))
UserOutput ""
if [[ $archive_cache_last_shown -lt $archive_cache_lines ]]; then
LogUserOutput "[0] Show (up to) $BORGBACKUP_RESTORE_ARCHIVES_SHOW_NUMBER older archives"
else
archive_cache_last_shown=0
LogUserOutput "[0] Show all archives again"
fi
# Show "Exit" option.
UserOutput ""
LogUserOutput "[$(( archive_cache_lines + 1 ))]" Exit
UserOutput ""
# Read user input.
choice="$( UserInput -I BORGBACKUP_ARCHIVE_TO_RECOVER -p "Choose archive to recover from" )"
# Evaluate user selection and save archive name to restore.
# Valid pick
if [[ $choice -ge 1 && $choice -le $archive_cache_lines ]]; then
# shellcheck disable=SC2034
BORGBACKUP_ARCHIVE=$(sed "$choice!d" "$BORGBACKUP_ARCHIVE_CACHE" \
| awk '{print $1}')
break
# Exit
elif [[ $choice -eq $(( archive_cache_lines + 1 )) ]]; then
Error "Operation aborted by user"
break
fi
done