Beginner 7 min read

How to Automate Server Tasks with Cron – Beginner Backup Guide

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.

🧠 Easy Analogy (Must Read First!)

Think of your server as a shop.

Every night, someone must:

  • Make a copy of the ledger (backup)
  • Check that the shutters work (updates)
  • Count the cash (disk space)

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.


📘 What You Will Learn

  • Which server tasks to automate first
  • What cron is and how to read its schedule
  • How to write a simple backup script
  • How to schedule it to run every night
  • 3 habits that make a backup actually useful

💡 Why Learn This?

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.


🔑 Words You Should Know

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

🎯 What Should You Automate First?

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

⚠️ What NOT to automate yet

Anything destructive that you have not run by hand at least 3 times:

  • Deleting data
  • Changing firewall rules on a live server
  • Restoring a database over production

Rule: first by hand, then by script.


🗺️ How It Works

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

⏰ Understanding the Cron Schedule

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."


🛠️ Step 1: Write the Backup Script

Why first? Cron only runs commands. So we need a script that does the actual backup.

What this script does:

  1. Copies your website folder into one compressed file
  2. Puts today's date in the file name
  3. Keeps only the newest 7 backups, so your disk does not fill up

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

🧪 Step 2: Test It by Hand

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.


📅 Step 3: Schedule It with Cron

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 file
  • 2>&1 saves errors in the same log

Check your jobs any time:

sudo crontab -l

Check the log the next morning:

cat /var/log/backup-site.log

🔄 Other Tasks You Can Automate

Security updates (Ubuntu)

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/

Disk and service checks

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.


✅ 3 Habits That Make a Backup Real

  1. Restore it once. A backup you never restored is just a guess.
  2. Copy it off the machine. A backup on the same disk dies with the disk. Use scp to copy it to another computer or server.
  3. Make it safe to run twice. Running the script twice in one day should overwrite, not fail.

🗓️ Simple Weekly Checklist

  • Is last night's backup there? Is the size normal?
  • Did the update log show any failure?
  • Is free disk space okay?
  • Once a month, test a restore.

❓ FAQ

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.


🎯 Final Summary

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

📚 References


📌 Next Tutorial

Gunicorn + Nginx for Flask – run your Flask app like a real production server.


TechWithJuned · Learn → Execute → Build

✍️ Written by SI Systems Team

💬 Questions & Discussion

Have a doubt? Ask below — no login needed.

Loading comments...