Last active
March 10, 2022 12:00
-
-
Save Kudratullah/0d8210e7f4fb26cf24b503baef8fc0b9 to your computer and use it in GitHub Desktop.
WordPress Backup Bash Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set environment | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
# Status Output Functions. | |
RED_BOLD='\033[1;31m' | |
GREEN_BOLD='\033[1;32m' | |
BLUE_BOLD='\033[1;34m' | |
YELLOW_BOLD='\033[1;33m' | |
#ROX='\033[1;31m' | |
ROX='\033[1;37m' | |
WP=$BLUE_BOLD | |
COLOR_RESET='\033[0m' | |
error() { | |
echo -e "${RED_BOLD}$1${COLOR_RESET}" | |
} | |
_status() { | |
echo -e "${BLUE_BOLD}$1${COLOR_RESET}" | |
} | |
success() { | |
echo -e "${GREEN_BOLD}$1${COLOR_RESET}" | |
} | |
warning() { | |
echo -e "${YELLOW_BOLD}$1${COLOR_RESET}" | |
} | |
# Are we running as root | |
if [ $(id -u) != "0" ]; then | |
error "WP Backup utility needs to be run with root privileges" | |
error "Try again with root privileges" | |
error "E.G. sudo bash $0 $1 $2" | |
exit 1; | |
fi | |
WP_PATH="$1/" | |
BACKUP_PATH=$2 | |
BACKUP_NAME="backup-`date +"%d-%m-%Y"`" | |
_PWD=$(pwd) | |
if [ $# -lt 2 ]; then | |
BACKUP_PATH=$(pwd) | |
fi | |
BACKUP_FILE="$BACKUP_NAME" | |
CONFIG_FILE="${WP_PATH}wp-config.php" | |
if [ -f "$CONFIG_FILE" ]; then | |
echo "" | |
else | |
error "WordPress config file (wp-config) not found in specified path ($WP_PATH)" | |
exit 1; | |
fi | |
if [ -f "$BACKUP_FILE.tar.gz" ]; then | |
i=1 | |
while [ -f "$BACKUP_FILE-$i.tar.gz" ]; do | |
((i=i+1)) | |
done | |
BACKUP_FILE="$BACKUP_FILE-$i" | |
fi | |
_status "Reading wp-config.php file" | |
# GET WP DB, DB User & DB Pass | |
WP_DB_HOST=`cat $CONFIG_FILE | grep DB_HOST | cut -d \' -f 4` | |
WP_DB_NAME=`cat $CONFIG_FILE | grep DB_NAME | cut -d \' -f 4` | |
WP_DB_USER=`cat $CONFIG_FILE | grep DB_USER | cut -d \' -f 4` | |
WP_DB_PASS=`cat $CONFIG_FILE | grep DB_PASSWORD | cut -d \' -f 4` | |
# Temp Dir. | |
mkdir $BACKUP_FILE | |
cd $BACKUP_FILE | |
echo "" | |
_status "Creating DB Backup..." | |
mysqldump --user $WP_DB_USER --password="$WP_DB_PASS" $WP_DB_NAME | gzip > $WP_DB_NAME-$(date +%Y%m%d%H%M%S).sql.gz | |
echo "" | |
_status "Backing up WordPress installation" | |
mkdir wp_files | |
sudo cp -rp $WP_PATH wp_files | |
echo "" | |
_status "Creating Final Backup Archive" | |
cd $_PWD | |
tar -zcf "$BACKUP_FILE.tar.gz" $BACKUP_FILE | |
echo "" | |
_status "Cleaning Up Temp Files..." | |
rm -rf $BACKUP_FILE | |
echo "" | |
success "Backup Created $BACKUP_FILE.tar.gz !" | |
echo "" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment