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
| Word | What it means | Example |
|---|---|---|
| Backup | a copy of files, kept in case the originals are lost | |
| Archive | many files packed into one file | notes-backup.tar.gz |
| Schedule | to set a job to run at fixed times | |
| Cron | the service that runs jobs on a schedule | |
| Crontab | your 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
cdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a folder to protect
mkdir -p notesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Put an order in it
echo "Order 17 sent" > notes/orders.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Back it up by hand
tar -czf notes-backup.tar.gz notesShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Look inside the backup
tar -tzf notes-backup.tar.gzShown 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
# 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
echo '* * * * * tar -czf /home/student/auto-backup.tar.gz -C /home/student notes' > mycronShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Hand the file to cron
crontab mycronShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read your list of jobs
crontab -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
After a minute, look inside
tar -tzf auto-backup.tar.gzShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Remove your every-minute job
crontab -rShown 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.