No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tarsnap.sh 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. # Tarsnap backup script
  3. # Written by Tim Bishop, 2009.
  4. # Directories to backup (relative to /)
  5. DIRS="home root decrypted var/www etc/letsencrypt"
  6. # Number of daily backups to keep
  7. DAILY=7
  8. # Number of weekly backups to keep
  9. WEEKLY=3
  10. # Which day to do weekly backups on
  11. # 1-7, Monday = 1
  12. WEEKLY_DAY=5
  13. # Number of monthly backups to keep
  14. MONTHLY=1
  15. # Which day to do monthly backups on
  16. # 01-31 (leading 0 is important)
  17. MONTHLY_DAY=01
  18. # Path to tarsnap
  19. TARSNAP="/usr/local/bin/tarsnap"
  20. # Extra flags to pass to tarsnap
  21. EXTRA_FLAGS="-L -C /"
  22. # end of config
  23. set -e
  24. # day of week: 1-7, monday = 1
  25. DOW=`date +%u`
  26. # day of month: 01-31
  27. DOM=`date +%d`
  28. # month of year: 01-12
  29. MOY=`date +%m`
  30. # year
  31. YEAR=`date +%Y`
  32. # time
  33. TIME=`date +%H%M%S`
  34. # Backup name
  35. if [ X"$DOM" = X"$MONTHLY_DAY" ]; then
  36. # monthly backup
  37. BACKUP="$YEAR$MOY$DOM-$TIME-monthly"
  38. elif [ X"$DOW" = X"$WEEKLY_DAY" ]; then
  39. # weekly backup
  40. BACKUP="$YEAR$MOY$DOM-$TIME-weekly"
  41. else
  42. # daily backup
  43. BACKUP="$YEAR$MOY$DOM-$TIME-daily"
  44. fi
  45. # Below command complains to stderr if postgres user cannot write to CWD
  46. cd /home/
  47. # Dump PostgreSQL to file
  48. umask 077
  49. sudo -u postgres pg_dumpall -c | gzip > /decrypted/postgresql-backup.sql.gz
  50. # Do backups
  51. for dir in $DIRS; do
  52. echo "==> create $BACKUP-$dir"
  53. $TARSNAP $EXTRA_FLAGS -c -f $BACKUP-$dir $dir
  54. done
  55. # Backups done, time for cleaning up old archives
  56. # using tail to find archives to delete, but its
  57. # +n syntax is out by one from what we want to do
  58. # (also +0 == +1, so we're safe :-)
  59. DAILY=`expr $DAILY + 1`
  60. WEEKLY=`expr $WEEKLY + 1`
  61. MONTHLY=`expr $MONTHLY + 1`
  62. # Do deletes
  63. TMPFILE=/tmp/tarsnap.archives.$$
  64. $TARSNAP --list-archives > $TMPFILE
  65. for dir in $DIRS; do
  66. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-daily-$dir" $TMPFILE | sort -rn | tail -n +$DAILY`; do
  67. echo "==> delete $i"
  68. $TARSNAP -d -f $i
  69. done
  70. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-weekly-$dir" $TMPFILE | sort -rn | tail -n +$WEEKLY`; do
  71. echo "==> delete $i"
  72. $TARSNAP -d -f $i
  73. done
  74. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-monthly-$dir" $TMPFILE | sort -rn | tail -n +$MONTHLY`; do
  75. echo "==> delete $i"
  76. $TARSNAP -d -f $i
  77. done
  78. done
  79. rm $TMPFILE