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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#!/usr/bin/env bash
#
# Script that determines whether it is time to run a backup.
echo "Running timer script: $@"
client="$1" ; shift
current="$1" ; shift
storage_dir="$1" ; shift
reserved1="$1" ; shift
reserved2="$1" ; shift
interval="$1" ; shift
timestamp="$current/timestamp"
# A 'backup' file placed in the storage directory tells this script that
# a backup needs to be done right now.
# This gives the 'server initiates a manual backup' feature.
manual_file="$storage_dir/$client/backup"
if [ -f "$manual_file" ] ; then
echo "Found $manual_file"
echo "Do a backup of $client now"
rm -f "$manual_file"
exit 0
fi
# The rest of the arguments, if any, should be timebands.
# Set LANG=C and LC_TIME=C so that 'date' returns English day names.
curdayhour=$(LANG=C LC_TIME=C date +"*%a*%H*")
intimeband=0 # If no timebands given, default to not OK.
while [ "$#" -gt 0 ] ; do
intimeband=0
timeband="$1"
case "$timeband" in
$curdayhour|always)
echo "In timeband: $timeband"
intimeband=1
break
;;
*)
echo "Out of timeband: $timeband"
;;
esac
shift
done
get_intervals()
{
if [ ! -e "$current" ] ; then
echo "No prior backup of $client"
return 0
fi
if [ ! -f "$timestamp" ] ; then
echo "$0: Timestamp file missing for $client."
return 0
fi
if [ -z "$interval" ] ; then
echo "$0: No time interval given for $client."
return 0
fi
case "$interval" in
[0-9]*s) i=${interval%s*} ; intervalsecs=$i ;;
[0-9]*m) i=${interval%m*} ; intervalsecs=$((i*60)) ;;
[0-9]*h) i=${interval%h*} ; intervalsecs=$((i*60*60)) ;;
[0-9]*d) i=${interval%d*} ; intervalsecs=$((i*60*60*24)) ;;
[0-9]*w) i=${interval%w*} ; intervalsecs=$((i*60*60*24*7)) ;;
[0-9]*n) i=${interval%n*} ; intervalsecs=$((i*60*60*24*30)) ;;
*) echo "$0: interval $interval not understood for $client."
return 0 ;;
esac
if [ -z "$intervalsecs" ] ; then
echo "$0: interval $interval not understood for $client."
return 0
fi
read junk ts < "$timestamp"
if ! secs=$(LANG=C LC_TIME=C date +%s -d "$ts") \
|| ! now=$(LANG=C LC_TIME=C date +"%Y-%m-%d %H:%M:%S") \
|| ! nowsecs=$(LANG=C LC_TIME=C date +%s -d "$now")
then
echo "$0: Date command returned error for $client."
return 0
fi
min_timesecs=$((secs+intervalsecs))
# GNU coreutils 'date' command should accept the following (even
# slightly old versions).
if ! min_time=$(LANG=C LC_TIME=C date -d "Jan 1, 1970 00:00:00 +0000 + $min_timesecs seconds" +"%Y-%m-%d %H:%M:%S")
then
# FreeBSD 'date' will return an error with the above, so try
# a version that FreeBSD 'date' should be happy with.
if ! min_time=$(LANG=C LC_TIME=C date -r $min_timesecs +"%Y-%m-%d %H:%M:%S")
then
echo "$0: Date command returned error for $client."
return 0
fi
fi
echo "Last backup: $ts"
echo "Next after : $min_time (interval $interval)"
return 1
}
if [ "$intimeband" = "0" ] ; then
get_intervals
exit 1
fi
if get_intervals ; then
echo "Currently in timeband"
exit 0
fi
if [ "$min_timesecs" -lt "$nowsecs" ] ; then
echo "$min_time < $now."
echo "Currently in timeband"
exit 0
fi
echo "Currently out of timeband"
exit 1
|