WordPress Developer & Linux Administrator

Bash script to backup Wordpress

This bash script will accept input and guide you through what it does, but in short, it checks to see if the backup directory specified is set, if not creates it. It will then ask you whether you want one of the following types of backup:

  • database
  • files
  • both

Installation would look something like this:

cd /root/scripts
touch wordpress_backup
chmod u+x wordpress_backup
vi wordpress_backup

Just paste the script in there and run it.

Before running the script, you should first cd into the WordPress root directory for the website in question. Once there, you would execute it like this:

/root/scripts/wordpress_backup

That’s pretty much all there is to it!

#!/bin/bash
#
#  Date: Dec 11th 2014
#  Author: Will Ashworth (williamashworth.com || linuxscripts.org)
#
#  Bash script to backup an existing WordPress site
#
#  Copyright (C) 2015 Will Ashworth
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details. http://www.gnu.org/licenses/

# If using a version of bash earlier than 4.x you need to uncomment this
# shopt -s extglob

projectName=magepsycho_blog
# backupDirDefault=$(readlink -m ../_backups)
ownerOfCurrentDir=`ls -ld . | awk '{print $3}'`
backupDirDefault="/home/$ownerOfCurrentDir/_backups"

# Colors
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
COL_BLUE=$ESC_SEQ"34;01m"
COL_MAGENTA=$ESC_SEQ"35;01m"
COL_CYAN=$ESC_SEQ"36;01m"


echo "----------------------------------------------------"
printf "What should we call this project?\nDefault is this directory's user/owner: $ownerOfCurrentDir\nPress enter to accept default project name!\n"
read -e -p "Project Name: " projectName
if [ "$projectName" = "" ]
then
    projectName=$ownerOfCurrentDir
fi

echo "----------------------------------------------------"
printf "Where should we place the backups?\nDefault is: $backupDirDefault\nPress enter to accept default backup dir!\n"
read -e -p "Backup Dir: " backupDir
if [ "$backupDir" = "" ]
then
    backupDir=$backupDirDefault
fi

if [ -d "$backupDir" ]; then
    echo "----------------------------------------------------"
    echo "Great news. Backup dir already exists!"
    echo "Proceeding..."
else
    echo "----------------------------------------------------"
    echo "Creating our backup directory since it doesn't exist yet..."
    mkdir $backupDir
    echo "Done!"
fi



echo "----------------------------------------------------"
printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType

if [[ $backupType = @(d) ]]; then
    echo "Backing up DB only..."
    backupTypeName="database"
fi
if [[ $backupType = @(f) ]]; then
    echo "Backing up files only..."
    backupTypeName="files"
fi
if [[ $backupType = @(b) ]]; then
    echo "Backing up both files & DB..."
    backupTypeName="full"
fi

fileName=$projectName-$backupTypeName-$(date +"%Y%m%d-%H%M%S")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

if [[ $backupType = @(d|b) ]]; then
    echo "----------------------------------------------------"
    echo "Dumping MySQL..."
    mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
    echo "Done!"
fi

if [[ $backupType = @(f|b) ]]; then
    echo "----------------------------------------------------"
    echo "Archiving Files..."
    #tar -pcvzf $fileName.tar.gz * .htaccess
    tar -pcvzf $fileName.tar.gz .
    echo "Done!"
    echo "----------------------------------------------------"
    echo "Cleaning..."
    rm -f $fileName.sql.gz
    echo "Done!"
fi

if [[ $backupType = @(d|f|b) ]]; then
    echo "----------------------------------------------------"
    mkdir -p $backupDir;
    echo "Moving file to backup dir..."
    if [[ $backupType == d ]]; then
        mv $fileName.sql.gz $backupDir
    fi

    if [[ $backupType = @(f|b) ]]; then
        mv $fileName.tar.gz $backupDir
    fi
    echo "Done!"
else
    echo "Invalid selection!"
fi

# Fix permissions on backup dir to the current owner
chown -R $ownerOfCurrentDir:$ownerOfCurrentDir "$backupDir"
Categories: Bash, WordPressTags:

Comments

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