Master Linux Cron: Schedule Automated Backups with Easy Step‑by‑Step Guide
Learn how to use Linux's built‑in Cron scheduler to create, configure, and run automated backup jobs, covering crontab syntax, time fields, script creation, permission setup, and redirection of output to /dev/null, with step‑by‑step commands and code examples.
Using Cron, you can schedule almost any type of job on Linux, such as backing up a specific directory to an external drive.
When using Linux, you can create a powerful, flexible, and automated environment to meet any purpose.
Cron is one of the tools for automation. With Cron you can schedule virtually any type of job on Linux. For example, you can write a bash script to back up a directory to an external drive and create a cronjob so the script runs automatically at the desired time.
Almost every Linux distribution installs Cron by default, so you can automate tasks without installing anything extra.
Cron How It Works
Cron relies on crontab files stored in /var/spool/cron. Do not edit these files manually; use the provided tool to edit them.
Each user on a Linux system has their own crontab file. Any user can create a crontab and view their current cron jobs with: crontab -l The output shows the contents of the crontab file, including any custom cron jobs you have created.
Cron uses five time and date fields:
Minute (0‑59)
Hour (0‑23)
Day of month (1‑31)
Month (1‑12)
Day of week (0‑6, where 0 or 7 is Sunday)
The format is: M H D MO DW Where M is minute, H is hour, D is day of month, MO is month, and DW is day of week.
Example: to run a backup script every Sunday at 23:00, use: 0 23 * * 0 The asterisk means “every” for that field. If you want the job to run every Saturday at 23:59, use: 59 23 * * 6 Or you can write the day of week as a name: 59 23 * * Saturday Adding a Cronjob
First, create a backup script (saved as sat_backup.sh):
#!/bin/bash
# What you want to backup.
backup_files="/home/$USER/Documents"
# Where you want to backup to.
dest="/backup"
# Create an archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
# Backup the files using tar.
tar czf $dest/$archive_file $backup_filesCreate the destination directory and set permissions:
sudo mkdir /backup sudo chmod -R u+w /data sudo chown -R $USER:$USER /dataMove the script to /usr/local/bin and make it executable:
sudo mv sat_backup.sh /usr/local/bin sudo chmod u+x /usr/local/bin/sat_backup.shTest the script: sat_backup.sh Now create the cronjob: crontab -e If this is your first time, choose an editor (Nano is recommended). Add the following line at the bottom:
0 23 * * 0 /usr/local/bin/sat_backup.sh > /dev/null 2>&1The /dev/null 2>&1 part redirects both standard output and standard error to the null device, preventing the job from failing silently if any output is produced.
Save the file (Ctrl+X in Nano) and verify the cronjob: crontab -l You should see the new entry, which will run every Sunday at 23:00 and create a backup file in /data.
This is the full functionality of Linux cronjobs, a useful tool for Linux users and administrators.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
