f in x
Bash Server Automation — Backup, Deploy and Monitoring That Actually Work
> cd .. / HUB_EDITORIALE
Sistemi Operativi & Sicurezza

Bash Server Automation — Backup, Deploy and Monitoring That Actually Work

[2026-06-29] Author: Ing. Calogero Bono
Zenithby Meteora Web The operating system for your business. Social, clients, bookings and invoices in one platform. Gyms, barbers, professionals. Discover Zenith Free demo · no card

Your server is in production. You do backups by hand when you remember. You deploy via FTP and hope nothing breaks. Monitoring? If disk fills up at 3 AM, you find out the next morning when the site is already down. That's not server management—it's gambling. We at Meteora Web see it every day: businesses losing data, clients, and money because Bash server automation is seen as a luxury. It's not. It's a defense line that costs nothing in licenses and pays for itself at the first crash. Let's get straight to the point: three scripts that change your day.

Why Automate Backup and Deploy with Bash Instead of Using GUI Tools?

Every time we open a console on a new server, the first thing we check is whether a cron job for backups exists. Half the time it doesn't. The reason is simple: many think they need a panel, a paid plugin, or a cloud service. The truth is that Bash, rsync, tar, and cron do everything, for free, with full control. No fees, no third-party dependencies, no hostage data.

We've automated dozens of client servers with scripts that start from lines of code you can write in ten minutes. The payoff? If a server crashes, you recover data in a few steps. If a deploy breaks everything, you roll back with one command. If disk fills up, you find out before it becomes a problem.

Real example: an e-commerce client had images weighing several MB. By optimizing them with an automated script, we reduced weight by 60% without quality loss and scheduled incremental backups every night. Cost? Zero. Result? Lighter disk, faster restore.

Sponsored Protocol

How to Set Up an Automated Backup with Bash and Cron

Start with a basic script for database and file backup. Save it as /usr/local/bin/backup.sh, make it executable (chmod +x /usr/local/bin/backup.sh), and add it to crontab.

#!/bin/bash
# Meteora Web automated backup
# Configuration
BACKUP_DIR="/var/backups/$(date +%Y-%m-%d)"
DB_NAME="mydb"
DB_USER="root"
DB_PASS="secure_password"
SITE_DIR="/var/www/mysite"

# Create daily directory
mkdir -p "$BACKUP_DIR"

# Database backup
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > "$BACKUP_DIR/db.sql.gz"

# File backup (exclude cache and logs)
tar -czf "$BACKUP_DIR/files.tar.gz" \
    --exclude="$SITE_DIR/wp-content/cache" \
    --exclude="$SITE_DIR/var/log" \
    "$SITE_DIR"

# Remove backups older than 7 days
find /var/backups/ -type d -mtime +7 -exec rm -rf {} \;

echo "Backup completed: $(date)"

What it does: creates a date-stamped folder, exports the database, compresses site files (excluding cache and logs), deletes backups older than 7 days. Immediate actions: replace variables with your data, test with bash backup.sh, then add 0 3 * * * /usr/local/bin/backup.sh to crontab (runs every night at 3 AM).

Incremental Backup with rsync to Save Space

If the site is large, a full backup every day can fill the disk. Use rsync to copy only changed files. Here's a variant for remote backup to a second server or NAS:

Sponsored Protocol

#!/bin/bash
# Incremental rsync backup
SRC="/var/www/mysite"
DEST="user@backup-server:/backups/mysite/"

rsync -avz --delete --exclude='wp-content/cache' --exclude='.git' "$SRC/" "$DEST"

# Separate database backup
mysqldump -u root -p$DB_PASS $DB_NAME | gzip | ssh user@backup-server "cat > /backups/mysite/db_$(date +%Y-%m-%d).sql.gz"

Benefit: first run copies everything, subsequent runs only differences. With --delete, files deleted locally are also removed on backup. Warning: set up passwordless SSH key for automatic access.

How to Automate Deploy with Bash to Avoid “Works on My Machine”?

How many times have you uploaded a file via FTP only to realize you overwrote the wrong one? Or did a manual deploy and forgot to update dependencies? We use a Bash script that pulls the latest version from git, installs dependencies, runs migrations, clears cache, and keeps a history of every release. If something goes wrong, a rollback is one command away.

Deploy Script with Rollback

#!/bin/bash
# Meteora Web deploy script
set -e  # Exit on first failure

APP_DIR="/var/www/mysite"
RELEASE_DIR="$APP_DIR/releases/$(date +%Y%m%d%H%M%S)"
CURRENT_LINK="$APP_DIR/current"
REPO_URL="git@github.com:your/mysite.git"
BRANCH="main"

echo "Cloning repository..."
git clone --depth 1 --branch $BRANCH $REPO_URL $RELEASE_DIR

echo "Installing dependencies..."
cd $RELEASE_DIR
composer install --no-dev --optimize-autoloader  # for Laravel/PHP
# npm ci --production  # for frontend

