Backup plans are ignored too often.

I know. Believe me. I know.

Any good backup plan will include an offsite backup.

I run different servers that are automatically backed up by AWS.

But, just in case, I like offloading important files to a server that I have control over.

I backup mysql databases every day. You can see how I use cronjobs.

Then I offload these files to my local computer by using a rsync script.

After it gets downloaded to the local computer, I then upload it to a NAS.

The script is stored on the local computer with a file name that ends with a .sh extension. For example, the script can be called

Then to run the script, you go to the directory where the script is stored on the local computer and execute this command at the command prompt:


Below is an example of the code that could be used in the file

Just substitute your usernames, ip addresses, and directories.

Backup Script Example Using Rsync


echo "downloading files"

rsync -av remote_user_name@ip_address_of_remote_server:/home/username_of_remote_backup/remote_backup_directory /Users/local_user/local_backup_directory

echo "Files will upload to NAS."

rsync -av /Users/username_of_local_backup/local_backup_directory username_of_NASe@NAS_ip_address:/volume1/homes/username_of_NAS/NAS_backup_directory

echo "Finished!!!"

Explanation Of Backup Script

The first line of the script just defines which interpreter to use. NOTE: There is no leading whitespace before #!/bin/bash.

See Ubuntu.

echo "downloading files gives a prompt that the script is beginning.

rsync -av remote_user_name@ip_address_of_remote_server:/home/username_of_remote_backup/remote_backup_directory /Users/local_user/local_backup_directory

This lines downloads files from the remote server directory to your local backup directory.

The av option in the rsync command stands for “archive” and “verbose.” “archive” means that everything in the directory will be backed up. “verbose” means that rsync will output which files are being copied.

Here’s an example from Rsync.

rsync -avz foo:src/bar /data/tmp

This would recursively transfer all files from the directory src/bar on the machine foo into the /data/tmp/bar directory on the local machine. The files are transferred in archive mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

See Rsync Docs

On my example, rsync -av, I left off the “z” option as I didn’t want to use compression.

Also, in my code, after I download the remote files locally, I then upload the files to another server using a similar command.

That way my backups are in another place.

echo "Files will upload to NAS."

rsync -av /Users/username_of_local_backup/local_backup_directory username_of_NAS@NAS_ip_address:/volume1/homes/username_of_NAS/NAS_backup_directory

echo "Finished!!!"

The purpose of the echo statements is to track the backup. The echo statements are printed out on the screen as the script is run. If there’s an error, it would narrow down where the happened occurred.