cryptshotr-cron 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. ## This script is a cron wrapper for cyrptshotr.
  3. # It was made to help ensure backup run, even if the lid was closed when
  4. # they should have. It will default to running the periodicity if not found
  5. # in the log. There is some buffer time built in, to avoid dropping backups.
  6. # It will delete old periodicity log entires on pass.
  7. ## Set log location.
  8. CONF_DIR="$HOME/.config/cryptshotr-cron"
  9. LOG_PATH="$CONF_DIR/log"
  10. ## Get start time, in epoch
  11. start=$(date +%s)
  12. ## Test for conf dir, make if missing
  13. if [ ! -d $CONF_DIR ]; then
  14. mkdir -p $CONF_DIR
  15. fi
  16. ### Set Time Check ### {{{
  17. ## Declare intended interval distance in seconds
  18. # hour 3600, day 86400, week 604800, month 2419200, year 29030400
  19. s_hour=3600
  20. s_day=86400
  21. s_week=604800
  22. s_month=2419200
  23. s_year=29030400
  24. ## Added a buffer, to help reduce dropping backup frequency
  25. b_hour=0 # Zero Hours
  26. b_day=14400 # Four hours
  27. b_week=14400 # Four hours
  28. b_month=14400 # Four hours
  29. b_year=14400 # Four hours
  30. ## Initialize values to beat, add one for postarity
  31. # buffer margin: hourly 0, daily 4h, weekly 4h, monthly 6h, year 12h
  32. ch_hour=$(($start + 1 - $s_hour + $b_hour))
  33. ch_day=$(($start + 1 - $s_day + $b_day))
  34. ch_week=$(($start + 1 - $s_week + $b_week))
  35. ch_month=$(($start + 1 - $s_month + $b_month))
  36. ch_year=$(($start + 1 - $s_year + $b_year))
  37. ### End Set Time Check ### }}}
  38. ### Last Run Check ### {{{
  39. ## Initialize last run for periodicity to being un-run
  40. lr_hour=0
  41. lr_day=0
  42. lr_week=0
  43. lr_month=0
  44. lr_year=0
  45. if [ -e $LOG_PATH ]; then
  46. ## Awk matches one periodicity, and sees if last instance passed.
  47. ## If passed, set var to when. Otherwise set to un-run.
  48. lr_hour=$(awk '/hourly/ {LL=$0} END {match(LL, /(\w+)\t(passed)\t([0-9]+)/, m); if(m[3]==""){print 0} else {print m[3]}}' $LOG_PATH)
  49. lr_day=$(awk '/daily/ {LL=$0} END {match(LL, /(\w+)\t(passed)\t([0-9]+)/, m); if(m[3]==""){print 0} else {print m[3]}}' $LOG_PATH)
  50. lr_week=$(awk '/weekly/ {LL=$0} END {match(LL, /(\w+)\t(passed)\t([0-9]+)/, m); if(m[3]==""){print 0} else {print m[3]}}' $LOG_PATH)
  51. lr_month=$(awk '/monthly/ {LL=$0} END {match(LL, /(\w+)\t(passed)\t([0-9]+)/, m); if(m[3]==""){print 0} else {print m[3]}}' $LOG_PATH)
  52. lr_year=$(awk '/yearly/ {LL=$0} END {match(LL, /(\w+)\t(passed)\t([0-9]+)/, m); if(m[3]==""){print 0} else {print m[3]}}' $LOG_PATH)
  53. fi
  54. ### End Last Run Check ### }}}
  55. ### Backup Check and Run ### {{{
  56. backup_ch() {
  57. if [ $2 -lt $3 ]; then
  58. ## Run backup periodicity
  59. sudo /usr/local/sbin/cryptshotr -q $4
  60. if [ $? -eq 0 ]; then
  61. ## Purge old periodicity entries if passed
  62. sed -i "/$4/d" $1
  63. echo "$(date '+%F %T')\t$4\tpassed\t$(date '+%s')" | cat >> $1
  64. else
  65. echo "$(date '+%F %T')\t$4\tFAILED\t$(date '+%s')" | cat >> $1
  66. fi
  67. fi
  68. }
  69. backup_ch $LOG_PATH $lr_hour $ch_hour "hourly"
  70. backup_ch $LOG_PATH $lr_day $ch_day "daily"
  71. backup_ch $LOG_PATH $lr_week $ch_week "weekly"
  72. backup_ch $LOG_PATH $lr_month $ch_month "monthly"
  73. backup_ch $LOG_PATH $lr_year $ch_year "yearly"
  74. ### End Backup Check and Run ### }}}