docker-restic-cron/start.sh

54 lines
1.4 KiB
Bash
Raw Normal View History

2017-03-19 00:11:03 +00:00
#! /bin/bash
2017-07-17 02:37:16 +00:00
# If first arg is bash, we'll just execute directly
2017-03-19 00:11:03 +00:00
if [ "$1" == "bash" ]; then
exec "$@"
exit 0
fi
2017-07-17 02:37:16 +00:00
# If no env variable set, get from command line
if [ "$OPT_ARGUMENTS" == "" ]; then
export OPT_ARGUMENTS="$@"
fi
2018-07-24 15:38:22 +00:00
# Init the repo
restic -r $BACKUP_DEST snapshots || restic -r $BACKUP_DEST init
2017-06-29 06:28:48 +00:00
2018-03-10 01:29:41 +00:00
# If set to restore on start, restore if the data volume is empty
if [ "$RESTORE_ON_EMPTY_START" == "true" -a -z "$(ls -A $PATH_TO_BACKUP)" ]; then
2018-07-24 15:38:22 +00:00
/restore.sh latest
2018-03-10 01:29:41 +00:00
fi
# Unless explicitly skipping, take a backup on startup
2017-03-19 00:11:03 +00:00
if [ "$SKIP_ON_START" != "true" ]; then
/backup.sh
fi
if [ -n "$CRON_SCHEDULE" ]; then
# Export the environment to a file so it can be loaded from cron
env | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/g' > /env.sh
# Remove some vars we don't want to keep
sed -i '/\(HOSTNAME\|affinity\|SHLVL\|PWD\)/d' /env.sh
# Use bash for cron
echo "SHELL=/bin/bash" > /crontab.conf
# Schedule the backups
echo "$CRON_SCHEDULE source /env.sh && /backup.sh 2>> /cron.log" >> /crontab.conf
echo "Backups scheduled as $CRON_SCHEDULE"
if [ -n "$VERIFY_CRON_SCHEDULE" ]; then
2017-06-29 01:12:49 +00:00
echo "$VERIFY_CRON_SCHEDULE source /env.sh && /verify.sh 2>> /cron.log" >> /crontab.conf
2017-03-19 00:11:03 +00:00
echo "Verify scheduled as $VERIFY_CRON_SCHEDULE"
fi
# Add to crontab
crontab /crontab.conf
2018-07-24 15:38:22 +00:00
echo "Starting restic cron..."
2017-03-19 00:11:03 +00:00
cron
2018-07-24 15:38:22 +00:00
touch /cron.log
tail -f /cron.log
2017-03-19 00:11:03 +00:00
fi