暫無描述
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 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. # Tarsnap backup script
  3. # Written by Tim Bishop, 2009.
  4. # Directories to backup
  5. DIRS="/home /root /decrypted /var/www /var/lib/mysql"
  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. # end of config
  21. set -e
  22. # day of week: 1-7, monday = 1
  23. DOW=`date +%u`
  24. # day of month: 01-31
  25. DOM=`date +%d`
  26. # month of year: 01-12
  27. MOY=`date +%m`
  28. # year
  29. YEAR=`date +%Y`
  30. # time
  31. TIME=`date +%H%M%S`
  32. # Backup name
  33. if [ X"$DOM" = X"$MONTHLY_DAY" ]; then
  34. # monthly backup
  35. BACKUP="$YEAR$MOY$DOM-$TIME-monthly"
  36. elif [ X"$DOW" = X"$WEEKLY_DAY" ]; then
  37. # weekly backup
  38. BACKUP="$YEAR$MOY$DOM-$TIME-weekly"
  39. else
  40. # daily backup
  41. BACKUP="$YEAR$MOY$DOM-$TIME-daily"
  42. fi
  43. # Do backups
  44. for dir in $DIRS; do
  45. echo "==> create $BACKUP-$dir"
  46. $TARSNAP $EXTRA_FLAGS -c -f $BACKUP-$dir $dir
  47. done
  48. # Backups done, time for cleaning up old archives
  49. # using tail to find archives to delete, but its
  50. # +n syntax is out by one from what we want to do
  51. # (also +0 == +1, so we're safe :-)
  52. DAILY=`expr $DAILY + 1`
  53. WEEKLY=`expr $WEEKLY + 1`
  54. MONTHLY=`expr $MONTHLY + 1`
  55. # Do deletes
  56. TMPFILE=/tmp/tarsnap.archives.$$
  57. $TARSNAP --list-archives > $TMPFILE
  58. for dir in $DIRS; do
  59. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-daily-$dir" $TMPFILE | sort -rn | tail -n +$DAILY`; do
  60. echo "==> delete $i"
  61. $TARSNAP -d -f $i
  62. done
  63. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-weekly-$dir" $TMPFILE | sort -rn | tail -n +$WEEKLY`; do
  64. echo "==> delete $i"
  65. $TARSNAP -d -f $i
  66. done
  67. for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-monthly-$dir" $TMPFILE | sort -rn | tail -n +$MONTHLY`; do
  68. echo "==> delete $i"
  69. $TARSNAP -d -f $i
  70. done
  71. done
  72. rm $TMPFILE