Ei kuvausta
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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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"
  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. sudo -u postgres pg_dumpall -c | gzip > /decrypted/postgresql-backup.sql.gz
  49. # Do backups
  50. for dir in $DIRS; do
  51. echo "==> create $BACKUP-$dir"
  52. $TARSNAP $EXTRA_FLAGS -c -f $BACKUP-$dir $dir
  53. done
  54. # Backups done, time for cleaning up old archives
  55. # using tail to find archives to delete, but its
  56. # +n syntax is out by one from what we want to do
  57. # (also +0 == +1, so we're safe :-)
  58. DAILY=`expr $DAILY + 1`
  59. WEEKLY=`expr $WEEKLY + 1`
  60. MONTHLY=`expr $MONTHLY + 1`
  61. # Do deletes
  62. TMPFILE=/tmp/tarsnap.archives.$$
  63. $TARSNAP --list-archives > $TMPFILE
  64. for dir in $DIRS; do
  65. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-daily-$dir" $TMPFILE | sort -rn | tail -n +$DAILY`; do
  66. echo "==> delete $i"
  67. $TARSNAP -d -f $i
  68. done
  69. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-weekly-$dir" $TMPFILE | sort -rn | tail -n +$WEEKLY`; do
  70. echo "==> delete $i"
  71. $TARSNAP -d -f $i
  72. done
  73. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-monthly-$dir" $TMPFILE | sort -rn | tail -n +$MONTHLY`; do
  74. echo "==> delete $i"
  75. $TARSNAP -d -f $i
  76. done
  77. done
  78. rm $TMPFILE