Organising cron jobs. How I manage my backup scripts
Using Cron to back up your files (and keep your .sh scripts organised)
I use cron mainly for backups, though you can also use it to run all sorts of jobs on a schedule. I recently added a job to keep an eye on my IP and warn me if it changes, so that I don't get locked out by my own fail2ban.
Cron is one of those Linux tools that just quietly does its thing in the background
This isn't a how-to-do-it post; in fact, I'm crap at that. It's just what I realised one day, using cron jobs on three different PCs and getting muddled sometimes.
Why Organising Scripts Matters
Cron is one of those Linux tools that just quietly does its thing in the background
This isn't a how-to-do-it post; in fact, I'm crap at that. It's just what I realised one day, using cron jobs on three different PCs and getting muddled sometimes.
Why Organising Scripts Matters
When you start writing cron jobs, it’s easy to end up with a mess of .sh files. So I put them all in one folder. Also, at first, I had a script called rsync_nextcloud.sh. Months later, I couldn’t remember where it was syncing to without opening the script to see the correct path.
So I renamed it to: rsync_nextcloud_to_BackUp2.sh Okay, it's not rocket science, but I didn't think about that then .....
Much clearer — BackUp2 is one of my backup HDDs. I should have named it properly from the beginning!
I keep all my shell scripts in one folder on each computer. Mine are for example: ~/Documents/My_SH_files/
You can create this folder anywhere, name it whatever you like.
You can create this folder anywhere, name it whatever you like.
Inside my My_SH_files folder, I have scripts like:
- rsync_nextcloud_to_Backup2.sh → copies my Nextcloud files to my named BackUp2 HDD
- ip_change_alert.sh → checks if my IP changes and alerts me by mail
Making Your Script Executable
Once you’ve written your .sh file, you need to make it executable: chmod +x /path/to/your/script.sh
Adding Jobs to Cron
Once you’ve written your .sh file, you need to make it executable: chmod +x /path/to/your/script.sh
Adding Jobs to Cron
To actually run your script on a schedule, you need to edit your crontab.
Use terminal:
crontab -e # runs jobs as your user
sudo crontab -e # runs jobs as root
👉 Use your user crontab for personal/home directory scripts.
👉 Use root’s crontab if the job needs access to system folders or external drives with root permissions.
I cheat on my Arch PC they are all in sudo crontab -e
Here are two of mine:
10 10 * * 6 /home/xxx/Documents/My_SH_files/rsync_nextcloud_to_Backup2.sh
*/30 * * * * /home/xxx/Documents/My_SH_files/ip_change_alert.sh
For info: The numbers at the start are the schedule. If you’re not sure how to set them, check out this site https://crontab.guru/
Don't forget. If you rename the sh file, you must rename the cron job as well; otherwise, it won't run.
ChatGPT
To save some work. Here's a trick for you. Run a simple ls command in terminal
~/D/My_SH_files> ls
Then, in crontab -e copy the results
Copy both the results into ChatGPT and ask it to compare the titles
ChatGPT "Can you compare my sh files to my cron jobs to check I haven't made any errors in the titles?"
I got this result : All scripts you listed have a corresponding cron job, no mismatches.
Keeping an eye on my jobs
crontab -e # runs jobs as your user
sudo crontab -e # runs jobs as root
👉 Use your user crontab for personal/home directory scripts.
👉 Use root’s crontab if the job needs access to system folders or external drives with root permissions.
I cheat on my Arch PC they are all in sudo crontab -e
Here are two of mine:
10 10 * * 6 /home/xxx/Documents/My_SH_files/rsync_nextcloud_to_Backup2.sh
*/30 * * * * /home/xxx/Documents/My_SH_files/ip_change_alert.sh
For info: The numbers at the start are the schedule. If you’re not sure how to set them, check out this site https://crontab.guru/
Don't forget. If you rename the sh file, you must rename the cron job as well; otherwise, it won't run.
ChatGPT
To save some work. Here's a trick for you. Run a simple ls command in terminal
~/D/My_SH_files> ls
Then, in crontab -e copy the results
Copy both the results into ChatGPT and ask it to compare the titles
ChatGPT "Can you compare my sh files to my cron jobs to check I haven't made any errors in the titles?"
I got this result : All scripts you listed have a corresponding cron job, no mismatches.
Keeping an eye on my jobs
On each of my computers, I also use a Conky script that shows me my current crontab jobs right on the desktop. Nothing special, just so that I have a visual reminder of what’s running and when.
Final Thoughts
With a bit of organisation — keeping your scripts in one place, naming them clearly, and setting up your crontab properly — cron becomes a powerful, easy backup tool.
Simple, right? I wish I'd done this from the start :)
PS;
PS;
I've already written a post a couple of years ago on my WordPress blog explaining how to use cron itself: Although re-reading it, I see that maybe I need to update it a little :)
👉 https://www.minty95.com/using-cron-to-back-up-files-or-folders/
👉 https://www.minty95.com/using-cron-to-back-up-files-or-folders/