There are numerous ways of accomplishing a migration from one location to another. This can happen for a variety of reasons (such as version updates to wordpress). Another case could be moving to a new domain. The steps below will detail backing up (and restoring) a WordPress instance to another location. This will be accomplished all the while using bash scripts.
These “backup steps” can be utilized for performing WordPress updates. Especially useful, if you don’t have it on domain, and don’t want to use ftp.
= BACKUP=
We need to backup the wordpress directory structure, and we also need to export the mysql DB into a flat file. Below is a script which can be utilized for doing this. You should alter it to match your directory structures, and specific mysql details.
#!/bin/bash
### Backup script for creating a backup of a wordpress site (database) and all of the files
#### Variables ####
LOG=/tmp/wordpress.backup.log
ELOG=/tmp/wordpress.backup.error.log
NOW=$(date +"%Y%m%d")
FILE=wordpress.backup.${NOW}.tar
BACKUP_DIR="/var/backups" ## Where your backup will be saved
WWW_DIR="/var/www/html/example.com/wordpress" ## Where the wordpress web instance is located
# DB Details #
DB_USER="testuser"
DB_PASS="mydbpassword"
DB_NAME="tstwordpress"
DB_FILE="wordpress.backup.${NOW}.sql" #exported DB will be saved to this file
WWW_TRANSFORM='s,^www/html/example.com/wordpress,wordpress,'
DB_TRANSFORM='s,^var/backups,database,'
= Error Checking #
if [ ! -e $BACKUP_DIR ] ; then
echo "The backup directory does NOT exist, creating now..."
mkdir -p $BACKUP_DIR
fi
if [ ! -e $WWW_DIR ] ; then
echo "The WWW directory does not exist... So nothing to do, exiting...."
exit 1
fi
# = Main = #
echo "Creating a tarball of the website..." | tee $LOG
/bin/tar -cvpf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR 1>$LOG 2>$ELOG
echo ""
echo "Tarball created, now creating a mysqldump of the database..." | tee -a $LOG
echo ""
/usr/bin/mysqldump -u$DB_USER -p${DB_PASS} $DB_NAME > $BACKUP_DIR/$DB_FILE 2>>$LOG
#/usr/bin/mysqldump -u$DB_USER -p $DB_NAME > $BACKUP_DIR/$DB_FILE 1>>$LOG 2>>$ELOG #### Command so password is asked for interactively
# The above command creates a tarball of the files, and then a backup file for the database dump
# use the tar command below to put the db_file backup into the tarball, then remove the db_backup file
echo "" | tee -a $LOG
echo "mysqldump successul. Now appending to the tarball" | tee -a $LOG
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE 2>>$LOG
if [ "$?" -ne "0" ]; then
echo "The appending of the backup DB file failed, exiting"
exit 1
else
rm $BACKUP_DIR/$DB_FILE
fi
# gzip the tarball
echo "" | tee -a $LOG
echo "tarball created successfully, now compressing..." | tee -a $LOG
gzip -9 $BACKUP_DIR/$FILE 1>>$LOG 2>>$ELOG
exit 0
You can copy the text above and save it as wp.backup.sh (or some other name if you wish). Afterwords, you will want to give it execute permission. This is accomplished with: chmod u+x wp.backup.sh
. Now run it to create a backup of your wordpress website. Next thing, is to transfer this gzipped tarball to your new location. Then you will want to extract it to your new website docroot. The steps would vary slightly if you are putting it on a domain (with say sftp access only).
= DB Creation for import =
You will want to create a new mysql DB. The sql file details will be imported into this new mysql DB.
Ensure mysql-server is installed. The mysql binary may not be provided with mysql-server. If that is the case, you may need to install mysql-client. Of course, you will need some web daemon (such as apache2, nginx, etc).
The configuration for the webserver won’t be provided here, as there are multiple http daemons out there. If you are using nginx, then you will need php installed as well as nginx doesn’t handle php natively.
The following commands will create a new DB, setup a wordpress user + password. Then once you run the restoration script, it will populate the mysql DB.
mysql -u root -p -h localhost
mysql> create database foo;
mysql> GRANT ALL ON foo.* TO wpadmin@localhost IDENTIFIED BY 'abc123';
### allow access from 192.168.1.5 too, if required ##
mysql> GRANT ALL ON foo.* TO [email protected] IDENTIFIED BY 'abc123';
mysql> flush privileges;
mysql> quit;
wp-restore.sh
#!/bin/bash
### Variables ###
LOG=/tmp/wordpress.restore.log
FILE=/tmp/wordpress.backup.20150424.tar.gz
RESTORE_DIR=/var/website
SQL_FILE=`ls ${RESTORE_DIR}/database/*.sql
# DB Details #
DB_NAME="foo"
DB_USER="wpadmin"
DB_PASS="abc123"
## Error-checking ##
if [ ! -e $RESTORE_DIR ]; then
echo "The restore directory does NOT exist, creating now..."
mkdir -p $RESTORE_DIR
fi
if [ ! -e $FILE ]; then
echo "FATAL: The wordpress gzip'd backup file can NOT be found"
exit 1
fi
### Main ####
cd $RESTORE_DIR
echo "Extracting contents of gzipped wordpress backup file, this will take a few minutes"
/bin/tar zxvf $FILE 1>$LOG 2>$ELOG
echo "The file extraction has completed successfully, now proceeding to import the data into the mysql DB $DB_NAME"
mysql -u root -p -h localhost foo >$LOG 2>>$ELOG
if [ "$?" -eq "0"]; then
echo "DB import successful"
fi
echo "Now putting a new wp-config.php file in place, and populating the DB required fields"
cp $RESTORE_DIR/wordpress/cp-config-sample.php $RESTORE_DIR/wordpress
sed -i 's/database_name_here/foo/g' $RESTORE_DIR/wordpress/wp-config.php
sed -i 's/username_here/wpadmin/g' $RESTORE_DIR/wordpress/wp-config.php
sed -i 's/password_here/abc123/g' $RESTORE_DIR/wordpress/wp-config.php
echo "Now that is completed, you may need to do the following:"
echo "1. Setup https for your wordpress website"
echo "2. Modify DB to point to your new location"
exit 0
= Point DB to new location =
Assuming your SQL DB has moved to a new home, you should update it to point to the new location. This is a mysql query.
mysql -u wpadmin -h localhost -D foo -p
# check current home value
select * from wp_options where option_name = 'home';
#### update fields
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
That will update your “old url” and replace it with your “new url”. Of course, change that to match your specific environment.