-
Notifications
You must be signed in to change notification settings - Fork 12
/
backup-system
executable file
·111 lines (92 loc) · 2.89 KB
/
backup-system
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
#!/bin/bash
#
# backup-system: Full system backup using rdiff-backup
#
# Copyright (C) 2022 Rodrigo Silva (MestreLion) <[email protected]>
# License: GPLv3 or later, at your choice. See <http://www.gnu.org/licenses/gpl>
#
# Scheduling:
# echo '@weekly root /path/to/this 2>> /var/log/backup-system.stderr.log' |
# sudo tee /etc/cron.d/backup-system
#
# Alternatives:
# Simple, single-dir backup:
# source=/media/"$USER"/a
# target=/media/"$USER"/b
# rsync -a --info=progress2 --no-inc-recursive "${source%\/}"/ "${target%\/}"/
# tar backup:
# cd "$source" && sudo tar --one-file-system -cvpzf "$target"/backup.tar.xz
# Mirror:
# sudo rsync -axS --info=progress2 --no-inc-recursive --delete "${source%\/}"/ "${target%\/}"/
# ------------------------------------------------------------------------------
system=$(lsb_release -sir | tr -d '.\n')
myself=${0##*/}
source=/
target=/backup/${system,,}
options=(
--verbosity 5 # default is 3, completely silent in a normal run
--terminal-verbosity 3 # default is --verbosity level
--include-symbolic-links # would be excluded by --exclude-special-files
--exclude-special-files # Exclude all device files, fifo files, socket files, and symbolic links.
--exclude-other-filesystems
)
excludes=(
'/dev/*'
'/lost+found'
'/media/*' # not really needed with --exclude-other-filesystems
'/proc/*'
'/run/*'
'/sys/*'
'/tmp/*'
'/swapfile'
# Users' homes
'/home/*/.local/share/Trash/*'
'/home/*/.cache/*'
'/home/*/.thumbnails/*' # Not in Ubuntu 22.04+
)
# to restore /swapfile:
# sudo fallocate -l 1G /swapfile
# sudo chmod 600 /swapfile
# sudo mkswap /swapfile
# sudo swapon /swapfile
# ------------------------------------------------------------------------------
is_root() { [[ "$(id -u)" -eq 0 ]]; }
fatal() { [[ "${1:-}" ]] && echo "$myself: error: $@" >&2 ; exit 1; }
exists() { type "$@" >/dev/null 2>&1; }
usage() {
if [[ "${1:-}" ]] ; then exec >&2; fi
cat <<-USAGE
Usage: ${myself} [SOURCE] [TARGET] [extra-rdiff-options]
USAGE
if [[ "${1:-}" ]] ; then
cat <<- USAGE
Try '$myself --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
Backup the root filesystem using rdiff-backup
Excludes mounts and other filesystems. Must run as root.
SOURCE defaults to: ${source}
TARGET defaults to: ${target}
Default excludes:
${excludes[@]}
Copyright (C) 2022 Rodrigo Silva (MestreLion) <[email protected]>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage; done
source=${1:-/}
target=${2:-/backup/${system,,}}
shift 2 || true
# ------------------------------------------------------------------------------
if ! is_root; then
echo "Must run as root" >&2
exit 1
fi
options+=( "$@" )
for exclude in "${excludes[@]}"; do
options+=(--exclude="$exclude")
done
rdiff-backup "${options[@]}" -- "$source" "$target"