Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ####################################
- # Backup script for backing up files onto a remote SSH server using rsync. Meant for running hourly. # Obviously requires rsync
- # Requires autologin with RSA keys.
- # 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).
- # (example msmtp config file, that should be located at $HOME/.msmtprc : http://msmtp.sourceforge.net/doc/msmtprc.txt )
- #
- # Create logfile: sudo touch /var/log/rsync.log # Good for future debugging.
- # And change its permissions, so rsync can write into it: 'sudo chmod 6666 /var/log/rsync.log'
- #
- # To set the script run every hour: 'crontab -e' and enter new line: '0 * * * * /usr/local/bin/data_backup'
- # 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.
- #
- # Usage: data_backup
- # data_backup delete # Adds --delete flag to rsync command, so the files not existing on client will get removed.
- # data_backup delete automated # Automated variable adds few extra messages and closing prompt in the beginning.
- #
- # Written by Laur Aliste <laur.aliste รคt gmail>
- #################################### EDIT ONLY THESE VALUES: ####################################
- user=aliste # Remote user to login to.
- host=192.168.1.101 # Remote host address.
- host_home=home/$user # Remote user /home directory location.
- # Where to send the error report to:
- email=server.report.2axf5wl@gmail.com
- # ... and a copy (optional):
- email2=laur.aliste@gmail.com
- # What to backup:
- 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.
- # Where to backup to:
- dest="$user@$host:/$host_home/Documents/8510w_backups/running_backups/" # Here $HOME cannot be used, unless the server and client usernames match.
- # Name and location of errorfile, that gets sent to email:
- errorfile=$HOME/.rsync_error.dat
- #################################################################################################
- bold="tput smso"
- offbold="tput rmso"
- delete_flag=""
- mail_sent=0
- if [[ $# > 2 ]]; then
- echo "Too many arguments!"; exit 1
- elif [[ "$1" == "delete" ]]; then
- if [[ "$2" == "automated" ]]; then
- $bold
- echo "This is a weekly automated script for backing up with --delete flag."
- $offbold
- fi
- echo "Delete files at remote host that no longer exist on the client?"
- echo "(i.e. add --delete flag?)"
- echo "y/n:"
- read prompt
- if [[ "$prompt" == "y" ]]; then
- delete_flag="--delete --delete-before"
- fi
- fi
- count=5
- # Check whether the server is online:
- if ping -W 2 -c 1 $host > /dev/null 2>&1; then
- # Check whether there are other rsync process currently running:
- 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.
- if [[ "$(cat $HOME/.rsync_status.dat)" != "0" ]]; then
- until [[ "$(cat $HOME/.rsync_status.dat)" == "0" ]] || [[ "$count" == "0" ]]; do # Wait until already running rsync process finishes.
- clear
- $bold
- echo "Other rsync process in progress."
- echo "Sleeping for 5 min and then trying again ($count attempt(s) remaining)..."
- $offbold
- sleep 300
- echo $(ssh $user@$host cat /$host_home/.rsync_status.dat) > $HOME/.rsync_status.dat # Fetch updated rsync status from server.
- let count=$count-1
- done
- if [[ "$count" == "0" ]]; then # If already running rsync process didn't end, then print error, compile error file and send to email account.
- echo "To: $email" > $errorfile
- #echo "From: $email" >> $errorfile # Seems to not function.
- echo "Cc: $email2" >> $errorfile
- echo "Subject: rsync error on client machine.
- " >> $errorfile
- echo "Rsync status stays '1'. Check the server (or client)!" >> $errorfile
- echo $(date) >> $errorfile
- if ping -W 2 -c 1 google.com > /dev/null 2>&1; then
- msmtp -t < $errorfile
- mail_sent=1
- fi
- clear
- echo "There seems to be a problem with current running rsync process"
- echo "or server itself. Please try again or check the server; aborting."
- if [[ "$mail_sent" == "1" ]]; then
- echo "An error report was sent onto predefined email account."
- else
- echo "Error report was not sent onto email. Probable cause: internet connection offline."
- fi
- exit 1
- fi
- fi
- 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.
- # Print start status message.
- $bold
- echo "Backing up $backup_files to $dest"
- $offbold
- # Syncing commands:
- rsync -rav $delete_flag --log-file=/var/log/rsync.log $backup_files $dest
- 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.
- # Most usable rsync flags:
- # -r = recursive
- # -a = archive; preserves all attributes like ownership, timestamps, etc; also copies symlinks
- # -z = compress; saves bandwidth but is harder on your CPU so use it for slow/expensive connections only
- # -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.)
- # -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
- # -e = specify the remote shell to use (e.g. -e ssh)
- # -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.
- # --size-only = compare files based on their size instead of hashes (less CPU, so faster) i.e. skip files that match in size
- # --progress = shows you the progress of all the files that are being synced
- # --delete = delete files remotely that no longer exist locally
- # --dry-run = show what would have been transferred, but do not transfer anything
- # --delete-before = receiver deletes files that don't exist in the source before xfer, not during
- # --log-file=/var/log/rsync.log = self explanatory
- # Print end status message:
- $bold
- echo "Backup finished"
- $offbold
- echo '0' | ssh $user@$host "cat > /$host_home/.rsync_status.dat" # Set rsync status back to "0" in the end.
- if [[ "$2" == "automated" ]]; then
- echo "Press enter to close the window."
- read dummy_variable # Prevents window closing automagically.
- fi
- else
- $bold
- echo "Server seems to be offline."
- $offbold
- fi
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement