Linux on call · Chapter 28 of 30

Project: the backup script

By the end of this chapter you can write a backup script that names each archive by its date, keeps only the newest few, logs what it did, and runs every night on its own. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Shell scripting
  • 5 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

Ravi opened Tapri's backups/ folder:

ls backups
tapri-2026-09-26-020000.tar.gz	tapri-2026-09-28-020000.tar.gz
tapri-2026-09-27-020000.tar.gz	tapri-2026-09-29-020000.tar.gz

The newest is from the 29th of September. "Arjun made them by hand," he says, "when he remembered." Bunty suggests a calendar reminder. A reminder still needs someone awake at 2 am to type the right tar line, and nobody ever deletes the old archives, so one day they fill the disk. Kabir wants a script that does it all, and cron to run it. His NOTE.txt says what the script must do.

You know every piece already: tar (chapter 12), pipes (chapter 8), cron (chapter 22) and strict mode (chapter 27). This chapter puts them into one script.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Write backup.sh: a dated archive, without node_modules

    Write a script that backs up a folder into one dated archive.

    1. Read NOTE.txt. Make backup.sh in the chapter folder. Line one is the bash shebang. Turn on strict mode.

    2. The script gets two folders: ./backup.sh SRC DEST. SRC is the folder to back up. DEST is where the archive goes.

    3. Make a compressed archive named DEST/tapri-YYYY-MM-DD-HHMMSS.tar.gz, with today's date and the time now.

    4. Put the files of SRC in it, but leave out node_modules. Make the script executable.

    To pass: the Check runs the script from another folder, on a test app. It must end with exit code 0. The new archive's name starts with tapri- and today's date and ends in .tar.gz. The archive has the app's files, like menu.txt and server.js, and nothing from node_modules.

  2. Task 2

    Keep only the three newest backups in DEST

    Make backup.sh delete old backups, so the disk does not fill up.

    1. After the script makes the new archive, list the tapri-*.tar.gz files in DEST, newest first.

    2. Keep the first three: the new one and the two before it.

    3. Delete the older ones.

    To pass: the Check gives the script a DEST that already has four old backups. After the run, DEST has exactly three tapri-*.tar.gz files: the new one and the two newest old ones. Other files in DEST, like backup.log, stay.

  3. Task 3

    Write each backup in DEST/backup.log

    Make backup.sh write one line in a log file each time it runs.

    1. At the end of the script, print one line: the date and time, the source folder, and the new archive's path.

    2. Add that line to the end of DEST/backup.log. Do not replace the file.

    To pass: after a run, the last line of DEST/backup.log has the new archive's file name in it. The lines from earlier runs stay.

  4. Task 4

    Back up the app into backups/ now

    Now back up the real app with your script.

    1. Do this when tasks 1 to 3 pass on copies. Every run deletes old archives.

    2. In the chapter folder, run ./backup.sh app backups.

    3. Run ls backups, and look inside today's archive.

    To pass: backups/ has exactly three tapri-*.tar.gz files, and at least one is from today. After one run, that is today's and the ones from 28 and 29 September. Today's newest archive has menu.txt and nothing from node_modules. backups/backup.log names today's archive.

  5. Task 5

    Run the backup every night at 02:30 with cron

    Make cron run your backup every night, as NOTE.txt asks.

    1. Add a line to your crontab that runs at 02:30 every night.

    2. In it, write the script's full path: /workspace/chapters/28-backup-script/backup.sh.

    3. After it, write the full paths of the app folder and the backups folder.

    4. Keep the lines that are already in your crontab.

    To pass: crontab -l shows a line that runs at 02:30 every night and has /workspace/chapters/28-backup-script/backup.sh in it.

What you will learn

  • A date in a name: date +FORMAT, inside $( ).
  • A clean archive: tar with -C and --exclude.
  • Keep the newest few: ls -1t, tail -n +N and xargs -r.
  • A log, and a nightly run: >> with a timestamp, and a cron line with absolute paths.

More in Shell scripting

Build "Project: the backup script" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.