Backup Wordpress with Shell Script

Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-17-2015
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.

Code:
#!/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!"

# 2  
Old 03-17-2015
Code:
"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
# 3  
Old 03-17-2015
ah, that makes sense!

Trying out a different script here:

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

Code:
#!/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

Code:
#!/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!"


Last edited by wyclef; 03-18-2015 at 03:38 PM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Help with Backup Shell Script moh_abaloo UNIX for Beginners Questions & Answers 3 02-08-2018 04:25 AM
Help with Backup Shell Script joemb Shell Programming and Scripting 1 03-28-2014 09:07 AM
Shell script to call Oracle archive backup script when file system reaches threshold value sasikanthdba Shell Programming and Scripting 4 10-17-2013 10:51 AM
Help with Backup Shell Script JimCarrey Shell Programming and Scripting 4 02-11-2013 12:23 AM
Help with Backup Shell Script vino1989 Shell Programming and Scripting 1 02-06-2013 06:00 PM
Help with Backup Shell Script for Network Device Configuration backup saichand1985 Shell Programming and Scripting 4 10-12-2012 03:57 PM
shell script to backup pssooraj72 Shell Programming and Scripting 3 12-28-2011 08:42 AM
Help with Backup Shell Script harry289 Shell Programming and Scripting 6 09-15-2011 10:43 AM
Help with backup shell script telouet Shell Programming and Scripting 11 04-16-2011 11:37 AM
Help with Backup Shell Script kjmpa26 Homework & Coursework Questions 6 04-05-2011 12:07 PM
Help with backup script in C-shell lio123 Shell Programming and Scripting 7 02-09-2011 05:21 PM
Help with backup shell script Omoniyi Shell Programming and Scripting 1 02-05-2010 05:06 AM
Need help with a backup shell script rhm54 Shell Programming and Scripting 4 11-26-2009 07:11 PM
Shell Script backup a file ? anything Shell Programming and Scripting 1 02-03-2008 07:45 AM
Help with a backup shell script Dingosatemypant UNIX for Dummies Questions & Answers 1 11-04-2005 07:45 AM