Posted on
Administration

Using Bash to Automate Database Backups

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Automating database backups with Bash is a reliable way to ensure data integrity and recovery in case of failures. Here's how you can achieve it for common databases like MySQL, PostgreSQL, and SQLite.


1. General Steps for Automating Database Backups

  1. Determine Database Type: MySQL, PostgreSQL, SQLite, etc.
  2. Install Backup Tools:
    • MySQL: mysqldump
    • PostgreSQL: pg_dump
    • SQLite: Direct file copy.
  3. Write a Backup Script:
    • Specify the database credentials, backup location, and naming conventions.
  4. Schedule the Script: Use cron for periodic execution.
  5. Secure Backups: Encrypt sensitive data and restrict access to backup files.

2. MySQL/MariaDB Backup Script

Step 1: Install MySQL Utilities

Ensure mysqldump is installed:

sudo apt install mysql-client     # Debian/Ubuntu
sudo yum install mysql            # CentOS/RHEL
sudo dnf install mysql            # Fedora
sudo zypper install mysql-client  # openSUSE

Step 2: Create the Backup Script

Example:

#!/bin/bash

# Configuration
DB_USER="root"
DB_PASS="password"
DB_NAME="my_database"
BACKUP_DIR="/backups/mysql"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/$DB_NAME-$TIMESTAMP.sql"

# Ensure backup directory exists
mkdir -p $BACKUP_DIR

# Perform the backup
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_FILE

# Verify success
if [ $? -eq 0 ]; then
    echo "Backup successful: $BACKUP_FILE"
else
    echo "Backup failed!" >&2
fi

Step 3: Make the Script Executable

chmod +x /path/to/mysql_backup.sh

Step 4: Schedule with Cron

Edit crontab:

crontab -e

Add a daily backup job:

0 2 * * * /path/to/mysql_backup.sh >> /var/log/mysql_backup.log 2>&1

3. PostgreSQL Backup Script

Step 1: Install PostgreSQL Utilities

Ensure pg_dump is installed:

sudo apt install postgresql-client  # Debian/Ubuntu
sudo yum install postgresql         # CentOS/RHEL
sudo dnf install postgresql         # Fedora
sudo zypper install postgresql      # openSUSE

Step 2: Create the Backup Script

Example:

#!/bin/bash

# Configuration
DB_USER="postgres"
DB_NAME="my_database"
BACKUP_DIR="/backups/postgresql"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/$DB_NAME-$TIMESTAMP.sql"

# Ensure backup directory exists
mkdir -p $BACKUP_DIR

# Perform the backup
PGPASSWORD="password" pg_dump -U $DB_USER -d $DB_NAME > $BACKUP_FILE

# Verify success
if [ $? -eq 0 ]; then
    echo "Backup successful: $BACKUP_FILE"
else
    echo "Backup failed!" >&2
fi

4. SQLite Backup Script

Step 1: Identify SQLite Database File

SQLite databases are typically stored as .db files.

Step 2: Create the Backup Script

Example:

#!/bin/bash

# Configuration
DB_FILE="/path/to/database.db"
BACKUP_DIR="/backups/sqlite"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/database-$TIMESTAMP.db"

# Ensure backup directory exists
mkdir -p $BACKUP_DIR

# Perform the backup
cp $DB_FILE $BACKUP_FILE

# Verify success
if [ $? -eq 0 ]; then
    echo "Backup successful: $BACKUP_FILE"
else
    echo "Backup failed!" >&2
fi

5. Compressing and Encrypting Backups

Compress the Backup

To save space, compress the backup file:

gzip $BACKUP_FILE

Encrypt the Backup

For security, encrypt backups using gpg:

gpg --encrypt --recipient "your_email@example.com" $BACKUP_FILE

6. Automating with Cron

For all examples, schedule your scripts using cron. Example:

0 3 * * * /path/to/postgresql_backup.sh >> /var/log/postgresql_backup.log 2>&1

7. Rotating Backups

To avoid filling up disk space, remove old backups:

find /backups/mysql -type f -mtime +7 -exec rm {} \;
  • -mtime +7: Files older than 7 days.

Add this to your script after the backup step.


8. Uploading Backups to Cloud Storage

For remote storage:

  • AWS S3:

    aws s3 cp $BACKUP_FILE s3://your-bucket-name/
    
  • Google Drive: Use rclone.

  • FTP Server:

    curl -T $BACKUP_FILE ftp://ftp.example.com --user username:password
    

9. Monitoring Backup Status

Send email notifications:

echo "Backup completed successfully at $(date)" | mail -s "Backup Success" admin@example.com

10. Best Practices

  1. Test Regularly: Verify backups by restoring them.
  2. Secure Credentials: Use .my.cnf or .pgpass for storing credentials securely.
  3. Automate Cleanup: Use rotation policies to delete old backups.
  4. Log Output: Save script logs for troubleshooting.
  5. Store Offsite: Use cloud or remote servers for redundancy.

This approach ensures reliable, automated database backups. Let me know if you need help customizing your script!

Further Reading

To further deepen your understanding and expand your knowledge on automating database backups with Bash and related topics, consider exploring the following resources:

  1. Official mysqldump Documentation - MySQL Backup and Recovery

  2. PostgreSQL Official Documentation on pg_dump

    • A complete guide on how to use pg_dump to back up PostgreSQL databases.
    • URL: PostgreSQL pg_dump
  3. Digital Ocean - How To Schedule Bash Scripts Using Cron

    • Offers a step-by-step tutorial on scheduling scripts using cron, ideal for automating backup processes.
    • URL: Digital Ocean Guide on Cron
  4. Linuxize - Bash Scripting Cheat Sheet

    • A concise cheat sheet for Bash scripting that can assist in writing more efficient and robust backup scripts.
    • URL: Bash Scripting Cheat Sheet
  5. Rclone Documentation

    • Detailed guide on how to configure rclone for managing cloud storage, useful for storing database backups offsite.
    • URL: Rclone Docs

These resources will provide you with a comprehensive set of guidelines and best practices for automating your database backups smoothly and securely using Bash.