-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup
executable file
·157 lines (137 loc) · 3.85 KB
/
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
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# SPDX-FileCopyrightText: 2015 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command grep rsync && exit 3
#
# Functions
#
# Check if path for backup1 exists.
function backup_exists() {
if ! [[ -v backup1 ]]; then
show_error "backup1 variable is unset."
exit 1
fi
if ! [ -d "${backup1}" ]; then
show_error "Backup destination ${backup1@Q} does not exist."
exit 1
fi
}
# Backup home directory to Backups 1 and 2
function sync_home() {
local cmd
local excludedir
cmd="rsync -Pradog --delete-excluded ${HOME}/ ${backup1}/${USER^}/"
cmd="${cmd} ${vboxdir:+--exclude ${vboxdir#"${HOME}"/}\\*}"
cmd="${cmd} ${kvmdir:+--exclude ${kvmdir#"${HOME}"/}\\*}"
for excludedir in "${EXCLUDEDIRS[@]}"; do
cmd="${cmd} --exclude ${excludedir#"${HOME}"/}\\*"
done
show_info "Syncing ${HOME@Q} to ${backup1@Q}."
mkdir -p "${backup1}/${USER^}/"
bash -c "${cmd}"
sync
show_success "Done." && echo
if [[ -v backup2 ]] && [ -d "${backup2}" ]; then
show_info "Syncing ${backup1@Q} to ${backup2@Q}."
sudo rsync -Pradog --delete-excluded \
"${backup1}/" "${backup2}/" \
--exclude ".Trash-1000/*"
sync
show_success "Done." && echo
fi
}
# Backup VMS to Backups 1 and 2
function sync_vms() {
show_info "Syncing VMs in ${HOME@Q} to ${backup1@Q}."
local cmd
if [[ -n "${kvmdir}" ]] && [[ -d "${kvmdir}" ]]; then
mkdir -p "${backup1}/Images/KVM"
cmd="${cmd:+${cmd} && }rsync -Pradog --sparse ${kvmdir}/ ${backup1}/Images/KVM/"
fi
if [[ -n "${vboxdir}" ]] && [[ -d "${vboxdir}" ]]; then
mkdir -p "${backup1}/Images/VirtualBox"
cmd="${cmd:+${cmd} && }rsync -Pradog ${vboxdir}/ ${backup1}/Images/VirtualBox/"
fi
if [ -n "${cmd}" ]; then
sudo bash -c "${cmd}"
fi
sync
show_success "Done." && echo
if [ -d "${backup2}" ]; then
show_info "Syncing VMs in ${backup1@Q} to ${backup2@Q}."
cmd=
if [[ -d "${backup1}/Images/KVM" ]]; then
mkdir -p "${backup2}/Images/KVM"
cmd="${cmd:+${cmd} && }rsync -Pradog --sparse ${backup1}/Images/KVM/ ${backup2}/Images/KVM/"
fi
if [[ -d "${backup1}/Images/VirtualBox" ]]; then
mkdir -p "${backup2}/Images/VirtualBox"
cmd="${cmd:+${cmd} && }rsync -Pradog ${backup1}/Images/VirtualBox/ ${backup2}/Images/VirtualBox/"
fi
if [ -n "${cmd}" ]; then
sudo bash -c "${cmd}"
fi
sync
show_success "Done." && echo
fi
}
#
# Main
#
backup_exists
if grep -q "${HOME}" /etc/mtab; then
show_error "ERROR: Home directories found in /etc/mtab. Unmount and try again."
exit 1
fi
SYNC_HOME=false
SYNC_VM=false
EXCLUDEDIRS=()
OPTIONS=ahv
LONGOPTIONS=all,home,vms
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case ${1} in
-a | --all)
SYNC_HOME=true
SYNC_VM=true
shift
break
;;
-h | --home)
SYNC_HOME=true
shift
;;
-v | --vms)
SYNC_VM=true
shift
;;
--)
shift
break
;;
*)
show_error "ERROR: Invalid flag ${1@Q}."
exit 3
;;
esac
done
${SYNC_HOME} && sync_home
${SYNC_VM} && sync_vms