Howto:  Backups and Cron in Linux

Howto: Backups and Cron in Linux

So.  You lost stuff!

No matter how much work you put in to secure your website, or your desktop, or your laptop, chances are you’re going to have something happen that causes you to lose either your data or your data integrity.

If you’re a lazy ass like me, you’ve probably done one, maybe two backups in all the time you’ve had the damn machine, if you’re lucky.  Mostly though, probably no backups.  So when you lose everything, you rant and rave and probably rage for a bit and then realise there’s nothing you can do, and go ahead and set everything back up again, this time making a simple backup and…forget to do it again.  Vicious cycle where, at least I (dunno about you), depend more on blind luck than anything else.

This is where one of our heroes walks into the scene.  Her name is Cron and she’s a simple but efficient beauty!  Deadly for the enemies because she makes all their work useless in a heartbeat!

“cron” is a linux process that allows automation based on a clock.  Works sort of like an alarm clock except at the time for an alarm, it quietly runs a job you set for it.  Here’s how you set cron up.

type

crontab -e

the “-e” stands for edit and it opens an empty file.  What is open is actually a vi editor.  To insert text into the file, press the “i” key and type the following

37  2  *  *  *  tar -cvzf /backup/website`date +\%d\%m\%y\%H\%M`.tar.gz /webapps/website/wordpress

all on one line.  Once you do that, press the “esc” key, then press the following keys in exactly this order – “:”, “w”, “q”.

Lets break this down.

37  2  *  *  *   :Means at 2:37 AM, run what follows.

The order of numbers is minutes(0-59), hours(0-23), days(1-31), month(1-12) and day of week (0-7, Sunday is 0).

tar -cvzf   :”Tar” is an archiving utility where “c” is to create a tar, “v” is to show what it is doing (verbose), “z” is to compress and “f” is to archive.

To restore a tar archive one would use the options -xzvf, where “x” mean extract.

/backup/(name of file) is where the tarball (or tar file) will be created.  My file will be called “website2304150237.tar.gz” in the /backup folder.  The date command I used will output the date in day-month-year-hour-minute format of creation, assuming it was created on April 23rd, 2015 at 2:37AM.  This date will be appended (up ended? LOLZ) to the word “website” and finally

/webapps/website/wordpress   : This is the location of my website on the physical disk.  It will take all files and folders from that location and put them together.

The beauty of cron is that it will do that every night at 2:37 AM and I have to do nothing.  Ok, well, maybe once every few weeks make sure I delete some old backups.  Since this is a site backup, another line should be put in to backup your mysql folder.  Assuming you’ve been paying attention, all these files are in a non-default location to begin with (*wink*).

 

PS:  I like to call her cron, but her real name is “crontab”.  To list what tasks she has, you can use the command

crontab -l

Plug:: For more posts like this, check out blog.arizvi.ca

Thank you, Christian.

Like
Reply

Good article on one of the most vital Linux components ever! Good job!

Like
Reply

To view or add a comment, sign in

More articles by Ali R.

Others also viewed

Explore content categories