ranchodeluxemedia
4/6/2016 - 6:11 AM

wpdb_dev.sh

#!/bin/sh
 
#current info
CUR_URL=development.server.com
CUR_DB_HOST=database_host
CUR_DB_NAME=database_name
CUR_DB_USER=database_user
CUR_DB_PASS=database_password
CUR_PATH=/path/to/site/
 
#new info
NEW_URL=production.server.com
NEW_DB_HOST=new_database_host
NEW_DB_NAME=new_database_name
NEW_DB_USER=new_database_user
NEW_DB_PASS=new_database_password
 
#make MySQL backup filename
BACKUPMYSQLFILE=mysqlbackup.sql
#Make zip file backup name
BACKUPFILE=siteexport.tgz
 
#export Database
mysqldump -h localhost -u $CUR_DB_USER -p$CUR_DB_PASS $CUR_DB_NAME > $CUR_PATH$BACKUPMYSQLFILE
 
#find and replace old URLs in SQL file
sed -i “s/${CUR_URL}/${NEW_URL}/g” $CUR_PATH$BACKUPMYSQLFILE
 
#find and replace old Database Info in wp-config.php file
sed -i.backup -e”s/define(‘DB_NAME’, ‘${CUR_DB_NAME}’);/define(‘DB_NAME’, ‘${NEW_DB_NAME}’);/g” -e”s/define(‘DB_USER’, ‘${CUR_DB_USER}’);/define(‘DB_USER’, ‘${NEW_DB_USER}’);/g” -e”s/define(‘DB_PASSWORD’, ‘${CUR_DB_PASS}’);/define(‘DB_PASSWORD’, ‘${NEW_DB_PASS}’);/g” -e”s/define(‘DB_HOST’, ‘${CUR_DB_HOST}’);/define(‘DB_HOST’, ‘${NEW_DB_HOST}’);/g” ${CUR_PATH}wp-config.php
 
#change to the directory of the site, zip it up and move it into the document root
cd ${CUR_PATH} && tar -czf ../${BACKUPFILE} .
mv ../$BACKUPFILE $CUR_PATH