Skip to content

Commit e4c712d

Browse files
authored
feat(backup): add support for multiple compression methods in backup (#4795)
* feat(backup): add support for multiple compression methods in backup process * fix(backup): update backup file extensions to support multiple compression formats
1 parent 04f1654 commit e4c712d

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

lgsm/modules/command_backup.sh

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ fn_firstcommand_set
1313
# Trap to remove lockfile on quit.
1414
fn_backup_trap() {
1515
echo -e ""
16-
echo -en "backup ${backupname}.tar.gz..."
16+
echo -en "backup ${backupname}.${compressext}..."
1717
fn_print_canceled_eol_nl
18-
fn_script_log_info "Backup ${backupname}.tar.gz: CANCELED"
19-
rm -f "${backupdir:?}/${backupname}.tar.gz" | tee -a "${lgsmlog}"
20-
echo -en "backup ${backupname}.tar.gz..."
18+
fn_script_log_info "Backup ${backupname}.${compressext}: CANCELED"
19+
rm -f "${backupdir:?}/${backupname}.${compressext}" | tee -a "${lgsmlog}"
20+
echo -en "backup ${backupname}.${compressext}..."
2121
fn_print_removed_eol_nl
22-
fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED"
22+
fn_script_log_info "Backup ${backupname}.${compressext}: REMOVED"
2323
# Remove backup lockfile.
2424
rm -f "${lockdir:?}/backup.lock"
2525
fn_backup_start_server
@@ -110,23 +110,47 @@ fn_backup_create_lockfile() {
110110
trap fn_backup_trap INT
111111
}
112112

113+
fn_select_compression() {
114+
if command -v zstd > /dev/null 2>&1; then
115+
compressprog="zstd"
116+
compressext="tar.zst"
117+
compressflag="--zstd"
118+
elif command -v pigz > /dev/null 2>&1; then
119+
compressprog="pigz"
120+
compressext="tar.gz"
121+
compressflag="--use-compress-program=pigz"
122+
elif command -v gzip > /dev/null 2>&1; then
123+
compressprog="gzip"
124+
compressext="tar.gz"
125+
compressflag="--gzip"
126+
else
127+
compressprog=""
128+
compressext="tar"
129+
compressflag=""
130+
fi
131+
}
132+
113133
# Compressing files.
114134
fn_backup_compression() {
115-
# Tells how much will be compressed using rootdirduexbackup value from info_distro and prompt for continue.
135+
116136
fn_print_info "A total of ${rootdirduexbackup} will be compressed."
117-
fn_script_log_info "A total of ${rootdirduexbackup} will be compressed: ${backupdir}/${backupname}.tar.gz"
118-
fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.tar.gz, in progress ..."
119-
fn_script_log_info "Backup ${rootdirduexbackup} ${backupname}.tar.gz, in progress"
137+
fn_script_log_info "A total of ${rootdirduexbackup} will be compressed: ${backupdir}/${backupname}.${compressext}"
138+
fn_print_dots "Backup (${rootdirduexbackup}) ${backupname}.${compressext}, in progress ..."
139+
fn_script_log_info "Backup ${rootdirduexbackup} ${backupname}.${compressext}, in progress"
120140
excludedir=$(fn_backup_relpath)
121141

122-
# Check that excludedir is a valid path.
123142
if [ ! -d "${excludedir}" ]; then
124143
fn_print_fail_nl "Problem identifying the previous backup directory for exclusion."
125144
fn_script_log_fail "Problem identifying the previous backup directory for exclusion"
126145
core_exit.sh
127146
fi
128147

129-
tar --use-compress-program=pigz -hcf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" --exclude "${tmpdir}" ./.
148+
if [ -n "${compressflag}" ]; then
149+
tar ${compressflag} -hcf "${backupdir}/${backupname}.${compressext}" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" --exclude "${tmpdir}" ./.
150+
else
151+
tar -hcf "${backupdir}/${backupname}.${compressext}" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" --exclude "${tmpdir}" ./.
152+
fi
153+
130154
exitcode=$?
131155
if [ "${exitcode}" -ne 0 ]; then
132156
fn_print_fail_eol
@@ -136,8 +160,8 @@ fn_backup_compression() {
136160
fn_script_log_fail "Starting backup"
137161
else
138162
fn_print_ok_eol
139-
fn_print_ok_nl "Completed: ${italic}${backupname}.tar.gz${default}, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
140-
fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
163+
fn_print_ok "Completed: ${italic}${backupname}.${compressext}${default}, total size $(du -sh "${backupdir}/${backupname}.${compressext}" | awk '{print $1}')"
164+
fn_script_log_pass "Backup created: ${backupname}.${compressext}, total size $(du -sh "${backupdir}/${backupname}.${compressext}" | awk '{print $1}')"
141165
alert="backup"
142166
alert.sh
143167
fi
@@ -152,7 +176,7 @@ fn_backup_prune() {
152176
# How many backups exceed maxbackups.
153177
backupquotadiff=$((backupcount - maxbackups))
154178
# How many backups exceed maxbackupdays.
155-
backupsoudatedcount=$(find "${backupdir}"/ -type f -name "*.tar.gz" -mtime +"${maxbackupdays}" | wc -l)
179+
backupsoudatedcount=$(find "${backupdir}"/ -type f -name "*.tar.*" -mtime +"${maxbackupdays}" | wc -l)
156180
# If anything can be cleared.
157181
if [ "${backupquotadiff}" -gt "0" ] || [ "${backupsoudatedcount}" -gt "0" ]; then
158182
fn_print_dots "Pruning"
@@ -167,7 +191,7 @@ fn_backup_prune() {
167191
fn_print_dots "Pruning: Clearing ${backupquotadiff} backup(s)"
168192
fn_script_log_info "Pruning: Clearing ${backupquotadiff} backup(s)"
169193
# Clear backups over quota.
170-
find "${backupdir}"/ -type f -name "*.tar.gz" -printf '%T@ %p\n' | sort -rn | tail -${backupquotadiff} | cut -f2- -d" " | xargs rm
194+
find "${backupdir}"/ -type f -name "*.tar.*" -printf '%T@ %p\n' | sort -rn | tail -${backupquotadiff} | cut -f2- -d" " | xargs rm
171195
fn_print_ok_nl "Pruning: Clearing ${backupquotadiff} backup(s)"
172196
fn_script_log_pass "Pruning: Cleared ${backupquotadiff} backup(s)"
173197
# If maxbackupdays is used over maxbackups.
@@ -190,15 +214,15 @@ fn_backup_prune() {
190214
fn_backup_relpath() {
191215
# Written by CedarLUG as a "realpath --relative-to" alternative in bash.
192216
# Populate an array of tokens initialized from the rootdir components.
193-
declare -a rdirtoks=($(readlink -f "${rootdir}" | sed "s/\// /g"))
217+
mapfile -t rdirtoks < <(readlink -f "${rootdir}" | sed "s/\//\n/g")
194218
if [ ${#rdirtoks[@]} -eq 0 ]; then
195219
fn_print_fail_nl "Problem assessing rootdir during relative path assessment"
196220
fn_script_log_fail "Problem assessing rootdir during relative path assessment: ${rootdir}"
197221
core_exit.sh
198222
fi
199223

200224
# Populate an array of tokens initialized from the backupdir components.
201-
declare -a bdirtoks=($(readlink -f "${backupdir}" | sed "s/\// /g"))
225+
mapfile -t bdirtoks < <(readlink -f "${backupdir}" | sed "s/\//\n/g")
202226
if [ ${#bdirtoks[@]} -eq 0 ]; then
203227
fn_print_fail_nl "Problem assessing backupdir during relative path assessment"
204228
fn_script_log_fail "Problem assessing backupdir during relative path assessment: ${rootdir}"
@@ -243,7 +267,7 @@ fn_backup_start_server() {
243267
fn_print_dots ""
244268
check.sh
245269
core_logs.sh
246-
270+
fn_select_compression
247271
fn_backup_check_lockfile
248272
fn_backup_init
249273
fn_backup_stop_server

lgsm/modules/info_distro.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ if [ -d "${backupdir}" ]; then
252252
backupcount=0
253253

254254
# If there are backups in backup dir.
255-
if [ "$(find "${backupdir}" -name "*.tar.gz" | wc -l)" -ne "0" ]; then
255+
if [ "$(find "${backupdir}" -name "*.tar.*" | wc -l)" -ne "0" ]; then
256256
# number of backups.
257-
backupcount="$(find "${backupdir}"/*.tar.gz | wc -l)" # integer
257+
backupcount="$(find "${backupdir}"/*.tar.* | wc -l)" # integer
258258
# most recent backup.
259-
lastbackup="$(ls -1t "${backupdir}"/*.tar.gz | head -1)" # string
259+
lastbackup="$(ls -1t "${backupdir}"/*.tar.* | head -1)" # string
260260
# date of most recent backup.
261261
lastbackupdate="$(date -r "${lastbackup}")" # string
262262
# no of days since last backup.

0 commit comments

Comments
 (0)