-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync-snapshot
executable file
·174 lines (148 loc) · 3.79 KB
/
rsync-snapshot
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/dash
user_id="$(\id -u)" || exit $?
[ "$user_id" != 0 ] && \
printf '%s\n' "$0: Permission denied." 'Need root access.' && \
exit 2
backup_dir='/root/backup'
target_snapshot='/'
exclude_file='./exclude.list'
device="${dev:-nvme0n1p3}"
[ ! -z "$dry_run" ] && dry_run='--dry-run'
[ ! -z "$1" ] && {
echo "Mounting /dev/$device at $backup_dir"
mount --mkdir /dev/$device $backup_dir || exit $?
}
list_snapshot="$(\find $backup_dir/ -mindepth 1 -maxdepth 1 -type d | sort -nr)" || exit "$?"
[ ! -z "$list_snapshot" ] && \
prev_snapshot="$(printf '%s\n' "$list_snapshot" | head -1)/snapshot/"
current_date="$(date +'%F_%H-%M-%S')"
unmount_dev() {
echo "Unmounting /dev/$device"
umount -l /dev/$device
}
rsync_cmd() {
rsync \
-a \
--delete \
--numeric-ids \
--exclude-from="$exclude_file" \
--sparse \
--info=progress2,stats2 \
$dry_run \
$* || {
local rsync_exit_code="$?"
unmount_dev
exit $rsync_exit_code
}
unmount_dev
}
backup() {
# so that we can create the log file.
# seems like `--mkpath` comes after creating log file.
[ -z "$dry_run" ] && \
\mkdir -p "$backup_dir/$current_date" && \
log_file="--log-file=$backup_dir/$current_date/changes.log"
if [ ! -z "$prev_snapshot" ]; then
echo "Hard link $prev_snapshot"
rsync_cmd \
--link-dest="$prev_snapshot" \
--mkpath $log_file \
"$target_snapshot" "$backup_dir/$current_date/snapshot/"
else
rsync_cmd \
--mkpath $log_file \
"$target_snapshot" "$backup_dir/$current_date/snapshot/"
fi
}
restore() {
if [ ! -z "$1" ]; then
local grep_snapshot
grep_snapshot="$(printf '%s\n' "$list_snapshot" | \grep $1)" || {
local exit_code="$?"
[ "$exit_code" = 1 ] || exit "$exit_code"
}
[ ! -z "$grep_snapshot" ] && selected_snapshot="$grep_snapshot/snapshot/"
else
selected_snapshot="$prev_snapshot"
fi
# after update the system,
# we need to reexecute systemd manager after restore from snapshot before
# update.
# also, check whether we are in chroot environment or not before reexecute
# or reload systemd.
# reference on arch linux:
# - /usr/share/libalpm/scripts/systemd-hook
# - /usr/bin/checkservices
if [ ! -z "$selected_snapshot" ]; then
echo "Restoring from $selected_snapshot"
rsync_cmd "$selected_snapshot" "$target_snapshot"
if $(stat -c %d:%i / /proc/1/root/ | awk -v RS= '{exit $1 != $2}') && \
[ -d /run/systemd/system ] && \
command -v systemctl >/dev/null && \
[ -z "$dry_run" ]; then
if grep -qF '(deleted)' /proc/1/maps; then
echo 'Restart systemd system...'
systemctl --system daemon-reexec || exit $?
else
echo 'Reload systemd system...'
systemctl --system daemon-reload || exit $?
fi
else
echo 'Skipping systemd restart/reload'
exit
fi
else
echo 'There is no snapshot, nothing to restore.'
unmount_dev
exit 1
fi
}
ls() {
[ ! -z "$list_snapshot" ] && {
printf '%s\n' "$list_snapshot" | sed --expression='s/[a-z/]//g'
}
unmount_dev
}
remove() {
{
{
[ ! -z "$1" ] || {
echo 'Please specify snapshot to remove.'
unmount_dev
exit 1
}
} && {
[ -d "$backup_dir/$1" ] || {
echo "Snapshot $1 not found."
unmount_dev
exit 1
}
}
} && {
echo "Removing snapshot $backup_dir/$1"
\rm -r "$backup_dir/$1"
echo "Removed"
unmount_dev
}
}
case "$1" in
backup)
backup
;;
restore)
restore "$2"
;;
ls)
ls
;;
rm)
remove "$2"
;;
*)
printf "Usage:
- [dry_run=1] [dev=sda1, check lsblk] $0 backup
- [dry_run=1] [dev=sda1, check lsblk] $0 restore [year-month-date_hour-minute-second]
- $0 ls
- $0 rm year-month-date_hour-minute-second\n"
;;
esac