Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Making a backup run by itself (cron)

After this lesson: I can schedule a backup.

50 min lesson10 commandsLinux administration lab: users, permissions, cron and systemd in your browser

The bookings nobody backed up

A hair salon keeps its bookings on one small computer. One morning the computer will not start, and a month of bookings is gone.

The owner meant to copy the files every night. Some nights she forgot. A machine never forgets.

By the end of this lesson

Note

I can schedule a backup.

Key words

WordWhat it meansExample
Backupa copy of files, kept in case the originals are lost
Archivemany files packed into one filenotes-backup.tar.gz
Scheduleto set a job to run at fixed times
Cronthe service that runs jobs on a schedule
Crontabyour own list of cron jobs, one a line

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top and wait for the machine. Press the Runs in order. The first one takes you to your home folder.

tar packs a folder into one file

A backup is easier to keep and move as one file. tar packs a folder into an archive.

tar -czf name.tar.gz folder creates (c) a squeezed (z) archive with that file name (f). tar -tzf name.tar.gz lists what is inside without unpacking it. Always look inside a backup before you trust it.

Go to your home folder

Command
cd

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a folder to protect

Command
mkdir -p notes

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Put an order in it

Command
echo "Order 17 sent" > notes/orders.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Back it up by hand

Command
tar -czf notes-backup.tar.gz notes

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Look inside the backup

Command
tar -tzf notes-backup.tar.gz

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A cron line: five times, then a command

A cron line starts with five fields: minute, hour, day of the month, month and day of the week. The command comes after them.

A star * means every one. A number means only that one. In the minute field, */15 means every fifteen minutes.

Three cron lines for a small shop

Code: bash
# minute hour day-of-month month day-of-week  command
0 22 * * *     tar -czf /home/sam/shop-backup.tar.gz -C /home/sam shop
30 7 * * 1     /usr/local/bin/weekly-sales.sh
*/15 * * * *   /usr/local/bin/check-stock.sh

A common mistake

Warning

Many people write the hour first, because we say "half past seven" as 7:30. Cron reads the minute first. So 7 30 * * * is not valid, and 30 7 * * * is 07:30. Read the line aloud as "minute 30, hour 7".

Give the line to cron

The Runs below write one line into a file, then hand the file to cron with crontab. The line runs every minute, so you can see it work. A real backup would run once a night.

Cron runs the job on its own, not in your terminal. So the line uses full paths, and -C /home/student tells tar where to start.

Write the cron line into a file

Command
echo '* * * * * tar -czf /home/student/auto-backup.tar.gz -C /home/student notes' > mycron

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Hand the file to cron

Command
crontab mycron

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read your list of jobs

Command
crontab -l

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

After a minute, look inside

Command
tar -tzf auto-backup.tar.gz

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Remove your every-minute job

Command
crontab -r

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

A file in the system folder /etc/cron.d needs one more field: the user to run as, between the five times and the command.

On paper, write a line for that folder. It runs /usr/local/bin/weekly-sales.sh as root at 18:45 every Friday. Friday is 5 in the last time field.

About this lab and its source: Linux administration lab: users, permissions, cron and systemd in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.