-
Notifications
You must be signed in to change notification settings - Fork 12
/
backup-home
executable file
·106 lines (88 loc) · 2.86 KB
/
backup-home
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
#!/bin/bash
#
# backup-home - back up home folder using rdiff-backup
#
# Copyright (C) 2019 Rodrigo Silva (MestreLion) <[email protected]>
# License: GPLv3 or later, at your choice. See <http://www.gnu.org/licenses/gpl>
#------------------------------------------------------------------------------
set -Eeuo pipefail # Exit on most errors.
trap '>&2 echo "error: line $LINENO, status $?: $BASH_COMMAND"' ERR
# See https://fvue.nl/wiki/Bash:_Error_handling for caveats
# Factory default options, configurable by config file
# See notes on default config file creation
dest=${1:-}
mount=
sudo_mount=0
excludes=(
"$HOME"/.cache
"$HOME"/.thumbnails
"$HOME"/.local/share/Trash
'**/.Trash-*'
'**/*cache*/*'
'**/Temp/*'
'**/Temporary Internet Files/*'
)
options=(
--print-statistics
--terminal-verbosity 5
--exclude-other-filesystems
--exclude-special-files # Exclude all device files, fifo files, socket files, and symbolic links.
--include-symbolic-links # Re-include symbolic links
)
# Config file location
config=${XDG_CONFIG_HOME:-$HOME/.config}/backup-home.conf
myname="${0##*/}"
fatal() { [[ "${1:-}" ]] && echo "$myname: error: $@" >&2 ; exit 1; }
exists() { type "$@" >/dev/null 2>&1; }
escape() { printf '%q' "$1"; }
if ! exists rdiff-backup; then
sudo apt install -y rdiff-backup
fi
if ! [[ -f "$config" ]]; then
echo "warning: config file not found, creating blank one at $config" >&2
cat > "$config" <<-EOF
# backup-home config file
# Sourced by bash, mind the syntax!
# Full path to backup destination directory
# Can also be set on each run by command line argument
target=$(escape "$dest")
# Mountpoint to auto-mount, usually backup destination dir or a parent
# Usually requires an entry on /etc/fstab for the device, set to 'noauto'
# Leave blank to disable automount
mount=
# If auto-mount and its mountpoint require sudo or not. 0 = no, 1 = yes
sudo_mount=0
# rdiff-backup general options
# Must be a Bash array
options=(
--print-statistics
--terminal-verbosity 5
--exclude-special-files # Exclude all device files, fifo files, socket files, and symbolic links.
--include-symbolic-links # Re-include symbolic links
)
# Paths to exclude from backup. Each entry will be prepended with
# '--exclude=' and included to rdiff-backup options
# Must be a Bash array
excludes=(
"\$HOME"/.cache
"\$HOME"/.thumbnails
"\$HOME"/.local/share/Trash
'**/.Trash-*'
'**/*cache*/*'
'**/Temp/*'
'**/Temporary Internet Files/*' # Wine "bottles"
)
EOF
fi
source "$config" || fatal "could not read config file $config"
dest=${dest:-$target}
[[ "$dest" ]] || fatal "backup destination directory not set"
for exclude in "${excludes[@]}"; do
options+=(--exclude="$exclude")
done
if [[ "$mount" ]]; then
if ((sudo_mount)); then sudo=sudo; else sudo=env; fi
$sudo mount "$mount"
fi
cd "$HOME" &&
rdiff-backup "${options[@]}" -- "$HOME" "$dest"