Backup and restore crontabs

Here’s a thing I came up with: you’re administering a Linux system with 100 users circa and you’re moving to a new server, you can save crontabs  per user with this:

mkdir crontabz && cd crontabz; for user in `cat /etc/cron.allow`; do crontab -l -u $user > cron_$user; done

you will end up with a list of files cron_xxx, each one has the users’ cron. Hopefully your cron version will use the /etc/cron.allow file to control  user based access to crontab.

Now, once users in the new system are all in place you can copy the directory crontabz and then restore their cron with:

cd crontabz;  for file in `ls -m1`; do echo `basename $file`|sed -s 's/cron_//' >> temp ; done ; \ 

for user in `cat temp`; do  cat cron_$user | crontab -u $user -; done;

Optionally you can include an “rm -f temp” at the end to delete the file used to store user names.

I put the script on github here,


See also

comments powered by Disqus