2020-08-26 23:30:14 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
#############
|
|
|
|
# CONSTANTS #
|
|
|
|
#############
|
|
|
|
|
|
|
|
env_exe="$(command -v env)"
|
|
|
|
ionice_exe="$(command -v ionice)"
|
|
|
|
duplicity_exe="$(command -v duplicity)"
|
|
|
|
|
|
|
|
if [ -n "${GPG_KEY_ID}" ]; then
|
|
|
|
# gpg key given
|
|
|
|
env_changes="PASSPHRASE='${GPG_PASSPHRASE}'"
|
|
|
|
encrypt_opts="--encrypt-key='${GPG_KEY_ID}'"
|
|
|
|
else
|
|
|
|
# no key given
|
|
|
|
env_changes=""
|
|
|
|
encrypt_opts="--no-encryption"
|
|
|
|
fi
|
|
|
|
|
|
|
|
#############
|
|
|
|
# FUNCTIONS #
|
|
|
|
#############
|
|
|
|
|
|
|
|
trim_options() {
|
|
|
|
# if args are given, trim whitespace, then add a space in front
|
|
|
|
if [ -n "${1}" ]; then
|
|
|
|
echo " $( echo "${1}" | xargs )"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
print_command() {
|
2020-08-26 23:30:14 +00:00
|
|
|
task="${1}"
|
|
|
|
shift 1
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
if [ -n "${env_changes}" ]; then
|
|
|
|
# should change environment
|
|
|
|
cmdline="${env_exe} ${env_changes} "
|
|
|
|
else
|
|
|
|
cmdline=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
cmdline="${cmdline}${ionice_exe} -c 3 ${duplicity_exe} ${encrypt_opts}"
|
2020-08-26 23:30:14 +00:00
|
|
|
|
|
|
|
case "${task}" in
|
|
|
|
backup)
|
2020-08-27 13:57:36 +00:00
|
|
|
cmdline="${cmdline} --allow-source-mismatch --volsize '${BACKUP_VOLSIZE}' --full-if-older-than '${FULL_BACKUP_FREQUENCY}'"
|
|
|
|
cmdline="${cmdline}$( trim_options "${OPTIONS_BACKUP}" )"
|
|
|
|
cmdline="${cmdline} /backup/source"
|
2020-08-26 23:30:14 +00:00
|
|
|
;;
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
cleanup)
|
2020-08-26 23:30:14 +00:00
|
|
|
cmdline="${cmdline} cleanup --force"
|
2020-08-27 13:57:36 +00:00
|
|
|
cmdline="${cmdline}$( trim_options "${OPTIONS_CLEAN}" )"
|
2020-08-26 23:30:14 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
rmfull)
|
2020-08-27 13:57:36 +00:00
|
|
|
cmdline="${cmdline} remove-older-than '${BACKUP_RETENTION_TIME}' --force"
|
|
|
|
cmdline="${cmdline}$( trim_options "${OPTIONS_RMFULL}" )"
|
2020-08-26 23:30:14 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
rmincr)
|
2020-08-27 13:57:36 +00:00
|
|
|
cmdline="${cmdline} remove-all-inc-of-but-n-full '${KEEP_NUM_FULL_CHAINS}' --force"
|
|
|
|
cmdline="${cmdline}$( trim_options "${OPTIONS_RMINCR}" )"
|
2020-08-26 23:30:14 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
cmdline="${cmdline} '${BACKUP_TARGET}'"
|
2020-08-26 23:30:14 +00:00
|
|
|
echo "${cmdline}"
|
|
|
|
}
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
print_cron_schedule() {
|
|
|
|
min="$( echo "${1}" | cut -d' ' -f1 )"
|
|
|
|
hour="$( echo "${1}" | cut -d' ' -f2 )"
|
|
|
|
day="$( echo "${1}" | cut -d' ' -f3 )"
|
|
|
|
month="$( echo "${1}" | cut -d' ' -f4 )"
|
|
|
|
weekday="$( echo "${1}" | cut -d' ' -f5 )"
|
|
|
|
command="${2}"
|
|
|
|
|
|
|
|
printf '%-8s%-8s%-8s%-8s%-8s%s' "${min}" "${hour}" "${day}" "${month}" "${weekday}" "${command}"
|
|
|
|
}
|
|
|
|
|
|
|
|
print_cron_header() {
|
|
|
|
# don't split the '#' from 'min'
|
|
|
|
print_cron_schedule '#_min hour day month weekday' 'command' | tr '_' ' '
|
2020-08-26 23:30:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
print_crontab() {
|
2020-08-26 23:30:14 +00:00
|
|
|
echo '# crontab generated for kiwi-backup'
|
|
|
|
printf '# generation time: '; date
|
|
|
|
echo '#'
|
2020-08-27 13:57:36 +00:00
|
|
|
echo "$( print_cron_header )"
|
|
|
|
echo "$( print_cron_schedule "${SCHEDULE_BACKUP}" "$( print_command backup )" )"
|
|
|
|
echo "$( print_cron_schedule "${SCHEDULE_CLEANUP}" "$( print_command cleanup )" )"
|
|
|
|
echo "$( print_cron_schedule "${SCHEDULE_RMFULL}" "$( print_command rmfull )" )"
|
|
|
|
echo "$( print_cron_schedule "${SCHEDULE_RMINCR}" "$( print_command rmincr )" )"
|
2020-08-26 23:30:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 13:57:36 +00:00
|
|
|
########
|
|
|
|
# MAIN #
|
|
|
|
########
|
|
|
|
|
|
|
|
if [ "${1}" = '-n' ]; then
|
|
|
|
# dry-run
|
|
|
|
print_crontab
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-08-26 23:30:14 +00:00
|
|
|
# replace crontab, start crond
|
2020-08-27 13:57:36 +00:00
|
|
|
print_crontab | crontab -
|
2020-08-26 23:30:14 +00:00
|
|
|
crond -fl 8
|
2020-08-27 13:57:36 +00:00
|
|
|
exit 0
|