$_ CLI In Depth
Learning progress 0%
Build · Packages, Services & Scheduled Jobs

Build: A Scheduled Backup Rotator

Write rotate.sh to the spec, then run it three times and check that only the newest two archives remain.

~30 min Intermediate 2 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: Write rotate.sh to the spec

Create rotate.sh in /workspace. It takes a source directory, a destination directory, and a keep count. It creates a time-stamped gzip tar archive, and it deletes archives beyond the keep count only after tar succeeds.

Write it with a heredoc, using the reference logic from the unit.

BASH13 lines
cat > rotate.sh <<'EOF'
#!/bin/bash
if [ "$#" -ne 3 ]; then echo "usage: rotate.sh SOURCE DEST KEEP" >&2; exit 2; fi
src=$1; dest=$2; keep=$3
mkdir -p "$dest" || exit 1
name="backup-$(date +%Y%m%d-%H%M%S-%N).tar.gz"
tar -czf "$dest/$name" -C "$src" . || exit 1
i=0
for f in $(ls -1 "$dest"/backup-*.tar.gz | sort -r); do
  i=$((i + 1))
  if [ "$i" -gt "$keep" ]; then rm -f -- "$f"; fi
done
EOF

Step 2: Run three backups and keep two

Create a source directory named src with one file in it. Run rotate.sh three times with keep set to 2, with a one-second pause between runs, so that the archives get different names. Then count the archives in backups.

BASH5 lines
mkdir -p src && echo data > src/a.txt
bash rotate.sh src backups 2; sleep 1
bash rotate.sh src backups 2; sleep 1
bash rotate.sh src backups 2
ls backups | wc -l

The count printed at the end should be 2.

Files

  • rotate.sh: the finished script.
  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

rotate.sh

BASH12 lines
#!/bin/bash
# usage: rotate.sh SOURCE_DIR DEST_DIR KEEP
if [ "$#" -ne 3 ]; then echo "usage: rotate.sh SOURCE DEST KEEP" >&2; exit 2; fi
src=$1; dest=$2; keep=$3
mkdir -p "$dest" || exit 1
name="backup-$(date +%Y%m%d-%H%M%S-%N).tar.gz"
tar -czf "$dest/$name" -C "$src" . || exit 1
i=0
for f in $(ls -1 "$dest"/backup-*.tar.gz | sort -r); do
  i=$((i + 1))
  if [ "$i" -gt "$keep" ]; then rm -f -- "$f"; fi
done

walkthrough.sh

BASH30 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Write rotate.sh to the spec
cd /workspace
cat > rotate.sh <<&#039;EOF&#039;
#!/bin/bash
# usage: rotate.sh SOURCE_DIR DEST_DIR KEEP
if [ "$#" -ne 3 ]; then echo "usage: rotate.sh SOURCE DEST KEEP" >&2; exit 2; fi
src=$1; dest=$2; keep=$3
mkdir -p "$dest" || exit 1
name="backup-$(date +%Y%m%d-%H%M%S-%N).tar.gz"
tar -czf "$dest/$name" -C "$src" . || exit 1
i=0
for f in $(ls -1 "$dest"/backup-*.tar.gz | sort -r); do
  i=$((i + 1))
  if [ "$i" -gt "$keep" ]; then rm -f -- "$f"; fi
done
EOF

# Step 2: Run three backups and keep two
# (the lab prepares this first)
# cd /workspace
# mkdir -p src && echo data > src/a.txt
# 
cd /workspace
mkdir -p src && echo data > src/a.txt
bash rotate.sh src backups 2; sleep 1
bash rotate.sh src backups 2; sleep 1
bash rotate.sh src backups 2

Lessons this build uses