Advertisement
layr

backup_script

Mar 30th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. #!/bin/bash
  2. ####################################
  3. # Backup script for backing up files onto a remote SSH server using rsync. Meant for running hourly. # Obviously requires rsync
  4. # Requires autologin with RSA keys.
  5. # If there's another rsync process running (on server side) for unreasonably long time, error report gets sent onto mail (requires msmtp to be installed and configured).
  6. # (example msmtp config file, that should be located at $HOME/.msmtprc : http://msmtp.sourceforge.net/doc/msmtprc.txt )
  7. #
  8. # Create logfile: sudo touch /var/log/rsync.log # Good for future debugging.
  9. # And change its permissions, so rsync can write into it: 'sudo chmod 6666 /var/log/rsync.log'
  10. #
  11. # To set the script run every hour: 'crontab -e' and enter new line: '0 * * * * /usr/local/bin/data_backup'
  12. # Once a week with --delete flag (with prompt): '0 0 * * 1 xfce4-terminal --command="data_backup delete automated"' # This example is obviously for xfce4 terminal.
  13. #
  14. # Usage: data_backup
  15. # data_backup delete # Adds --delete flag to rsync command, so the files not existing on client will get removed.
  16. # data_backup delete automated # Automated variable adds few extra messages and closing prompt in the beginning.
  17. #
  18. # Written by Laur Aliste <laur.aliste รคt gmail>
  19. #################################### EDIT ONLY THESE VALUES: ####################################
  20. user=aliste # Remote user to login to.
  21. host=192.168.1.101 # Remote host address.
  22. host_home=home/$user # Remote user /home directory location.
  23. # Where to send the error report to:
  24. email=server.report.2axf5wl@gmail.com
  25. # ... and a copy (optional):
  26. email2=laur.aliste@gmail.com
  27. # What to backup:
  28. backup_files="$HOME/Documents $HOME/Desktop $HOME/Music $HOME/Pictures $HOME/.gnome2 $HOME/.mpd $HOME/.ncmpcpp $HOME/.ssh $HOME/.fonts.conf $HOME/.msmtprc $HOME/.conky $HOME/.conkyrc_error_reporting" # If a slash is at the end of a folder name, then only the contents will be backed up into $dest instead of the folder itself.
  29. # Where to backup to:
  30. dest="$user@$host:/$host_home/Documents/8510w_backups/running_backups/" # Here $HOME cannot be used, unless the server and client usernames match.
  31. # Name and location of errorfile, that gets sent to email:
  32. errorfile=$HOME/.rsync_error.dat
  33. #################################################################################################
  34. bold="tput smso"
  35. offbold="tput rmso"
  36. delete_flag=""
  37. mail_sent=0
  38. if [[ $# > 2 ]]; then
  39. echo "Too many arguments!"; exit 1
  40. elif [[ "$1" == "delete" ]]; then
  41. if [[ "$2" == "automated" ]]; then
  42. $bold
  43. echo "This is a weekly automated script for backing up with --delete flag."
  44. $offbold
  45. fi
  46. echo "Delete files at remote host that no longer exist on the client?"
  47. echo "(i.e. add --delete flag?)"
  48. echo "y/n:"
  49. read prompt
  50. if [[ "$prompt" == "y" ]]; then
  51. delete_flag="--delete --delete-before"
  52. fi
  53. fi
  54.  
  55. count=5
  56. # Check whether the server is online:
  57. if ping -W 2 -c 1 $host > /dev/null 2>&1; then
  58. # Check whether there are other rsync process currently running:
  59. echo $(ssh $user@$host cat /$host_home/.rsync_status.dat) > $HOME/.rsync_status.dat # Copies status value from server; directly reading somehow didn't work.
  60. if [[ "$(cat $HOME/.rsync_status.dat)" != "0" ]]; then
  61. until [[ "$(cat $HOME/.rsync_status.dat)" == "0" ]] || [[ "$count" == "0" ]]; do # Wait until already running rsync process finishes.
  62. clear
  63. $bold
  64. echo "Other rsync process in progress."
  65. echo "Sleeping for 5 min and then trying again ($count attempt(s) remaining)..."
  66. $offbold
  67. sleep 300
  68. echo $(ssh $user@$host cat /$host_home/.rsync_status.dat) > $HOME/.rsync_status.dat # Fetch updated rsync status from server.
  69. let count=$count-1
  70. done
  71. if [[ "$count" == "0" ]]; then # If already running rsync process didn't end, then print error, compile error file and send to email account.
  72. echo "To: $email" > $errorfile
  73. #echo "From: $email" >> $errorfile # Seems to not function.
  74. echo "Cc: $email2" >> $errorfile
  75. echo "Subject: rsync error on client machine.
  76. " >> $errorfile
  77. echo "Rsync status stays '1'. Check the server (or client)!" >> $errorfile
  78. echo $(date) >> $errorfile
  79. if ping -W 2 -c 1 google.com > /dev/null 2>&1; then
  80. msmtp -t < $errorfile
  81. mail_sent=1
  82. fi
  83. clear
  84. echo "There seems to be a problem with current running rsync process"
  85. echo "or server itself. Please try again or check the server; aborting."
  86. if [[ "$mail_sent" == "1" ]]; then
  87. echo "An error report was sent onto predefined email account."
  88. else
  89. echo "Error report was not sent onto email. Probable cause: internet connection offline."
  90. fi
  91. exit 1
  92. fi
  93. fi
  94. echo '1' | ssh $user@$host "cat > /$host_home/.rsync_status.dat" # Set rsync status file to '1', so server side couldn't start syncing while client is updating.
  95.  
  96. # Print start status message.
  97. $bold
  98. echo "Backing up $backup_files to $dest"
  99. $offbold
  100. # Syncing commands:
  101. rsync -rav $delete_flag --log-file=/var/log/rsync.log $backup_files $dest
  102. rsync -rav $delete_flag --log-file=/var/log/rsync.log $backup_files $dest # Second run for syncing the changes that were made during first sync; this is always a quick run.
  103. # Most usable rsync flags:
  104. # -r = recursive
  105. # -a = archive; preserves all attributes like ownership, timestamps, etc; also copies symlinks
  106. # -z = compress; saves bandwidth but is harder on your CPU so use it for slow/expensive connections only
  107. # -u = skip files that are newer on the receiver (avoids copying the files that we already have in the destination folder that have not been modified in the source folder.)
  108. # -h = output numbers in a human-readable format, i.e. the file sizes are given in K's, M's, G's and so on
  109. # -e = specify the remote shell to use (e.g. -e ssh)
  110. # -v = verbose; this parameter will print information about the execution of the command, such as the files that are successfully transferred, so we can use this as a way to keep track of the progress of rsync.
  111. # --size-only = compare files based on their size instead of hashes (less CPU, so faster) i.e. skip files that match in size
  112. # --progress = shows you the progress of all the files that are being synced
  113. # --delete = delete files remotely that no longer exist locally
  114. # --dry-run = show what would have been transferred, but do not transfer anything
  115. # --delete-before = receiver deletes files that don't exist in the source before xfer, not during
  116. # --log-file=/var/log/rsync.log = self explanatory
  117.  
  118. # Print end status message:
  119. $bold
  120. echo "Backup finished"
  121. $offbold
  122. echo '0' | ssh $user@$host "cat > /$host_home/.rsync_status.dat" # Set rsync status back to "0" in the end.
  123. if [[ "$2" == "automated" ]]; then
  124. echo "Press enter to close the window."
  125. read dummy_variable # Prevents window closing automagically.
  126. fi
  127. else
  128. $bold
  129. echo "Server seems to be offline."
  130. $offbold
  131. fi
  132. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement