#!/bin/bash # Wordpress backup script # Author: yohanes.gultom@gmail.com, 2019 # Change variables below according to your setup NOW=$(date +"%Y%m%d%H%M%S") FILE="yohanes.gultom.me.$NOW.tar" BACKUP_DIR="/home/yohanesgultom/backups/yohanes" WP_HOME="/var/www/yohanes" DB_NAME="wordpress" DB_FILE="yohanes.gultom.me.$NOW.sql" MAXFILE=3 # Do not edit codes below unless you know what you are doing WP_PLUGINS="$WP_HOME/wp-content/plugins" WP_THEMES="$WP_HOME/wp-content/themes" WP_UPLOADS="$WP_HOME/wp-content/uploads" WP_CONFIG="$WP_HOME/wp-config.php" # Create WP backup with relative path tar --transform="s,^${WP_HOME:1}/,," --show-transformed -cvf $BACKUP_DIR/$FILE $WP_PLUGINS $WP_THEMES $WP_UPLOADS $WP_CONFIG && # Create WP database backup using credentials from .my.cnf and add it to archive mysqldump $DB_NAME > $BACKUP_DIR/$DB_FILE && # Append the dump to the archive, remove the dump and gzip the whole archive tar --transform="s,^${BACKUP_DIR:1},database," --show-transformed --append --file=$BACKUP_DIR/$FILE $BACKUP_DIR/$DB_FILE && gzip -9 $BACKUP_DIR/$FILE && # Delete DB file rm $BACKUP_DIR/$DB_FILE # This will list our file by modification time and delete all but the MAXFILE most recent MAXFILE=$((MAXFILE+1)) purging=$(ls -dt "$BACKUP_DIR"/* | tail -n +"$MAXFILE"); if [ "$purging" != "" ]; then echo Purging old file: $purging; rm -rf $purging; else echo "No file found for purging at this time"; fi