mirror of
https://github.com/yavook/kiwi-backup.git
synced 2024-11-25 00:02:59 +00:00
177 lines
No EOL
4.9 KiB
Bash
Executable file
177 lines
No EOL
4.9 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
#############
|
|
# CONSTANTS #
|
|
#############
|
|
|
|
# commands
|
|
this_exe="$(command -v "${0}")"
|
|
ionice_exe="$(command -v ionice)"
|
|
duplicity_exe="$(command -v duplicity)"
|
|
|
|
# files
|
|
duplicity_secrets_file='/root/duplicity_secrets'
|
|
|
|
#############
|
|
# FUNCTIONS #
|
|
#############
|
|
|
|
append_options() {
|
|
ao_cmdline="${1}"
|
|
ao_options="${2}"
|
|
shift 1
|
|
|
|
if [ -n "${ao_cmdline}" ] && [ -n "${ao_options}" ]; then
|
|
# if both are given, stitch together with a space
|
|
echo "${ao_cmdline} ${ao_options}"
|
|
elif [ -n "${ao_options}" ]; then
|
|
# if only options are given, output them
|
|
echo "${ao_options}"
|
|
else
|
|
# if at most a cmdline is given, output that
|
|
echo "${ao_cmdline}"
|
|
fi
|
|
}
|
|
|
|
print_command() {
|
|
pc_task="${1}"
|
|
shift 1
|
|
|
|
pc_cmdline="${ionice_exe} -c 3 ${duplicity_exe}"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_ALL}" )"
|
|
|
|
case "${pc_task}" in
|
|
backup)
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "--allow-source-mismatch" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "--volsize ${BACKUP_VOLSIZE}" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "--full-if-older-than ${FULL_BACKUP_FREQUENCY}" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_BACKUP}" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "/backup/source" )"
|
|
;;
|
|
|
|
cleanup)
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "cleanup --force" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_CLEAN}" )"
|
|
;;
|
|
|
|
rmfull)
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "remove-older-than ${BACKUP_RETENTION_TIME} --force" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_RMFULL}" )"
|
|
;;
|
|
|
|
rmincr)
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "remove-all-inc-of-but-n-full ${KEEP_NUM_FULL_CHAINS} --force" )"
|
|
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_RMINCR}" )"
|
|
;;
|
|
esac
|
|
|
|
pc_cmdline="${pc_cmdline} ${BACKUP_TARGET}"
|
|
echo "${pc_cmdline}"
|
|
}
|
|
|
|
print_cron_schedule() {
|
|
pcs_min="$( echo "${1}" | awk '{print $1}' )"
|
|
pcs_hour="$( echo "${1}" | awk '{print $2}' )"
|
|
pcs_day="$( echo "${1}" | awk '{print $3}' )"
|
|
pcs_month="$( echo "${1}" | awk '{print $4}' )"
|
|
pcs_weekday="$( echo "${1}" | awk '{print $5}' )"
|
|
pcs_command="${2}"
|
|
shift 2
|
|
|
|
printf '%-8s%-8s%-8s%-8s%-8s%s\n' "${pcs_min}" "${pcs_hour}" "${pcs_day}" "${pcs_month}" "${pcs_weekday}" "${pcs_command}"
|
|
}
|
|
|
|
print_crontab() {
|
|
echo '# crontab generated for kiwi-backup'
|
|
printf '# generation time: '; date
|
|
echo '#'
|
|
|
|
# don't split the '#' from 'min'
|
|
print_cron_schedule '#_min hour day month weekday' 'command' | tr '_' ' '
|
|
|
|
print_cron_schedule "${SCHEDULE_BACKUP}" "${this_exe} backup"
|
|
print_cron_schedule "${SCHEDULE_CLEANUP}" "${this_exe} cleanup"
|
|
print_cron_schedule "${SCHEDULE_RMFULL}" "${this_exe} rmfull"
|
|
print_cron_schedule "${SCHEDULE_RMINCR}" "${this_exe} rmincr"
|
|
}
|
|
|
|
###############
|
|
# ENVIRONMENT #
|
|
###############
|
|
|
|
# load secrets file
|
|
if [ -f "${duplicity_secrets_file}" ]; then
|
|
# shellcheck disable=SC1090
|
|
. "${duplicity_secrets_file}"
|
|
fi
|
|
|
|
# check if uses encryption
|
|
if [ -n "${GPG_KEY_ID}" ]; then
|
|
# gpg key given
|
|
OPTIONS_ALL="$( append_options "${OPTIONS_ALL}" "--encrypt-key=${GPG_KEY_ID}" )"
|
|
|
|
# handle more verbose "GPG_PASSPHRASE" env_var
|
|
PASSPHRASE="${GPG_PASSPHRASE:-${PASSPHRASE}}"
|
|
export PASSPHRASE
|
|
unset GPG_PASSPHRASE
|
|
else
|
|
# no key given
|
|
OPTIONS_ALL="$( append_options "${OPTIONS_ALL}" "--no-encryption" )"
|
|
fi
|
|
|
|
########
|
|
# MAIN #
|
|
########
|
|
|
|
if [ "${#}" -gt 0 ]; then
|
|
|
|
# CLI
|
|
task="${1}"
|
|
shift 1
|
|
|
|
# run task
|
|
case "${task}" in
|
|
print-*)
|
|
task="${task##*-}"
|
|
|
|
case "${task}" in
|
|
# print out the crontab
|
|
crontab)
|
|
print_crontab
|
|
;;
|
|
|
|
# print out a task
|
|
backup|cleanup|rmfull|rmincr)
|
|
print_command "${task}"
|
|
;;
|
|
|
|
# unknown task
|
|
*)
|
|
>&2 echo "Cannot print '${task}'."
|
|
>&2 echo "Choose from: crontab, backup, cleanup, rmfull, rmincr"
|
|
exit 1
|
|
;;
|
|
esac
|
|
;;
|
|
|
|
# execute single task
|
|
backup|cleanup|rmfull|rmincr)
|
|
# shellcheck disable=SC2091
|
|
$(print_command "${task}")
|
|
;;
|
|
|
|
# unknown task
|
|
*)
|
|
>&2 echo "Unknown task '${task}'."
|
|
>&2 echo "Choose from: backup, cleanup, rmfull, rmincr"
|
|
exit 1
|
|
;;
|
|
esac
|
|
else
|
|
|
|
# default run: replace crontab, then start crond
|
|
print_crontab | crontab -
|
|
crond -fl 8
|
|
fi
|
|
|
|
exit 0 |