Automate your backups

In this short “how-to” I’ll explain on how to back up your home folder (or anything else you want) every week.

I’ll be using rsync and bash script and a cron job.

The whole thing will take 2 minutes to set up.

Before you start reading,  keep in mind that when you see “–xxx” in a command, it is actually “- -xxx” but without a space in between them.

Create the rsync

I’ll be using this command

sudo rsync -av –progress –delete  /home /media/Disk

This command will back your you home folder and copy it to a external hdd.

You external hdd could have another name.

Navigate to /media and find out how yours is called.

Note: if you to exclude a folder from the backup, use  –exclude “/home/username/folder” .

Read the rsync man page for more options.


Creating the script.

Create a new file and simply type “#!/bin/bash” followed with the rsync command.

Mine looks like this:

#!/bin/bash

#Back up my home folder to my external hdd
sudo rsync -av –progress –delete  /home /media/Disk

I’ll call my script “backup.sh”.

Right-click the script, click properties and in the permission tab select “allow executing …”.

Or you can do the same from the terminal using this command

sudo chmod +x /home/username/directory/backup.sh

You could now just launch the script and it will back up your system  (double click the script and choose “run in terminal”).

But you can just automate it and you’ll never have to think about back ups again.

Creating the cron job

Because the script will prompt you for your password, we’ll need to be creative here.

What’s the point of automated backups when you’ll need to enter you password everytime before it starts?

The easy solution is just moving the script to /root.

Press “alt+f2” and enter gksudo nautilus.

Now you can easily move the file to /root.

Now for the cron job part.

In a terminal enter

sudo crontab -e

A nano text document will open.

Use your arrow keys to navigate to the bottom of the document and type in this:

0 12 * * 7 /root/backup.sh

This job will launch the backup script every sunday at noon.

The first number here, the 0 is meant for the number of minutes after the hour.

The second number here, 12 is the hour of the day. For American users, this uses the 24 hour system.

The third number, here a *, is the day of the month.

The fourth number, here a * is the month of the year.

The fifth number is the day of the week. The 0 or 7 being Sunday.

After the fifth number you insert the command.

Now press “ctrl + x” and press “y” to save the file.

Done.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: