Guest User

www.backup.sh

a guest
Aug 27th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.30 KB | None | 0 0
  1. #!/bin/bash
  2. filename=site_backups/$(date +%Y%m%d).tar.gz
  3. current_log_text=site_backups/$(date +%Y%m%d).txt
  4. is_error=0
  5. WEBROOT=www/
  6. SUBJECT="Website nightly backup failed!"
  7.  
  8. date >> $current_log_text
  9. echo -e "Synchronizing www directory...\n" >> $current_log_text
  10. rsync -ra --delete $WEBROOT backup_www/ >> $current_log_text
  11. if [ "$?" -ne "0" ]; then
  12.     date >> $current_log_text
  13.     echo -e "Failed to synchronize." >> $current_log_text
  14.     let "is_error += 1"
  15. fi
  16.  
  17. date >> $current_log_text
  18. echo -e "Zipping most current backup...\n" >> $current_log_text
  19. tar -zcf $filename backup_www/
  20. if [ "$?" -ne "0" ]; then
  21.     date >> $current_log_text
  22.     echo -e "Failed to zip." >> $current_log_text
  23.     let "is_error += 2"
  24. fi
  25.  
  26. date >> $current_log_text
  27. echo -e "Deleting old backups...\n" >> $current_log_text
  28. find site_backups -type f -mtime +14 -exec rm {} \;
  29. if [ "$?" -ne "0" ]; then
  30.     date >> $current_log_text
  31.     echo -e "Failed to delete old backups." >> $current_log_text
  32.     let "is_error += 4"
  33. fi
  34.  
  35. date >> $current_log_text
  36. echo -e "Backup complete. Exit ("$is_error")." >> $current_log_text
  37.  
  38. if [ "$is_error" -gt "0" ]; then
  39.     (echo -e 'Something went wrong with website nightly backup. (Error '$is_error')\n' ; cat $current_log_text) | mail -s "$SUBJECT" "$EMAIL"
  40.     exit $is_error
  41. fi
Advertisement
Add Comment
Please, Sign In to add comment