-
Notifications
You must be signed in to change notification settings - Fork 0
/
restic-backup
executable file
·145 lines (137 loc) · 3.67 KB
/
restic-backup
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
#!/usr/bin/env bash
# to use this, I use udiskie, which mounts the drive
# with user permissions in /run/media
#
# the UUID can be found by running lsblk -f
#
# I keep the include/exclude lists for backups
# in ~/Documents/BackupManifest
#
# to initialize, do restic-backup restic init
#
# uses https://github.com/purarue/on_machine
# internally to backup to a drive for this
# machine
#
# to skip the cleanup process, I set SKIP_CLEANUP=1
# kondo prompts me to removes a bunch of common
# build/cache directories for programming languages,
# which is useful in case Ive missed one
#
# the other things cleanup/remove extra data in my ~/data
# folder, as well as merge some larger files into single
# instances, so Im not backing up duplicate data
# drive uuid
UUID="${RESTIC_BACKUP_DRIVE_UUID?:'RESTIC_BACKUP_DRIVE_UUID not set'}"
UUID_FILE="/dev/disk/by-uuid/$UUID"
if [[ ! -e "$UUID_FILE" ]]; then
printf 'Could not find drive with UUID %s\n' "$UUID" >&2
exit 1
fi
DISK="$(readlink -f "$UUID_FILE")"
MOUNT_POINT="/run/media/$USER/$UUID"
INCLUDE_FILE_RAW="$XDG_DOCUMENTS_DIR/BackupManifest/include_rel.txt"
EXCLUDE_FILE_RAW="$XDG_DOCUMENTS_DIR/BackupManifest/exclude_rel.txt"
INCLUDE_FILE="$HOME/.cache/restic_include.txt"
EXCLUDE_FILE="$HOME/.cache/restic_exclude.txt"
generate_include_files() {
sed -e "s|^~|$HOME|" <"$INCLUDE_FILE_RAW" >"$INCLUDE_FILE"
sed -e "s|^~|$HOME|" <"$EXCLUDE_FILE_RAW" >"$EXCLUDE_FILE"
}
MACHINE_NAME="${ON_OS:-$(on_machine)}"
backup_to() {
echo "${MOUNT_POINT}/${MACHINE_NAME}"
}
shell() {
echo "DISK=$DISK"
echo "MOUNT_POINT=$MOUNT_POINT"
echo "RESTIC_REPOSITORY=$(backup_to)"
if [[ -n "$RESTIC_PRINT_PASSWORD" ]]; then
echo "RESTIC_PASSWORD=${RESTIC_PASSWORD?:'RESTIC_PASSWORD not set'}"
fi
}
run_backup() {
shell
generate_include_files
if [[ -z "$DRY" ]] && [[ -z "$SKIP_CLEANUP" ]]; then
kondo ~/Repos ~/Files
remove-broken-sms-files -v
remove-empty-ipython-dbs -v
bleanser-runall
merge-mpv-history
merge-browser-history
fi
flags=(-r "$(backup_to)" backup --verbose --one-file-system
--files-from "$INCLUDE_FILE"
--exclude-file "$EXCLUDE_FILE" "$@")
echo "Running: restic ${flags[*]}"
restic "${flags[@]}"
# update the timestamp on a cache file so we know when the last backup was
[[ -z "$DRY" ]] && touch "$HOME/.cache/restic-backup.lastrun"
}
main() {
case "$1" in
shell)
# to use this, run `eval "$(system-backup shell)"` in the shell
# when the drive is mounted
shell
;;
backup)
if [[ ! -d "$MOUNT_POINT" ]]; then
echo "Drive not mounted"
exit 1
fi
run_backup
;;
restic)
# run restic directly (useful for any other commands)
shift
exec restic -r "$(backup_to)" "$@"
;;
forget-latest)
local snapshot
snapshot="$(restic -r "$(backup_to)" list snapshots -q | head -n 1)"
[[ -n "$snapshot" ]] && echo "Forgetting $snapshot"
restic -r "$(backup_to)" forget "$snapshot"
;;
diff-latest)
restic -r "$(backup_to)" list snapshots -q | head -n 2 | tac | xargs -n 2 restic -r "$(backup_to)" diff
;;
last-snapshot)
local snapshot
restic -r "$(backup_to)" list snapshots -q | head -n 1
;;
dry)
if [[ ! -d "$MOUNT_POINT" ]]; then
echo "Drive not mounted"
exit 1
fi
DRY=1 run_backup --dry-run
;;
poweroff)
# unmount and poweroff the drive
if [[ ! -d "$MOUNT_POINT" ]]; then
echo "Drive not mounted"
exit 1
fi
udisksctl unmount -b "$DISK"
udisksctl power-off -b "$DISK"
;;
connected)
# check if the drive is connected
if [[ -d "$MOUNT_POINT" ]]; then
echo "Backup drive is connected"
exit 0
else
echo "Backup drive is not connected"
exit 1
fi
;;
*)
printf "Usage: "
grep -E '^\s*[A-Za-z-]+\)' <"$0" | chomp | tr -d ')' | paste -d '|' -s
exit 1
;;
esac
}
main "$@"