summaryrefslogtreecommitdiff
path: root/rsnapshot-wrapper
blob: bb930a3f6daf21f9e09e092c18e08eef435a82f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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