Backup Wordpress with Shell Script

Hello,

I'm trying to backup Wordpress files and database with a shell script. I found one online that works great and am trying to work it a bit to provide more notices and progress info as well as work on some problems I'm having.

For starters... when I run this I get a notice after Create the archive... and Append the dump to the archive...

"tar: Removing leading `/' from member names"

Does anyone know what this means, and what I can do about it? This is run in a shared hosting environment if that helps.

Also, I'm sort of new to shell scripting but how can I add some kind of progress notice after each step, or a blinking ellipsis to designate that there is an ongoing process taking place. And one other thing... would there be a good way to secure the DB credentials so they aren't just out in the open? Also, how could I set this to cycle the files every month or set time period... like 30 days?

Any help would be appreciated.

#!/bin/bash

# This script creates a compressed backup archive of the given directory and the given MySQL table. More details on implementation here: http://theme.fm
# Feel free to use this script wherever you want, however you want. We produce open source, GPLv2 licensed stuff.
# Author: Konstantin Kovshenin exclusively for Theme.fm in June, 2011

# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="mydomain.com.$NOW.tar"
BACKUP_DIR="/home/user/backups"
WWW_DIR="/home/user/mydomain.com/"

# MySQL database credentials
DB_USER="database_user"
DB_PASS="database_password"
DB_NAME="database_name"
DB_HOST="mysql.mydomain.com"
DB_FILE="mydomain.com.$NOW.sql"

# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^home/user/mydomain.com,www,'
DB_TRANSFORM='s,^home/user/backups,database,'

# Initial setup
TODAY=$(date)
echo "----------------------------------------------------
Date: $TODAY
Host: mydomain.com automated backup"

# Create the archive and the MySQL dump
echo "----------------------------------------------------"
echo "Create the archive..."
tar -cpf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
echo "Done!"
echo "Create the MySQL dump..."
mysqldump -h $DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE
echo "Done!"

# Append the dump to the archive, remove the dump and compress the whole archive.
echo "----------------------------------------------------"
echo "Append the dump to the archive..."
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
echo "Done!"
echo "Remove the dump..."
rm $BACKUP_DIR/$DB_FILE
echo "Done!"
echo "Compress the whole archive..."
gzip -9 $BACKUP_DIR/$FILE
echo "Done!" 
"tar: Removing leading `/' from member names"

Just means that tar removes the leading "/" because when you tar the files, it will be relative to the directory where you untar the files.

If you untar a file and the archive contained the leading "/" then you would not have control over where the file is untarred, and then you might overwrite an exiting file by accident

ah, that makes sense!

Trying out a different script here:

How come if I use the following script and select b at the prompt:

#!/bin/bash
#@author        MagePsycho <magepsycho@gmail.com>
#@website        http://www.magepsycho.com
#@blog            http://www.blog.magepsycho.com
#@version        0.1.0

#/************************ EDIT VARIABLES ************************/
projectName=project_demo
backupDir=/home/user/_backups
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d")
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)

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|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 -zcf $fileName.tar.gz * .htaccess
    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

it works...

but then if I try to remove all the options and just have it run b I get the following error:

mv: cannot stat `project_demo-2015-03-17.sql.gz': No such file or directory

#!/bin/bash

#/************************ EDIT VARIABLES ************************/
projectName=project_demo
backupDir=/home/user/_backups
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d")
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)

echo "----------------------------------------------------"
echo "Dumping MySQL..."
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
echo "Done!"

echo "----------------------------------------------------"
echo "Archiving Files..."
tar -zcf $fileName.tar.gz * .htaccess
echo "Done!"
echo "----------------------------------------------------"
echo "Cleaning..."
rm -f $fileName.sql.gz
echo "Done!"

echo "----------------------------------------------------"
mkdir -p $backupDir;
echo "Moving file to backup dir..."
mv $fileName.sql.gz $backupDir
mv $fileName.tar.gz $backupDir
echo "Done!"