Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
usage() { echo "Usage: $0 SITE_DIR DEST_DIR" >&2; exit 2; }
[[ $# -eq 2 ]] || usage
src=$1
dest=$2
[[ -d "$src" ]] || { echo "No such folder: $src" >&2; exit 1; }
tmp=$(mktemp -d)
trap 'rm -rf -- "$tmp"' EXIT
tar -czf "$tmp/site.tar.gz" -C "$src" .
mkdir -p -- "$dest"
mv -- "$tmp/site.tar.gz" "$dest/site-$(date +%F).tar.gz"
echo "Backup written to $dest"shellcheck backup-site.sh
bash -n backup-site.shStep by step
- Strict mode — stop at the first problem instead of continuing in a broken state:
| Line | Effect |
|---|---|
set -e | Exit when a command fails. |
set -u | Exit when a variable is not set (no silent empty $DEST). |
set -o pipefail | A pipe fails if any part fails (not only the last). |
IFS=$'\n\t' | Do not split words on spaces. |
- Quote every variable —
"$file","$@". Without quotes, a name with a space becomes two names, and an empty variable can turnrm -rf "$dir"/intorm -rf /. Use--before file arguments so names starting with-are not read as options. - Check inputs early (as in the template): number of arguments, folders exist, values look right. Exit code
2for wrong usage,1for errors. - Temp files and clean-up:
mktemp(never fixed names like/tmp/x), andtrap … EXITremoves them on every exit — success, error, or Ctrl+C. - Be careful with dangerous commands:
rm -rf -- "${dir:?dir is empty}/cache"${dir:?msg} stops the script if dir is empty — rm never runs on /cache.
- Readable and debuggable: functions for steps,
localvariables, logs to stderr, and a debug switch:
bash -x backup-site.sh /var/www/shop /backup- ShellCheck catches the classic bugs (missing quotes,
cdwithout check, wrong tests) — run it in CI too:
dnf -y install ShellCheck
apt-get install -y shellcheck
shellcheck -S warning scripts/*.sh- When not to use Bash: JSON/YAML handling, APIs, more than ~150 lines or complex logic → Python is clearer and safer.
Check that it worked
shellcheck has no warnings; the script stops with a clear message on wrong input (./backup-site.sh alone, a missing folder); the temp folder is gone after success and after a failure; and the exit code is right (echo $?).
Common problems
| Problem | Cause | Fix |
|---|---|---|
unbound variable | set -u and a variable that may be empty. | ${VAR:-default}, or check it first. |
Script stops at grep that finds nothing | grep returns 1 when there is no match; set -e stops. | grep … || true, or test it in if. |
| Works by hand, fails in cron | Different PATH/environment. | Full paths or set PATH at the top. |
cd failed and the next command ran in the wrong folder | No check after cd. | cd "$dir" || exit 1 (strict mode also helps). |
$'\r': command not found | Windows line endings. | dos2unix script.sh. |
Secrets visible in ps or logs | Passed as arguments or printed with -x. | Read from files/env; turn off set -x around secrets. |
Go further
- Command pages: bash, set, test, read.
- Next how-tos: Schedule a job with cron, Read JSON in the shell with jq, Run one command on many servers.
- Learn it: TLCL — Defensive scripts and set -e, TLCL — shellcheck, TLCL — Traps and temp files, RH134 — Debug scripts.
- Practise: Lab: write admin scripts.
1What does set -o pipefail change?
A pipeline fails when any command in it fails, not only the last one.
2How do you make sure rm -rf "$dir/cache" never runs with an empty dir?
Use ${dir:?message} — the script stops if dir is empty or unset.