There's no need to glorify how a backup can save the day. If you've been hit by a critical hard drive error, you'll know what I mean.
So without further adieu, let's begin.
Setting up automatic access:
Your source machine will need to be able to access the destination machine without requiring a password. Why? It's better than leaving your passwords laying around in the script files.
You'll need to generate the private/public key files and then allow access on the destination machine.
Open up a terminal window and do the following:
- ssh-keygen -t rsa
- Press Enter (default file is fine)
- Press Enter (empty password is how it connects without needing one)
- Press Enter (confirm empty password)
- cat ~/.ssh/id_rsa.pub (to display the public key)
- Copy it to your clipboard
Now to do some preparation on your destination server.
- Log into your remote server
- vi .ssh/authorized_keys (to edit your access list)
- Paste the key from your clipboard into a new line at the bottom of the file
- Save and exit
Now try "ssh firstname.lastname@example.org". It should be able to connect without asking for a password.
Preparing the backup script
You'll need a place to store the backups without cluttering up your home folder.
From this point forward, I will be assuming the following:
- I'll be storing the backup archives in
- The folder I want to back up
A little explanation about what's about to happen.
We'll create a script called "backup.sh" which will:
- compress the contents of /code/ into a TGZ file
- The TGZ filename will depend on the current date (so you can have multiple archives)
- When it's done, we use scp to copy the archive over to the remote server.
- After it's been uploaded, we delete the file from our source machine.
- Just to do a bit of maintenance cleaning, we'll delete all the old archives on the remote machine apart from the newest 3.
Now to make the script that'll do the magic.
- "vi backup.sh" to create the script file and open it up. Paste in:
tar czf - $FOLDER > $FILE
scp $FILE email@example.com:~/backups/code/
ssh firstname.lastname@example.org '~/bin/clean_backups.sh'
- "chmod +x backup.sh" to make it executable.
- Use "cron" to schedule the script. Edit /etc/crontab to schedule the file under YOUR username (default is "root" user). Google the syntax for usage.
Keeping a certain number of backups
"clean_backups.sh" is a script on the remote machine which automatically deletes every backup file apart from the latest 3.
This step is optional, so it's up to you if you want to do it.
On the remote server, create a script in "~/bin/clean_backups.sh" and make it executable.
ls -t backup_code* | sed -e '1,3d' | xargs -d '\n' rm
For testing purposes, replace "rm" at the end with "echo". It'll print out all the files it wants to delete.
Now, if a critical hard drive failure decides to show up in your face... you're ready!