Automate backups Using Cronjobs In Linux

Backing up data is so important and there are so many choices in backup software out there that it can be difficuilt to decide on which one is best. In my opinion it depends on what you intend to backup. Pesonally I dont see the point to backing up installed software or even the root partition, I usually just backup either the home folder or just the files in ~/Documents for example. Personally I use and recommend rsync, I have previously written about it here.
To backup my home folder using rsync I would use
rsync -arvvp ~/ /mnt/Storage/Backup
To create a cronjob in terminal crontab -e scroll down to the bottom the format is
Minute / Hour / Day / Month / Day of the Week / Command
For instance every monday at 5am would look like
0 5 * * 1 rsync -arvvp ~/ /mnt/Storage/Backup
Ctrl X to quit , then Y to save , you should see something like crontab: installing new crontab
So thats all good and groovy , but it wouldnt it be nice to have a log of whats been backed up? I think so too. We can change the crontab and append to the end > /mnt/Storage/Backup/rsync-$(date -I).log so the new crontab line should look like
0 5 * * 1 rsync -arvvp ~/ /mnt/Storage/Backup > /mnt/Storage/Backup/rsync-$(date -I).log
You can even have that emailed to your local mbox so that you can see that in your mail client so we append && cat /mnt/Storage/Backup/rsync-$(date -I).log | mail -s “Backup” ben@localhost.
The crontab will look like
0 5 * * 1 rsync -arvvp ~/ /mnt/Storage/Backup > /mnt/Storage/Backup/rsync-$(date -I).log && cat /mnt/Storage/Backup/rsync-$(date -I).log | mail -s “Backup” ben@localhost
If you were wanting to create a tar you could use tar and use
tar -jcvpf /mnt/Storage/Backup/backup-$(date -I).tar.bz2 ~/ > /mnt/Storage/Backup/backup-$(date -I).log && cat /mnt/Storage/Backup/backup-$(date -I).log | mail -s “Backup” ben@localhost

For these examples I was backing up my home folder to /mnt/Storage/Backup , you may want to change this to suit your needs. I was also using $(date -I) to create my logs and tar archives so I can see what and when files/folders were backed up as in my opinion its easier to keep track of the ongoing backup process.
I used ben@desktop because that is my user@hostname, you may need to change this accordingly.
Also keep in mind if your home folder is large it might take awhile to backup so you might want to schedule the backup for when your not going to be using it.

Tagged with:
Posted in 01Ben, Computers, Linux
2 comments on “Automate backups Using Cronjobs In Linux
  1. b says:

    Awesome examples, gotta love the efficiency of command line!

    Found a minor error though… in your tar example, you first create backup.$(date -I).log (with a period before variable), then cat backup-$(date -I).log (with a hyphen before variable). Just wanted to point it out for the copy-paste crowd.

    Thank you!

Please feel free to leave a reply.

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

You are commenting using your 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


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 69 other followers

%d bloggers like this: