Last active
December 20, 2015 00:29
-
-
Save sadbox/6042589 to your computer and use it in GitHub Desktop.
tarsnap backup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Example crontab entry: | |
| #0 2 * * * /root/backup.sh | |
| logfile="/var/log/tarsnap.log" | |
| email="[email protected]" | |
| stuff_to_back_up="home/ etc/ var/www/ root/" | |
| exclude_pattern="Dropbox" | |
| run_backup() { | |
| local snapshot=$1 | |
| local backup="$HOSTNAME-$(/bin/date +%F)${snapshot}" | |
| ( | |
| while [ ${backupstatus:=1} -ne 0 ] && [ ${tries:=0} -le 5 ] | |
| do | |
| ((tries++)) | |
| /bin/echo "$(/bin/date +"%T %F") Starting backup: $backup" | |
| /usr/local/bin/tarsnap -c --configfile /usr/local/etc/tarsnap.conf -C / --exclude "$exclude_pattern" -f "$backup" $stuff_to_back_up 2>&1 | |
| backupstatus=$? | |
| sleep 600 | |
| done | |
| /bin/echo "$(/bin/date +"%T %F") Backup finished" | |
| ) | /usr/bin/tee -a $logfile | /usr/bin/mailx -s "$HOSTNAME backup results" $email | |
| } | |
| # Prune old backups | |
| for backup_type in "daily" "monthly" "weekly" | |
| do | |
| case $backup_type in | |
| "daily") filter_for="[0-9]$"; numbackups=7;; | |
| "monthly") filter_for="\-monthly\-snapshot$"; numbackups=12;; | |
| "weekly") filter_for="\-weekly\-snapshot$"; numbackups=4;; | |
| esac | |
| old_backups=$(/usr/local/bin/tarsnap --list-archives | /bin/grep ${filter_for} | /usr/bin/sort -t- -k1,3 | /usr/bin/head --lines -${numbackups}) | |
| for backup in $old_backups | |
| do | |
| ( /bin/echo "$(/bin/date +"%T %F") Removing backup: $backup" | |
| /usr/local/bin/tarsnap -d -f $backup 2>&1 | |
| /bin/echo "$(/bin/date +"%T %F") Finished removing backup: $backup" | |
| ) >> $logfile | |
| done | |
| done | |
| if [ "$(date +%u)" -eq "1" ] | |
| then | |
| run_backup "-weekly-snapshot" | |
| fi | |
| if [ "$(date +%d)" -eq "01" ] | |
| then | |
| run_backup "-monthly-snapshot" | |
| fi | |
| run_backup | |
| /bin/echo "=================================" >> $logfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| currentbackups="$(tarsnap --list-archives | /usr/bin/sort -t- -k1,3)" | |
| echo "Daily backups:" | |
| egrep -v '(weekly|monthly)' <<< "$currentbackups" | |
| echo | |
| echo "Weekly backups:" | |
| grep 'weekly-snapshot' <<< "$currentbackups" | |
| echo | |
| echo "Monthly backups:" | |
| grep 'monthly-snapshot' <<< "$currentbackups" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment