-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvm-backup
executable file
·206 lines (170 loc) · 7.94 KB
/
lvm-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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
set -eo pipefail
source "${0%/*}/common.sh"
# constant variables - you should not change these unless you know what are you doing
readonly SNAP_SIZE_UNIT="k"
readonly SNAP_VOL_PREFIX="snap"
readonly SNAP_MOUNT_PATH="/mnt"
# fail if required variables are not set
# assign default values for optional variables if not set
setup_variables() {
fail_when_empty "${INCLUDE_VG} ${INCLUDE_LV}" "required variables are not set"
: "${NONINTERACTIVE:=false}"
: "${KEEP_VG_FREE:=15}"
: "${BACKUP_DIR:=$(pwd)}"
: "${BACKUP_COMPRESS:=0}"
: "${BACKUP_FSCK:=true}"
: "${LOW_PRIORITY:=false}"
: "${BACKUP_STATUS:=false}"
}
# print welcome/info message
usage() {
echo -e \
"${RED}${0##*/}\n" \
"${LGRN}-h|--help " "${NRM}Print this message\n" \
"${LGRN}--non-interactive " "${NRM}Skip interactive sanity check " "${GRN}[optional]" "${RED}${NONINTERACTIVE}\n" \
"${LGRN}--config=<file> " "${NRM}Use specific configuration file " "${GRN}[optional]" "${RED}${CFG_FILE}\n" \
"${LGRN}<config-file> " "${BLU}INCLUDE_VG " "${NRM}Include following VGs during LVs lookup (regex) " "${YLW}[required]" "${RED}${INCLUDE_VG}\n" \
"${LGRN}<config-file> " "${BLU}INCLUDE_LV " "${NRM}Include following LVs during snapshot creation phase (regex)" "${YLW}[required]" "${RED}${INCLUDE_LV}\n" \
"${LGRN}<config-file> " "${BLU}KEEP_VG_FREE " "${NRM}Snapshot creation phase should leave following % of VGs free" "${GRN}[optional]" "${RED}${KEEP_VG_FREE}\n" \
"${LGRN}<config-file> " "${BLU}BACKUP_DIR " "${NRM}Backup target directory (initialized Bup repository) " "${GRN}[optional]" "${RED}${BACKUP_DIR}\n" \
"${LGRN}<config-file> " "${BLU}BACKUP_COMPRESS" "${NRM}Backup compression level (0-9) " "${GRN}[optional]" "${RED}${BACKUP_COMPRESS}\n" \
"${LGRN}<config-file> " "${BLU}BACKUP_FSCK " "${NRM}Generate recovery blocks after the backup " "${GRN}[optional]" "${RED}${BACKUP_FSCK}\n" \
"${LGRN}<config-file> " "${BLU}LOW_PRIORITY " "${NRM}Set 'nice' and 'ionice' to lowest priority levels " "${GRN}[optional]" "${RED}${LOW_PRIORITY}\n" \
"${LGRN}<config-file> " "${BLU}BACKUP_STATUS " "${NRM}Print each indexed file with it's status (A, M, D, or space)" "${GRN}[optional]" "${RED}${BACKUP_STATUS}${NRM}"
}
# fail if $BACKUP_DIR is not a valid Git (Bup) repository
validate_git_dir() {
if [[ "$(GIT_DIR=${BACKUP_DIR} git rev-parse >/dev/null 2>&1; echo $?)" != 0 ]]; then
log "'${BACKUP_DIR}' is not a valid Git/Bup repository" "${RED}" >&2
exit 1
fi
}
# fail if user is not root
validate_root_user() {
if [[ "${EUID}" != 0 ]]; then
log "this script requires root privileges" "${RED}" >&2
exit 1
fi
}
# list all volume groups that match $INCLUDE_VG (regex)
list_volume_groups() {
vgs --readonly --noheadings -o vg_name --select "vg_name =~ "${INCLUDE_VG}"" | tr -d " "
}
# list all logical volumes for selected volume group $1 that match $INCLUDE_LV (regex)
list_logical_vols() {
lvs --readonly --noheadings -o lv_name --select "vg_name = "${1}" && lv_name =~ "${INCLUDE_LV}"" | tr -d " "
}
# calculate size of snapshot volume considering free space on volume group $1, amount of volumes $2 and $KEEP_VG_FREE limit
get_snapshot_vol_size() {
local -r free="$(vgs --readonly --noheadings --units "${SNAP_SIZE_UNIT}" -o vg_free --select "vg_name = "${1}"" | grep -Eo '[0-9]+' | head -1)"
if (( "${free}" > 0 )); then
echo "$(( (((100 - ${KEEP_VG_FREE}) * ${free}) / 100) / ${2} ))${SNAP_SIZE_UNIT}"
else
log "volume group '${1}' has no free space left (${free}${SNAP_SIZE_UNIT})" "${RED}" >&2
exit 1
fi
}
# create snapshot of $1 volume with $2 name and $3 size
create_lv_snapshot() {
log "creating snapshot volume '${2}' of '${1}' with size '${3}'" "${MGT}"
lvcreate --yes --snapshot --name "${2}" --size "${3}" "${1}"
}
# translate boolean variables to execution parameters for eval
is_low_priority() { [[ "${LOW_PRIORITY}" = true ]] && LOW_PRIORITY="nice -n19 ionice -c2 -n7" || unset LOW_PRIORITY; }
is_backup_status() { [[ "${BACKUP_STATUS}" = true ]] && BACKUP_STATUS="--status" || unset BACKUP_STATUS; }
# transform $1 'vg_name/$SNAP_VOL_PREFIX_original_lv_name' to backup name 'hostname-original_lv_name'
get_backup_name() { echo "${HOST:-$(command -v hostnamectl >/dev/null 2>&1 && hostnamectl hostname || hostname)}-$(awk -F'_' '{ print $2 }' <<< "${1}")"; }
# replace '/' with '-' in $1 'vg_name/$SNAP_VOL_PREFIX_original_lv_name'
get_mount_path() { sed 's/\//-/' <<< "${1}"; }
# mount $1 'vg_name/snapshot_lv_name' to $2 location
mount_wrapper() {
log "mounting '/dev/${1}' to '${2}'" "${YLW}"
mkdir -pv "${2}"
mount -rv "/dev/${1}" "${2}"
}
# get list of '$1/path' to exclude from backup formatted as a list of '--exclude-rx=pattern' bup index parameters
get_exclusion_params() {
local args=() i=0
for dir in ${EXCLUDE_DIRS}; do
args[$i]="--exclude-rx=\"^${1}/${dir}\""
((++i))
done
echo "${args[@]}"
}
# index $1 location using Bup
bup_index() {
log "indexing '${1}'" "${CYA}"
eval "BUP_DIR=${BACKUP_DIR}" "${LOW_PRIORITY}" \
bup index --update --one-file-system --no-check-device "$(get_exclusion_params "${1}")" "${BACKUP_STATUS}" "${1}"
}
# convert $1 time in unix timestamp (seconds) to "Y-m-d T" format
unix_to_human_time() { date -d @${1} "+%Y-%m-%d %T"; }
# backup $1 location with $2 name and $3 unix timestamp (seconds)
bup_save() {
log "backing up '${1}' with name '${2}' and timestamp '$(unix_to_human_time "${3}")'" "${GRN}"
eval "BUP_DIR=${BACKUP_DIR}" "${LOW_PRIORITY}" \
bup save --strip "${1}" --name="${2}" --date="${3}" --compress="${BACKUP_COMPRESS}" "${1}"
}
# unmount file system mounted at $1 location
unmount_wrapper() {
log "unmounting '${1}'" "${YLW}"
sync
umount -v "${1}"
rmdir -v "${1}"
}
# remove $1 logical volume
remove_lv() {
log "removing logical volume '${1}'" "${MGT}"
lvremove --yes "${1}"
}
# generate Bup backup recovery blocks
bup_fsck() {
log "[phase 3] generate backup recovery blocks" "${RED}"
eval "BUP_DIR=${BACKUP_DIR}" "${LOW_PRIORITY}" \
bup fsck --generate --quick
}
# get human readable size of $1 directory
get_dir_size() { du -sh "${1}" | awk '{ print $1 }'; }
# main flow
parse_arguments "${@}"
load_config "${CFG_FILE}" "lvm-backup.cfg"
setup_variables
usage
ensure_deps "lvm git bup par2"
validate_git_dir
validate_root_user
sanity_check
readonly vol_groups="$(list_volume_groups)"
fail_when_empty "${vol_groups}" "no VGs found matching '${INCLUDE_VG}'"
log "[phase 1] create snapshot volumes" "${RED}"
created_snap_vols=""
for vg in ${vol_groups}; do
logical_vols="$(list_logical_vols "${vg}")"
fail_when_empty "${logical_vols}" "no LVs found matching '${INCLUDE_LV}'"
logical_vols_num="$(wc -l <<< "${logical_vols}")"
snap_vol_size="$(get_snapshot_vol_size "${vg}" "${logical_vols_num}")"
log "volume group: ${vg} | logical volumes: ${logical_vols_num}" "${BLU}"
for lv in ${logical_vols}; do
snap_vol_name="${SNAP_VOL_PREFIX}_${lv}"
created_snap_vols+=" ${vg}/${snap_vol_name}"
create_lv_snapshot "${vg}/${lv}" "${snap_vol_name}" "${snap_vol_size}"
done
done
# all backups should have the same time (as close as possible to snapshot creation time)
readonly timestamp="$(date +%s)"
is_low_priority
is_backup_status
log "[phase 2] mount, backup, umount and remove snapshot volumes" "${RED}"
for lv in ${created_snap_vols}; do
mount_path="${SNAP_MOUNT_PATH}/$(get_mount_path "${lv}")"
mount_wrapper "${lv}" "${mount_path}"
bup_index "${mount_path}"
bup_save "${mount_path}" "$(get_backup_name "${lv}")" "${timestamp}"
unmount_wrapper "${mount_path}"
remove_lv "${lv}"
done
[[ "${BACKUP_FSCK}" = true ]] && bup_fsck
log "[success] backup finished" "${RED}"
log "took: $(print_elapsed_time "${timestamp}")" "${BLU}"
log "backup target '${BACKUP_DIR}' size: $(get_dir_size "${BACKUP_DIR}")" "${BLU}"