Project: a repeatable backup workflow
Write a small Bash script, rehearse it and prepare a scheduling plan.
By the end of this lesson
- Use quoted variables and explicit error handling in Bash
- Produce a timestamped archive with restrictive permissions
- Verify a restore and describe a sensible scheduling plan
Your environment: Requires Bash, tar and gzip. Execute with bash backup.sh, not sh backup.sh. No root required. Setup help
Automate a workflow you understand
Automation magnifies both good habits and mistakes. Before scheduling a backup, make it work interactively against a tiny practice directory. Give the script an explicit interpreter, use quoted paths and return a failure status when prerequisites are missing. A script that silently continues after an error can leave reassuring but empty output.
This example uses Bash with set -euo pipefail as a useful baseline, not as a replacement for reasoning about failures. The script checks its source directory explicitly and relies on tar returning a failure when archiving fails. It does not delete old backups or modify the source.
Make the operational contract visible
A scheduled job needs a known interpreter, an explicit path, a suitable account, a place for diagnostics and someone to notice failure. Cron often runs with a smaller environment than your interactive shell. Do not assume that your current directory or custom PATH will be present.
The project deliberately stops before installing a schedule. First run the script twice, identify the new archives and restore one. Then write a proposed schedule and recovery check in your notes. A production version would also need retention, monitoring, concurrency control and off-machine storage, reviewed for its data and workload.
See it in context
Worked example
A script should fail clearly when its source is missing.
#!/usr/bin/env bash
set -euo pipefail
source_dir="$HOME/training4linux/admin-project/source"
if [ ! -d "$source_dir" ]; then
printf "%s\n" "Source directory missing" >&2
exit 1
fiThis is a fragment illustrating a guard, not the entire backup program. The complete practice script is in the solution.
Your turn
Practical project
Create a working backup script for a small project directory, then prove restoration works.
Run commands in your own lab. This website displays instructions; the practice terminal below helps you rehearse the syntax.
1. Prepare your workspace
mkdir -p "$HOME/training4linux/admin-project/source" "$HOME/training4linux/admin-project/restore"
cd "$HOME/training4linux/admin-project"
printf "%s\n" "version=1" > source/settings.txt2. Complete the task
- Write backup.sh using the guard from the example, a private umask and a timestamped output filename.
- Run bash backup.sh and read its printed archive path.
- List and extract that archive into restore, then compare settings.txt.
- Write a scheduling plan naming the account, full script path, log destination and restore-check frequency. Do not install a cron job yet.
3. Check your result
- The script prints the path of a created archive and exits successfully.
- A missing source would produce a non-zero exit rather than an empty success report.
- Restored settings.txt matches the source.
- Your plan includes failure monitoring and storage beyond the source disk.
Need a hint?
Use a timestamp plus $$ for unique practice filenames. tar -C lets you archive relative to the source directory.
Reveal a worked solution
Compare this with your approach after trying the task. More than one valid solution may exist.
cat > backup.sh <<'BASH'
#!/usr/bin/env bash
set -euo pipefail
umask 077
source_dir="$HOME/training4linux/admin-project/source"
backup_dir="$HOME/training4linux/admin-project/backups"
if [ ! -d "$source_dir" ]; then
printf "%s\n" "Source directory missing" >&2
exit 1
fi
mkdir -p "$backup_dir"
archive="$backup_dir/backup-$(date +%Y%m%d-%H%M%S)-$$.tar.gz"
tar -czf "$archive" -C "$source_dir" .
printf '%s\n' "$archive"
BASH
archive=$(bash backup.sh) && tar -tzf "$archive" && tar -xzf "$archive" -C restore
diff source/settings.txt restore/settings.txtAfter the lab: No recurring task is installed. Keep the script and schedule proposal as your administration project.
Common mistakes to avoid
- Do not assume a script is production-ready because it works once in a lab.
- The website does not execute or schedule your shell script.
Check your understanding
Knowledge check
Answer all 3 questions correctly to pass. Retry as often as you need; your best score is kept.
Your lesson progress
Not started · Best score: 0% · Attempts: 0
Practical completion is self-reported. Progress is linked to a private cookie in this browser and stored on the site. Clearing that cookie loses access to it. Manage or erase progress.