Like most developers, when I work on a new website, I build the site out on a development server until the site is ready. I use WordPress a lot. Recently I realized I was doing the same stuff over and over and over again to move the site from the development server to the production server. When I notice myself doing a process that is basically the same every time, I look for a way to make it easier or possibly to automate it. This article explains the process I have created. I am still working on it, but I think it is ready to show. This tutorial shows how to do this with bash scripts only. In the future, I plan to do a post using PHP instead. Bug me about that in the comments if you want to see that. As always, use these at your own risk. I have tested these on my Ubuntu server and they worked fine, but I am making no guarantees about the results on whatever server you are running.
On the Development Server
Lets start with the Development server. When creating bash scripts, I like to collect all of the variable info at the beginning of the file. You should be able to enter all your dev server info and all of your production server info here.
#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
Let’s name the SQL backup file and the site zip file
#make MySQL backup filename
BACKUPMYSQLFILE=mysqlbackup.sql
#Make zip file backup name
BACKUPFILE=siteexport.tgz
Getting into the mix, let’s back up the SQL database and save it into the same folder as all of the site files.
#export Database
mysqldump -h localhost -u $CUR_DB_USER -p$CUR_DB_PASS $CUR_DB_NAME > $CUR_PATH$BACKUPMYSQLFILE
Since we are moving this to a different domain name, let’s search for the references to the old URL in the database and replace them with the new one
#find and replace old URLs in SQL file
sed -i “s/${CUR_URL}/${NEW_URL}/g” $CUR_PATH$BACKUPMYSQLFILE
Now that that’s done, let’s replace the old connection information in the wp-config.php file with the new connection info
#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
Now that we have all of that done, We will change to the directory where the site lives and zip up the entire thing, SQL backup and all. Then we can move the file into the root of the web server so we will be able to download it later.
#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
Okay, we should be done on the development server now and there should be a single zip file in the document root that we can download by typing http://someURL.com/siteexport.tgz into the address bar. Now we move to the production server.
On the Production Server
Now on the production server, let’s run a bash script starting with defining the paths and credentials our script will need to install the site exactly the way it was
OLD_URL=http://someURL.com/
NEW_PATH=/path/to/site/
NEW_DB_HOST=some_host
NEW_DB_NAME=database_name
NEW_DB_USER=database_user_name
NEW_DB_PASS=database_password
Now let’s get busy downloading and unzipping the backup file we created on the development server into the document root on the new server.
wget {$OLD_URL}siteexport.tgz
tar -xsf siteexport.tgz -C NEW_PATH
Now we will import the SQL file into the database.
mysql -h {$NEW_DB_HOST} -D {$NEW_DB_NAME} -u {$NEW_DB_USER} -p’{$NEW_DB_PASS}’ < {$NEW_PATH}mysqlbackup.sql
Now that everything is done, let’s get rid of that zip file and the SQL file
rm {$NEW_PATH}siteexport.tgz
rm {$NEW_PATH}mysqlbackup.sql
So there it is. Two bash scripts, one to run on the development server, one to run on the production server. These sure did make my life easier. I hope you like them also. Just to recap, below are the scripts in their entirety. Or download them both here
Bash Script for Development Server
#!/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
Bash Script for Production Server
#!/bin/sh
OLD_URL=http://someURL.com/
NEW_PATH=/path/to/site/
NEW_DB_HOST=some_host
NEW_DB_NAME=database_name
NEW_DB_USER=database_user_name
NEW_DB_PASS=database_password
#download and extract site backup zip file
wget {$OLD_URL}siteexport.tgz
tar -xsf siteexport.tgz -C {$NEW_PATH}
#import data into database
mysql -h {$NEW_DB_HOST} -D {$NEW_DB_NAME} -u {$NEW_DB_USER} -p’{$NEW_DB_PASS}’ < {$NEW_PATH}mysqlbackup.sql
#clean up files
rm {$NEW_PATH}siteexport.tgz
rm {$NEW_PATH}mysqlbackup.sql











[...] for a way to move WP from one server to another? Jeff Johnson posted about it here. Posted in [...]
[...] for a way to move WP from one server to another? Jeff Johnson posted about it here. Posted in [...]
[...] чистый bash скрипт, который дампит БД и делает замену [...]