Automating Website Backups on Apache Servers Using Command-Line Tools

As a software engineer managing Linux servers with Apache, one of the most critical responsibilities is ensuring the safety and recoverability of your web site’s data. Automated backups are not only a best practice but essential for disaster recovery and business continuity. In this article, I’ll walk you through a straightforward method to automate website backups directly from the command line, leveraging common Linux tools.


Why Automate Backups?

Manual backups are prone to omission, human error, and time constraints. With automation, you guarantee that backups are performed regularly—even when you’re not around. This is especially important for dynamic web sites where content and configurations change frequently.

What to Back Up?

  • Website Files: This includes your HTML, PHP, JavaScript, images, and any configuration files under /var/www/ or wherever your web root is located.
  • Apache Configuration: Typically found in /etc/apache2/ or /etc/httpd/.
  • Database: If your site uses MySQL/MariaDB or PostgreSQL, make sure to grab a dump of your databases.

Sample Backup Script

Here’s a simple bash script to automate the backup of your Apache hosted web site:

#!/bin/bash
# Site Backup Script by Lenny

BACKUP_DIR="/backups/$(date +%F)"
WEB_ROOT="/var/www/html"
APACHE_CONF="/etc/apache2"
DB_USER="yourdbuser"
DB_PASS="yourdbpassword"
DB_NAME="yourdbname"

mkdir -p "$BACKUP_DIR"

echo "Backing up website files..."
tar czf "$BACKUP_DIR/web_files.tar.gz" -C "$WEB_ROOT" .

echo "Backing up Apache configuration..."
tar czf "$BACKUP_DIR/apache_conf.tar.gz" -C "$APACHE_CONF" .

echo "Backing up database..."
mysqldump -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" > "$BACKUP_DIR/db_backup.sql"

echo "Backup complete! Files saved in $BACKUP_DIR."

Tip: For PostgreSQL, use pg_dump instead of mysqldump.

Automating with Cron

Once you have your script saved (e.g., /usr/local/bin/site-backup.sh), give it execute permissions:

chmod +x /usr/local/bin/site-backup.sh

Edit your crontab to run the backup daily at 2:00 AM:

0 2 * * * /usr/local/bin/site-backup.sh

Final Thoughts

Automated backups are the foundation of reliable server administration. Always test your backup and restoration processes regularly to ensure that everything works as expected when you need it most.

Stay safe, and happy hosting!

— Lenny

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *