Learn how to make your Linux server do boring jobs by itself. Build a nightly backup script and schedule it with cron, step by step.
Think of your server as a shop.
Every night, someone must:
If you do this by hand, one day you will forget.
Automation = hiring a night watchman.
He does the same job, at the same time, every night. He never forgets.
In Linux, that watchman is called cron.
Automation is not just a nice trick. It is changing how server work is done.
The US Bureau of Labor Statistics says systems administrators are increasingly automating routine tasks.
That is exactly what you will learn here.
Simple lesson: scripting is now a core skill, not a bonus.
| Word | Simple meaning |
|---|---|
| Automation | A script does the task for you, on a schedule |
| Script | A text file with commands, run top to bottom |
| Cron | The Linux alarm clock for commands |
| Cron job | One scheduled command inside cron |
| Idempotent | Safe to run twice. Nothing breaks |
Start with boring, repeated, low-risk tasks.
| Task | Why automate | Risk |
|---|---|---|
| Backups | You will forget. Losing data is the worst outcome | Low |
| Security updates | Servers get attacked fast when unpatched | Low |
| Disk and service checks | Catch problems before the site goes down | Low |
| Add or remove users | Same steps every time | Medium |
Anything destructive that you have not run by hand at least 3 times:
Rule: first by hand, then by script.
You write script
|
v
Cron reads schedule
|
v
Every night at 2:30 AM
|
v
Script runs and makes backup
|
v
Log file records what happened
A cron schedule has 5 fields, then the command:
30 2 * * * /usr/local/bin/backup-site.sh
| | | | |
| | | | +-- day of week (0-7, * = every day)
| | | +----- month (1-12, * = every month)
| | +-------- day of month (1-31, * = every day)
| +----------- hour (0-23)
+--------------- minute (0-59)
So 30 2 * * * means: minute 30, hour 2, every day = 2:30 AM daily.
The * means "every". Think of it like setting an alarm: "7:00, every day."
Why first? Cron only runs commands. So we need a script that does the actual backup.
What this script does:
Create the file:
sudo nano /usr/local/bin/backup-site.sh
Paste this:
#!/usr/bin/env bash
set -euo pipefail
SRC="/var/www/site"
DEST="/backups"
STAMP="$(date +%F)"
mkdir -p "$DEST"
tar -czf "$DEST/site-$STAMP.tar.gz" "$SRC"
# keep only the newest 7 archives
ls -1t "$DEST"/site-*.tar.gz | tail -n +8 | xargs -r rm --
Line by line:
| Line | Meaning |
|---|---|
set -euo pipefail |
Stop the script at the first error instead of continuing blindly |
SRC |
The folder you want to protect. Change it to your site's folder |
DEST |
Where backups are stored |
STAMP |
Today's date, like 2026-10-13 |
tar -czf |
Pack and compress the folder into one file |
| last line | Delete everything except the newest 7 backups |
Make it executable:
sudo chmod +x /usr/local/bin/backup-site.sh
Never schedule a script you have not run yourself.
sudo /usr/local/bin/backup-site.sh
ls -lh /backups
You should see a file like site-2026-10-13.tar.gz.
You may see a warning: Removing leading '/' from member names. This is normal. It is not an error.
If you see "Permission denied", you forgot sudo. The script writes to /backups, and normal users cannot do that.
Because our script needs root permission, we use root's crontab:
sudo crontab -e
Add this line at the bottom:
30 2 * * * /usr/local/bin/backup-site.sh >> /var/log/backup-site.log 2>&1
What the end part does:
>> /var/log/backup-site.log saves the output to a log file2>&1 saves errors in the same logCheck your jobs any time:
sudo crontab -l
Check the log the next morning:
cat /var/log/backup-site.log
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
Ubuntu will now install security patches by itself. Check the logs once a week:
ls /var/log/unattended-upgrades/
Two commands you can put inside a script:
df -h /
systemctl is-active nginx
The first shows free disk space. The second prints active if Nginx is running.
scp to copy it to another computer or server.What should I automate first? Backups. They are repetitive, easy to script, and not having one hurts the most.
Is cron safe to use? Yes, if you use full paths, save logs, and test by hand first. Never schedule a destructive command you have not run manually.
My cron job did not run. What now?
Check three things: the script is executable (chmod +x), the path in cron is the full path, and the log file for errors.
Can I use this on my own VM? Yes. Any Ubuntu server works, including a free Oracle Cloud VM.
Automation = script + schedule
Script = the job (backup)
Cron = the alarm clock
Format = minute hour day month weekday command
Rule 1 = test by hand first
Rule 2 = save a log
Rule 3 = restore once, copy off-server
Gunicorn + Nginx for Flask – run your Flask app like a real production server.
TechWithJuned · Learn → Execute → Build
💬 Questions & Discussion
Have a doubt? Ask below — no login needed.
Loading comments...