#!/usr/bin/zsh set -x PRETEND="no" run_rsnapshot() { rsnapshot -c /etc/rsnapshot.conf $1 } while true; do NOW=$(date +"%Y-%m-%d %H:%M:%S") TIME=$(date --date="$NOW" +"%H:%M:%S") DATE=$(date --date="$NOW" +"%Y-%m-%d") TOMORROW=$(date --date="$NOW + 1 day" +"%Y-%m-%d") OVERNIGHT="no" if dateutils.dtest "$TIME" --gt "20:00:00" && dateutils.dtest "$NOW" --lt "$TOMORROW 08:00:00"; then if [[ $PRETEND != "yes" ]]; then echo "daily" run_rsnapshot daily fi if [[ $(date +%A) == "Sunday" ]]; then echo "Sunday -> weekly" if [[ $PRETEND != "yes" ]]; then sleep 1800 # wait 30 minutes run_rsnapshot weekly fi fi if [[ $(date +%d) == "01" ]]; then echo "1st of a month -> monthly" if [[ $PRETEND != "yes" ]]; then sleep 1800 # wait 30 minutes run_rsnapshot monthly fi fi if [[ $(date +"%m-%d") == "01-01" ]]; then echo "1st day of the year -> yearly" if [[ $PRETEND != "yes" ]]; then sleep 1800 # wait 30 minutes run_rsnapshot yearly fi fi wakeup=$(date -d "$TOMORROW 08:00:00" +'%Y%m%d%H%M%S') next=$(date -d "$TOMORROW 08:00:00" +'%s') OVERNIGHT="yes" else echo "hourly" if [[ $PRETEND != "yes" ]]; then run_rsnapshot hourly fi wakeup=$(date -d "now + 1 hour" +'%Y%m%d%H5959') next=$(date -d "now + 1 hour" +'%s') OVERNIGHT="no" fi echo "NEXT" echo $next echo "WAKEUP" echo $wakeup sleep 15 # wait 15 seconds if [[ $PRETEND != "yes" ]]; then if [[ $OVERNIGHT == "yes" && $(users) == "" ]]; then rtcwake -m mem --date $wakeup else sleep $(( $next - $(date +%s) )) fi fi done