Automating Server Backups Using Bash Scripts and Crontab

0
39

Introduction

Server backups are crucial for data integrity, disaster recovery, and system resilience. Manual backups can be time-consuming and error-prone, which is why automation is essential. In this guide, you’ll learn how to automate server backups using simple Bash scripts combined with Crontab for scheduling. This method ensures that your data is consistently backed up without manual intervention.

We’ll cover everything from writing a backup script to scheduling it to run at regular intervals, providing a comprehensive approach to server backup automation.

Prerequisites

Before getting started, ensure you have:

  • A Linux server (Ubuntu, Debian, or CentOS)
  • Basic knowledge of Bash scripting
  • Root or sudo privileges

Step 1: Create a Directory for Backups

Organize your backups by creating a dedicated directory:

sudo mkdir -p /backups/daily

Set the correct permissions:

sudo chmod 700 /backups/daily

Step 2: Write the Bash Backup Script

Create a Bash script to automate the backup process:

sudo nano /usr/local/bin/backup.sh

Insert the following script:

#!/bin/bash
# Define variables
BACKUP_DIR=\”/backups/daily\”
SOURCE_DIR=\”/var/www\”
DB_USER=\”root\”
DB_PASS=\”your_password\”
DATE=\$(date +\”%Y-%m-%d\”)

# Backup website files
tar -czf \$BACKUP_DIR/files-\$DATE.tar.gz \$SOURCE_DIR

# Backup MySQL databases
mysqldump -u \$DB_USER -p\$DB_PASS –all-databases | gzip > \$BACKUP_DIR/db-\$DATE.sql.gz

# Remove backups older than 7 days
find \$BACKUP_DIR -type f -mtime +7 -delete

Make the script executable:

sudo chmod +x /usr/local/bin/backup.sh

Explanation of the Script

  • BACKUP_DIR: The directory where backups are stored.
  • SOURCE_DIR: The folder containing files to back up (e.g., website data).
  • mysqldump: Used to export all MySQL databases securely.
  • find: Deletes backups older than 7 days to save disk space.

Step 3: Test the Backup Script

Run the script manually to ensure it works as expected:

sudo /usr/local/bin/backup.sh

Verify the backup files:

ls -lh /backups/daily

Step 4: Automate Backups with Crontab

Open the Crontab editor:

sudo crontab -e

Add the following line to schedule the backup to run daily at 2 AM:

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

Crontab Timing Breakdown:

  • 0 2 *: Executes at 2:00 AM daily.
  • * *: Wildcards for days and months (runs every day/month).

Step 5: Verify Crontab Automation

Ensure the cron job is scheduled correctly:

sudo crontab -l

To monitor cron logs and confirm execution:

grep CRON /var/log/syslog

Step 6: Implement Backup Rotation (Optional)

For long-term storage management, consider rotating backups using logrotate:

sudo apt install logrotate -y

Create a custom log rotation configuration:

sudo nano /etc/logrotate.d/backup

Add the following:

/backups/daily/*.gz {
daily
rotate 14
compress
missingok
notifempty
}

This configuration keeps backups for 14 days before rotating them.

Security Tips

  • Ensure backup directories have restricted permissions (chmod 700).
  • Encrypt sensitive backups using tools like gpg.
  • Store backups offsite for disaster recovery redundancy.

Conclusion

Congratulations! You’ve successfully automated server backups using Bash scripts and Crontab. This setup ensures data consistency, reduces manual workload, and protects against data loss. Regularly test your backups to ensure they can be restored when needed, and consider advanced tools for complex backup strategies.