backup cron table

Are you backing up your cron table?

It’s Sunday morning, my parents are coming into town in an hour and we have a pretty full and fun day planned. I decide to check my email one more time to make sure I can give my whole day to the family.

Pingdom Alert: Incindent #xx for XXX, has been assigned to you.

Yikes, okay, settle down Phil, you’ve seen this before. Sometimes it’s a false alarm, sometimes MySQL goes outta control, etc.

So I SSH in and find that my password doesn’t work! Did I suffer some memory loss last night?!

Within 5 minutes I get an email from my hosting provider notifying me that my entire Cloud Instance was deleted accidentally. My face goes white and I run up to my office, slam the door and boot up the laptop.

Just then I get an email that they restored my cloud instance, but changed the password. The data is not recoverable however. Thankfully, I have a separate off-site backup of all files & data up to the night before.

My host offers to re-provision the entire server exactly the way I had it before, and they did so quickly (within 2 hours or so).  They did a great job and everything went smoothly until the next morning.

I noticed that my off-site backups weren’t working, nor were many of the automated scripts I had setup. I had forgotten to recreate my cron table. What’s worse is that I didn’t have a backup of it. Three days later, I’m still discovering scripts that I need to add back to the cron.

The Lesson

Back up your cron table! It’s easy and I’m surprised I had never before considered it.

Drop this simple command in your backup script:

crontab -l > /backMeUp/crontab.backup

 

But I have other users with cron tables that I need to back up.

That’s where the runuser command comes in. runuser will run a shell with the user that you provide it. What this means is that the user you are running the shell as will need to have write permissions on the location where you are placing the backup file.

For instance, in my example below, the user “lanasa” will need to have write permission on the backMeUp folder. The write permissions does not get “acquired” by the shell.

runuser -l lanasa -c 'crontab -l > /backMeUp/crontab.lanasa'

Why follow me on Twitter?

  • I tweet about new technologies, services or libraries I find interesting
  • Yeah, sometimes I'll post a pet-peeve or rant about something trivial
  • If I discover something that made my web development life easier, I share it
  • I'll shout out any handy tip that I think might be useful to other devs


Tagged .

Updated: 2014-06-23

Phil LaNasa follow us in feedly