automated wordpress backup and restore

Automating WordPress Migration

Introduction

When managing a WordPress site, it is crucial to have reliable backup and restoration procedures to ensure data integrity and minimize downtime in case of issues. The following scripts provide an automated way to backup and restore a complete WordPress installation, including its database and web server configuration (Nginx or Apache).

Backup Script (backup_wordpress.sh)

The backup script performs the following tasks:

  1. Backs up WordPress Files: Compresses the WordPress installation directory into a tar.gz archive.
  2. Backs up the Database: Exports the WordPress MySQL/MariaDB database to a SQL file.
  3. Backs up Web Server Configuration: Archives the configuration files for Nginx or Apache into a tar.gz file.

Restore Script (restore_wordpress.sh)

The restore script handles the restoration process by:

  1. Installing Required Software: Ensures that Nginx, Apache, MySQL, and PHP are installed.
  2. Restoring Web Server Configuration: Extracts and restores the Nginx or Apache configuration files from the backup.
  3. Restoring WordPress Files: Extracts the WordPress files from the backup archive to the specified directory.
  4. Restoring the Database: Creates a new database and imports the backup SQL file to restore the WordPress database.

These scripts automate the tedious and error-prone process of backing up and restoring a WordPress site, making it easier to maintain the site’s integrity and recover quickly from potential issues.

Backup Script: backup_wordpress.sh

#!/bin/bash

# Variables
WP_DIR="/var/www/html/wordpress"  # Change to your WordPress installation directory
BACKUP_DIR="/path/to/backup"      # Change to your desired backup directory
DB_NAME="wordpress_db"            # Change to your WordPress database name
DB_USER="wp_user"                 # Change to your WordPress database user
DB_PASS="your_db_password"        # Change to your WordPress database password
DATE=$(date +%F)

# Create backup directory if it doesn't exist
mkdir -p ${BACKUP_DIR}

# Backup WordPress files
tar -czvf ${BACKUP_DIR}/wordpress_files_${DATE}.tar.gz -C ${WP_DIR} .

# Backup database
mysqldump -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > ${BACKUP_DIR}/wordpress_db_${DATE}.sql

# Backup Nginx or Apache configuration
if [ -d /etc/nginx ]; then
    tar -czvf ${BACKUP_DIR}/nginx_conf_${DATE}.tar.gz -C /etc/nginx .
elif [ -d /etc/apache2 ]; then
    tar -czvf ${BACKUP_DIR}/apache_conf_${DATE}.tar.gz -C /etc/apache2 .
fi

echo "Backup completed successfully."

Restore Script: restore_wordpress.sh

#!/bin/bash

# Variables
BACKUP_DIR="/path/to/backup"      # Change to your backup directory
WP_DIR="/var/www/html/wordpress"  # Change to your WordPress installation directory
DB_NAME="wordpress_db"            # Change to your WordPress database name
DB_USER="wp_user"                 # Change to your WordPress database user
DB_PASS="your_db_password"        # Change to your WordPress database password

# Install required software
apt-get update
apt-get install -y nginx apache2 mysql-server php php-fpm php-mysql tar

# Restore Nginx or Apache configuration
if [ -f ${BACKUP_DIR}/nginx_conf_*.tar.gz ]; then
    tar -xzvf ${BACKUP_DIR}/nginx_conf_*.tar.gz -C /etc/nginx
    systemctl restart nginx
elif [ -f ${BACKUP_DIR}/apache_conf_*.tar.gz ]; then
    tar -xzvf ${BACKUP_DIR}/apache_conf_*.tar.gz -C /etc/apache2
    systemctl restart apache2
fi

# Restore WordPress files
mkdir -p ${WP_DIR}
tar -xzvf ${BACKUP_DIR}/wordpress_files_*.tar.gz -C ${WP_DIR}

# Restore database
mysql -u ${DB_USER} -p${DB_PASS} -e "CREATE DATABASE ${DB_NAME};"
mysql -u ${DB_USER} -p${DB_PASS} ${DB_NAME} < ${BACKUP_DIR}/wordpress_db_*.sql

echo "Restore completed successfully."

Instructions:

  1. Backup Script (backup_wordpress.sh):
  • Replace the variables with your actual WordPress directory, backup directory, database name, user, and password.
  • Make the script executable: chmod +x backup_wordpress.sh
  • Run the script to create backups: ./backup_wordpress.sh
  1. Restore Script (restore_wordpress.sh):
  • Replace the variables with your actual backup directory, WordPress directory, database name, user, and password.
  • Make the script executable: chmod +x restore_wordpress.sh
  • Run the script to restore the backup: ./restore_wordpress.sh

These scripts assume the use of MySQL/MariaDB as the database server. Adjust the database-related commands if you use a different database system.

Other Recent Posts