1
0
Fork 0
mirror of https://github.com/yavook/kiwi-backup.git synced 2024-11-22 06:53:00 +00:00

run.sh: quoting bugs, args, func prefixes, readability

This commit is contained in:
Jörn-Michael Miehe 2020-08-28 11:37:27 +02:00
parent 8ee3939990
commit 9ad0b72bad

121
run.sh
View file

@ -4,10 +4,12 @@
# CONSTANTS # # CONSTANTS #
############# #############
# commands
env_exe="$(command -v env)" env_exe="$(command -v env)"
ionice_exe="$(command -v ionice)" ionice_exe="$(command -v ionice)"
duplicity_exe="$(command -v duplicity)" duplicity_exe="$(command -v duplicity)"
# check if uses encryption
if [ -n "${GPG_KEY_ID}" ]; then if [ -n "${GPG_KEY_ID}" ]; then
# gpg key given # gpg key given
env_changes="PASSPHRASE='${GPG_PASSPHRASE}'" env_changes="PASSPHRASE='${GPG_PASSPHRASE}'"
@ -22,91 +24,122 @@ fi
# FUNCTIONS # # FUNCTIONS #
############# #############
trim_options() { append_options() {
# if args are given, trim whitespace, then add a space in front ao_cmdline="${1}"
if [ -n "${1}" ]; then ao_options="${2}"
echo " $( echo "${1}" | xargs )" shift 1
# remove leading whitespace characters
ao_options="${ao_options#"${ao_options%%[![:space:]]*}"}"
# remove trailing whitespace characters
ao_options="${ao_options%"${ao_options##*[![:space:]]}"}"
# if options are given, stitch together with a space
if [ -n "${ao_options}" ]; then
echo "${ao_cmdline} ${ao_options}"
else
echo "${ao_cmdline}"
fi fi
} }
print_command() { print_command() {
task="${1}" pc_task="${1}"
shift 1 shift 1
# if environment should be changed, call with "env"
if [ -n "${env_changes}" ]; then if [ -n "${env_changes}" ]; then
# should change environment pc_cmdline="${env_exe} ${env_changes} "
cmdline="${env_exe} ${env_changes} "
else else
cmdline="" pc_cmdline=""
fi fi
cmdline="${cmdline}${ionice_exe} -c 3 ${duplicity_exe} ${encrypt_opts}" pc_cmdline="${pc_cmdline}${ionice_exe} -c 3 ${duplicity_exe} ${encrypt_opts}"
case "${task}" in case "${pc_task}" in
backup) backup)
cmdline="${cmdline} --allow-source-mismatch --volsize '${BACKUP_VOLSIZE}' --full-if-older-than '${FULL_BACKUP_FREQUENCY}'" pc_cmdline="${pc_cmdline} --allow-source-mismatch --volsize ${BACKUP_VOLSIZE} --full-if-older-than ${FULL_BACKUP_FREQUENCY}"
cmdline="${cmdline}$( trim_options "${OPTIONS_BACKUP}" )" pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_BACKUP} /backup/source" )"
cmdline="${cmdline} /backup/source"
;; ;;
cleanup) cleanup)
cmdline="${cmdline} cleanup --force" pc_cmdline="${pc_cmdline} cleanup --force"
cmdline="${cmdline}$( trim_options "${OPTIONS_CLEAN}" )" pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_CLEAN}" )"
;; ;;
rmfull) rmfull)
cmdline="${cmdline} remove-older-than '${BACKUP_RETENTION_TIME}' --force" pc_cmdline="${pc_cmdline} remove-older-than ${BACKUP_RETENTION_TIME} --force"
cmdline="${cmdline}$( trim_options "${OPTIONS_RMFULL}" )" pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_RMFULL}" )"
;; ;;
rmincr) rmincr)
cmdline="${cmdline} remove-all-inc-of-but-n-full '${KEEP_NUM_FULL_CHAINS}' --force" pc_cmdline="${pc_cmdline} remove-all-inc-of-but-n-full ${KEEP_NUM_FULL_CHAINS} --force"
cmdline="${cmdline}$( trim_options "${OPTIONS_RMINCR}" )" pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_RMINCR}" )"
;; ;;
esac esac
cmdline="${cmdline} '${BACKUP_TARGET}'" pc_cmdline="${pc_cmdline} ${BACKUP_TARGET}"
echo "${cmdline}" echo "${pc_cmdline}"
} }
print_cron_schedule() { print_cron_schedule() {
min="$( echo "${1}" | cut -d' ' -f1 )" pcs_min="$( echo "${1}" | awk '{print $1}' )"
hour="$( echo "${1}" | cut -d' ' -f2 )" pcs_hour="$( echo "${1}" | awk '{print $2}' )"
day="$( echo "${1}" | cut -d' ' -f3 )" pcs_day="$( echo "${1}" | awk '{print $3}' )"
month="$( echo "${1}" | cut -d' ' -f4 )" pcs_month="$( echo "${1}" | awk '{print $4}' )"
weekday="$( echo "${1}" | cut -d' ' -f5 )" pcs_weekday="$( echo "${1}" | awk '{print $5}' )"
command="${2}" pcs_command="${2}"
shift 2
printf '%-8s%-8s%-8s%-8s%-8s%s' "${min}" "${hour}" "${day}" "${month}" "${weekday}" "${command}" printf '%-8s%-8s%-8s%-8s%-8s%s\n' "${pcs_min}" "${pcs_hour}" "${pcs_day}" "${pcs_month}" "${pcs_weekday}" "${pcs_command}"
}
print_cron_header() {
# don't split the '#' from 'min'
print_cron_schedule '#_min hour day month weekday' 'command' | tr '_' ' '
} }
print_crontab() { print_crontab() {
echo '# crontab generated for kiwi-backup' echo '# crontab generated for kiwi-backup'
printf '# generation time: '; date printf '# generation time: '; date
echo '#' echo '#'
echo "$( print_cron_header )"
echo "$( print_cron_schedule "${SCHEDULE_BACKUP}" "$( print_command backup )" )" # don't split the '#' from 'min'
echo "$( print_cron_schedule "${SCHEDULE_CLEANUP}" "$( print_command cleanup )" )" print_cron_schedule '#_min hour day month weekday' 'command' | tr '_' ' '
echo "$( print_cron_schedule "${SCHEDULE_RMFULL}" "$( print_command rmfull )" )"
echo "$( print_cron_schedule "${SCHEDULE_RMINCR}" "$( print_command rmincr )" )" print_cron_schedule "${SCHEDULE_BACKUP}" "$( print_command backup )"
print_cron_schedule "${SCHEDULE_CLEANUP}" "$( print_command cleanup )"
print_cron_schedule "${SCHEDULE_RMFULL}" "$( print_command rmfull )"
print_cron_schedule "${SCHEDULE_RMINCR}" "$( print_command rmincr )"
} }
######## ########
# MAIN # # MAIN #
######## ########
if [ "${1}" = '-n' ]; then
# dry-run
print_crontab
exit 0
fi
# replace crontab, start crond if [ "${#}" -gt 0 ]; then
# run a command
case "${1}" in
print-crontab)
print_crontab
;;
print-backup|print-cleanup|print-rmfull|print-rmincr)
print_command "${1##*-}"
;;
# execute single command
backup|cleanup|rmfull|rmincr)
print_command "${1}"
cmd="$(print_command "${1}")"
${cmd}
;;
*)
>&2 echo "Unknown command '${1}'."
exit 1
;;
esac
else
# default run: replace crontab, then start crond
print_crontab | crontab - print_crontab | crontab -
crond -fl 8 crond -fl 8
fi
exit 0 exit 0