echo "Running database migrations..."
php artisan migrate --force

echo "Updating symbolic link..."
ln -sfn $RELEASE_DIR $CURRENT_LINK

echo "Clearing application cache..."
php artisan cache:clear
php artisan config:cache

echo "Deploy completed: $(basename $RELEASE_DIR)"

How to roll back: the current symlink points to the latest release. To go back: ln -sfn /var/www/mysite/releases/20250301120000 current. Keep the last 5 releases and delete older ones with a cron job.

Sponsored Protocol

Deploy to Multiple Servers (Without Ansible)

If you have several servers, you don't need a complex orchestrator. Use ssh and parallel to run the same command on all nodes:

#!/bin/bash
SERVERS=("web1.example.com" "web2.example.com")
for server in "${SERVERS[@]}"; do
    ssh deployer@$server 'bash -s' < /usr/local/bin/deploy.sh &
done
wait
echo "Deploy completed on all servers."

Immediate actions: create a dedicated deployer user with SSH key and limited permissions. Never use root for deployment.

How to Monitor Your Server with Bash and Avoid 3 AM Wake-Up Calls?

Monitoring isn't just about expensive dashboards like Grafana. With a few Bash scripts and one cron line, you can check disk, RAM, CPU, service status, and SSL certificates. The difference? You act before the problem becomes an emergency.

Sponsored Protocol

Disk and Service Check Script

#!/bin/bash
# Meteora Web monitoring
THRESHOLD=85  # disk percentage
SERVICE="nginx"

disk_usage=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$disk_usage" -gt "$THRESHOLD" ]; then
    echo "Disk / at $disk_usage% critical" | mail -s "Disk alert" admin@example.com
fi

# Service check
if ! systemctl is-active --quiet $SERVICE; then
    systemctl restart $SERVICE
    echo "$SERVICE restarted" | mail -s "Service alert" admin@example.com
fi

# SSL expiry check (PEM file)
if [ -f /etc/ssl/certs/mysite.crt ]; then
    expiry=$(openssl x509 -enddate -noout -in /etc/ssl/certs/mysite.crt | cut -d= -f2)
    expiry_sec=$(date -d "$expiry" +%s)
    now_sec=$(date +%s)
    days_left=$(( ($expiry_sec - $now_sec) / 86400 ))
    if [ "$days_left" -lt 14 ]; then
        echo "SSL expires in $days_left days" | mail -s "SSL alert" admin@example.com
    fi
fi

Integration: add this script to cron with */30 * * * * /usr/local/bin/monitor.sh. Instead of email, you can use curl to send notifications to Telegram, Slack, or WhatsApp Business API.

Log Monitoring with Bash and grep

#!/bin/bash
# Detect 500 errors in last 5 minutes
last_minutes=5
errors=$(journalctl -u nginx --since "$last_minutes min ago" --no-pager | grep -c " 500 ")
if [ "$errors" -gt 10 ]; then
    echo "Over $errors 500 errors in $last_minutes minutes" | mail -s "Possible attack or bug" admin@example.com
fi

Caution: set reasonable thresholds to avoid false positives. We at Meteora Web have seen too many alerts ignored because the server sent dozens of daily emails about non-critical issues.

Sponsored Protocol

Where to Store and Version Your Scripts?

Don't keep scripts only on the server. Use a private Git repository (GitHub, GitLab, self-hosted). This way you can track changes, test in staging, and distribute to multiple machines with a simple git pull. We also add an environment variable ENVIRONMENT in the script to differentiate production from development.

What to Do Now: Three Immediate Actions

  1. Create your first backup script: copy the one above, customize the variables, run it manually, then schedule it every night with cron. Check after 24 hours that the file was created.
  2. Set up basic monitoring: take the disk check script, add a Telegram notification (free bots exist). Run it every 15 minutes.
  3. Version your deploy: if you still use FTP, switch to a Bash script that pulls from git. Even if you work alone, rollback saves the day.

Bash server automation is not an accessory for sysadmins—it's a practice that separates those who suffer outages from those who prevent them. We at Meteora Web started this way over eight years ago, and every server we configure still starts with a backup script and a monitoring script. The cost is zero. The value is everything.

Ing. Calogero Bono

> AUTHOR_EXTRACTED

Ing. Calogero Bono

Ingegnere informatico, fondatore di Meteora Web e Zenith OS. System administrator e progettista di piattaforme, app e CMS proprietari, con esperienza in sviluppo full-stack, marketing digitale ed ecosistema Google.
[ Read Full Dossier ]

> METEORA_WEB // DIGITAL AGENCY

We build the digital presence your business deserves.

Websites, social media, online advertising, e-commerce and high-performance hosting, engineered with method by computer engineers in Sciacca, for all of Italy.

> MW_JOURNAL

> READ_ALL()