Cyberlium

Linux › Module 6 › Lesson 5

BeginnerModule 6Lesson 5/6

Lab — Mini Automation Script

Build a backup script that copies files with a timestamp

25 min+40 XP3 quiz
Module progress5 of 6

Opening

Automate something real — starting tiny

You now know shebang, quoting, chmod +x, and exit codes. This lab writes a script you will actually run: $HOME/cyberlium-lab/sysinfo.sh prints whoami, pwd, and date — then an optional stretch copies a notes file into a timestamped backup under the same lab folder. That is the same pattern as later backup or log-collection scripts: a file, a shebang, execute bit, quoted paths, and a destination you own. Small and correct beats clever and destructive. You will not copy trees outside $HOME, touch anyone else's data, or "clean up" with rm -rf /.

1. What the script must do — and why those three lines

Create $HOME/cyberlium-lab if needed. Write sysinfo.sh with #!/bin/bash, then three commands: whoami (identity), pwd (where you are), and date (when you ran it). Those three lines are a mini "what machine, what directory, what time" header analysts paste at the top of notes. When you hand a report to yourself tomorrow, that header stops you from mixing WSL runs with VM runs or yesterday's cwd with today's.

chmod +x and run ./sysinfo.sh from that directory (or pass a quoted absolute path). The execute bit is a capability on a file you own — it is not privilege escalation. If the shell says Permission denied, check ls -l for the x bit and confirm you are not trying to execute a directory. If it says No such file, check pwd and the path you typed; do not invent sudo as a fix for a typo.

Optional stretch — still only under cyberlium-lab: redirect output into sysinfo-out.txt with >> so you keep a history, and copy notes.txt to backups/notes-YYYYMMDD-HHMMSS.txt using date +%Y%m%d-%H%M%S. Quoting the destination path keeps spaces from splitting the argument. Skipping quotes is how a "simple backup" becomes three broken partial paths and a confused ls.

2. Mechanics: shebang, PATH, and relative ./

The shebang #!/bin/bash tells the kernel which interpreter to launch when you run ./sysinfo.sh. Without +x, the kernel will not treat the file as an executable entry point — you can still run bash sysinfo.sh because bash reads the file as a script argument. Both paths are valid; ./ with +x is the habit you want for tools you own.

Relative ./sysinfo.sh means "in this directory," not "somewhere on PATH." That is deliberate safety: you do not want a same-named script from /tmp to win because PATH was polluted. which sysinfo.sh should usually find nothing until you install a package that ships that name. Your lab script lives in cyberlium-lab and you invoke it by path.

date formats matter for backups. date alone is human-readable. date +%Y%m%d-%H%M%S is sortable and filename-safe on most filesystems. Embedding that expansion in a destination path is how you avoid overwriting yesterday's copy. Overwriting is silent data loss — worse than a loud error when you thought you had a second copy.

3. Why this lab is a backup rehearsal, not a root ritual

Real backup scripts add checksums, retention, and restore tests. This lab teaches the skeleton: create a known directory, write a script with a shebang, make it executable, run it, and prove output. If you cannot do that reliably under $HOME/cyberlium-lab, you are not ready to automate anything that touches /etc or production mounts.

Security angle: a script that copies secrets into world-readable backups is a self-inflicted leak. Keep practice files non-secret, or chmod 600 the backup directory after you create it. Never paste API keys into notes.txt "just for the lab." The habit you rehearse is the habit that will ship.

Ethics and scope stay absolute: only your VM, WSL, or spare machine; only paths under $HOME/cyberlium-lab for this exercise; no scanning other hosts; no chmod 777 /; no rm -rf /. Automation multiplies mistakes — so the lab keeps the blast radius tiny on purpose.

4. Wrong vs right: automation that respects your own disk

Failure mode — "I wrote a backup script" means opposite things. Right is owned paths and reversible steps:

  • Wrong

    Paste a forum script that rsyncs / to a random USB, runs as root by default, skips quoting, and deletes "old" backups with rm -rf $DEST/* where DEST was empty because a typo expanded wrong. Or chmod 777 the script "so it works." Or write secrets into the lab notes file. Or run the script against /etc "to feel real" on a machine shared with family photos and no snapshot.

  • Right

    Create $HOME/cyberlium-lab, write sysinfo.sh with #!/bin/bash and three safe commands, chmod +x on that file only, run ./sysinfo.sh, and confirm whoami/pwd/date print. Optional: mkdir -p backups, copy a non-secret notes.txt to a timestamped name with quoted paths, leave originals intact, and keep modes tight (600/700) if anything sensitive ever appears. Snapshot the VM first if you are nervous.

5. Practical: build and run sysinfo.sh on your machine

Run these on YOUR Linux VM, YOUR WSL distro, or YOUR spare box only. Create the lab folder, write the script with nano or a here-doc, chmod +x, execute, and optionally create a timestamped copy of a practice notes file. Do not point cp at directories outside cyberlium-lab for this lesson.

Safe mini automation under $HOME/cyberlium-lab

# YOUR VM / WSL / spare machine only — stay under $HOME
mkdir -p "$HOME/cyberlium-lab/backups"
cd "$HOME/cyberlium-lab" || exit 1

# Write the script (nano is fine if you prefer)
cat > sysinfo.sh << 'EOF'
#!/bin/bash
set -euo pipefail
echo "whoami: $(whoami)"
echo "pwd:    $(pwd)"
echo "date:   $(date)"
EOF

chmod +x sysinfo.sh
./sysinfo.sh

# Optional stretch: practice notes + timestamped copy (no secrets)
echo "lab notes — non-secret" > notes.txt
STAMP="$(date +%Y%m%d-%H%M%S)"
cp -v notes.txt "backups/notes-${STAMP}.txt"
ls -la backups/

# NEVER for "cleanup" or "realism":
# rm -rf /
# chmod 777 /
# sudo cp -a /etc "$HOME/cyberlium-lab/"

Mission: ship a runnable sysinfo.sh

1) On your own VM or WSL, create $HOME/cyberlium-lab and write sysinfo.sh with #!/bin/bash plus whoami, pwd, and date (labels optional). 2) chmod +x sysinfo.sh and run ./sysinfo.sh. Confirm three lines of output. 3) Stretch: copy a non-secret notes.txt into backups/ with a date +%Y%m%d-%H%M%S stamp using quoted paths. 4) Write one sentence: why ./sysinfo.sh is safer than dropping a same-named script on PATH from /tmp.

Stuck? Ask Cyberlium AI Mentor

If chmod +x or ./sysinfo.sh fails, ask Cyberlium AI Mentor for a hint — not a paste-ready rewrite. Try: "Hint only: Permission denied on ./sysinfo.sh — what should ls -l show, and should I use sudo?" No spoilers; you should still fix the bit and path yourself.

You now have a real automation skeleton: shebang, execute bit, quoted paths, and a timestamped copy under a folder you own. That is the bridge from "I can type bash" to "I can leave a reliable tool for myself." Next up — Quiz — Linux Fundamentals Final — locks Module 6 and the whole Topic 2 map.

Knowledge Check

1

APPLY: Your sysinfo.sh exists in $HOME/cyberlium-lab but ./sysinfo.sh says Permission denied. ls -l shows -rw-r--r--. Best first fix on a file you own?

Multiple choice

Knowledge Check

2

APPLY: You want a second copy of notes.txt that will not overwrite yesterday's backup. Best destination pattern under cyberlium-lab?

Multiple choice

Knowledge Check

3

APPLY: True or False: Because your backup script works on your Ubuntu VM, you may run the same script to copy another student's home directory on a shared lab server without asking.

True or False

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)