How to backup and restore crontab
Hi folks,
Today I had to come up with a solution for a customer using AWS/EC2 to make their crontab resilient to server re-builds in case it goes down by autoscaler or other situations. That’s why I created a script that is called daily that backups the crontab of a specific user to a file in an EBS mounted volume maintaining a 7 days library just in case
:
#!/bin/bash
find /data -maxdepth 1 -name ‘bkp-crontab-*’ -mtime 7 -exec rm {} \;
crontab -l -u YOUR_USER_GOES_HERE > /data/bkp-crontab-`date +%Y-%m-%d`
Then to recover the last backup of crontab for the user you can put this in your server script when you are building it:
cat `ls -tr /data/bkp-crontab-* | tail -1` | crontab -u YOUR_USER_GOES_HERE -
This will load the last backup file in the users crontab.
I hope this helps you to have your crontabs backed up
Cheers!
