Linux: Script to backup files/folders and copy it onto another server

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.

They don't have a clue how it feels to be hit by a hdd error...

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/ (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". 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 "" 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" to create the script file and open it up. Paste in:
FILE="/home/user/backups/backup_code_`date +%Y.%m.%d`.tgz"

tar czf - $FOLDER > $FILE
scp $FILE
rm $FILE
ssh '~/bin/'
  • "chmod +x" 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

"" 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/" and make it executable.

Paste in:

cd ~/backups/
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!



Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog