Getting the files
Now that the database is backed up, what’s the best way to suck the files from the hosting provider?
There’s another tool called rsync which can synchronize a set of files between two machines over a network. It’s quite easy to use. Here’s the command that I use on my local machine at home to grab the files:
rsync -avz --delete -e ssh <username>@<username>:~/backups ~/localbackups/
Just replace <username> and <hostname> with the proper values. One thing to point out is that I use the –delete parameter. This specifies that if the file does not exist on the remote machine, then delete it from the local machine. So, when the cleanup-files.cron script runs and cleans out the old files, rsync will mirror those changes locally. Otherwise I would continue to fill up my hard drive with database backups.
If you ran the rsync command I’m sure you noticed that it prompted you for your password to the remote machine. This makes is difficult to automate this into a script. However, there is a solution for this as well. Basically we need to instruct the remote machine to “trust” the local machine on a permanent basis. This can be done by creating an ssh key and uploading it to the remote server (web host.)
On your local machine, run this command:
ssh-keygen -t rsa
This will create a file called
You will need to append this to your
~/.ssh/authorized_keys file on the remote host. Keep in mind the
~/.ssh directory and
id_rsa.pub file may not yet exist.
If all goes as expected, you can run the rsync command again and you should not be prompted for your password. Now we just copy the rsync command into a file, mark it as executable and schedule the cronjob on the local machine.
echo 'rsync -avz --delete -e ssh <username>@<hostname>:~/backups ~/localbackups/' > ~/cronjobs/get-db-backups.cron
chmod +x ~/cronjobs/get-db-backups.cron
(a, paste in, ESC, :wq, ENTER)
0 2 * * * ~/cronjobs/get-db-backups.cron
This will grab the files a 2 AM every day.
So, we’ve gone through all of this just to grab the SQL backup files, but this post is about backing up the whole website. Well, by no stretch of the imagination you can simply modify the get-db-backups.cron script to sync all of the files instead of just the ones in the ~/backups directory.
Better yet, why not create another script that maybe runs once a week (instead of every day) to sync all of the files.
For Windows users, there are some good websites that show how to set this up.