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

166 lines
4.3 KiB
Text
Raw Permalink Normal View History

2020-08-26 23:30:14 +00:00
#!/bin/sh
#############
# CONSTANTS #
#############
# commands
2020-09-22 15:00:09 +00:00
env_exe="$(command -v env)"
ionice_exe="$(command -v ionice)"
duplicity_exe="$(command -v duplicity)"
2020-09-22 14:40:04 +00:00
# files
duplicity_secrets_file='/root/duplicity_secrets'
###############
# ENVIRONMENT #
###############
# load secrets file
if [ -f "${duplicity_secrets_file}" ]; then
# shellcheck disable=SC1090
. "${duplicity_secrets_file}"
fi
2020-09-22 15:00:09 +00:00
env_changes=""
# check if uses encryption
if [ -n "${GPG_KEY_ID}" ]; then
# gpg key given
2020-09-22 15:00:09 +00:00
env_changes="${env_changes} PASSPHRASE='${GPG_PASSPHRASE}'"
encrypt_opts="--encrypt-key='${GPG_KEY_ID}'"
else
# no key given
encrypt_opts="--no-encryption"
fi
2020-09-22 14:40:04 +00:00
# check if uses AWS
if [ -n "${AWS_ACCESS_KEY_ID}" ]; then
# export AWS credentials
2020-09-22 15:00:09 +00:00
env_changes="${env_changes} AWS_ACCESS_KEY_ID='${AWS_ACCESS_KEY_ID}'"
env_changes="${env_changes} AWS_SECRET_ACCESS_KEY='${AWS_SECRET_ACCESS_KEY}'"
2020-09-22 14:40:04 +00:00
fi
#############
# FUNCTIONS #
#############
append_options() {
ao_cmdline="${1}"
ao_options="${2}"
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
}
print_command() {
pc_task="${1}"
2020-08-26 23:30:14 +00:00
shift 1
2020-09-22 15:00:09 +00:00
# if environment should be changed, call with "env"
if [ -n "${env_changes}" ]; then
pc_cmdline="${env_exe}${env_changes} "
else
pc_cmdline=""
fi
pc_cmdline="${pc_cmdline}${ionice_exe} -c 3 ${duplicity_exe} ${encrypt_opts}"
2020-08-26 23:30:14 +00:00
case "${pc_task}" in
2020-08-26 23:30:14 +00:00
backup)
pc_cmdline="${pc_cmdline} --allow-source-mismatch --volsize ${BACKUP_VOLSIZE} --full-if-older-than ${FULL_BACKUP_FREQUENCY}"
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_BACKUP} /backup/source" )"
2020-08-26 23:30:14 +00:00
;;
cleanup)
pc_cmdline="${pc_cmdline} cleanup --force"
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_CLEAN}" )"
2020-08-26 23:30:14 +00:00
;;
rmfull)
pc_cmdline="${pc_cmdline} remove-older-than ${BACKUP_RETENTION_TIME} --force"
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_RMFULL}" )"
2020-08-26 23:30:14 +00:00
;;
rmincr)
pc_cmdline="${pc_cmdline} remove-all-inc-of-but-n-full ${KEEP_NUM_FULL_CHAINS} --force"
pc_cmdline="$( append_options "${pc_cmdline}" "${OPTIONS_RMINCR}" )"
2020-08-26 23:30:14 +00:00
;;
esac
pc_cmdline="${pc_cmdline} ${BACKUP_TARGET}"
echo "${pc_cmdline}"
2020-08-26 23:30:14 +00:00
}
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}"
2020-08-26 23:30:14 +00:00
}
print_crontab() {
2020-08-26 23:30:14 +00:00
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}" "$( 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 )"
2020-08-26 23:30:14 +00:00
}
########
# MAIN #
########
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 -
crond -fl 8
fi
exit 0