<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jeff Johnson</title>
	<atom:link href="http://jrjdev.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://jrjdev.net</link>
	<description>Christ follower, husband, father, web pro.</description>
	<lastBuildDate>Tue, 06 Mar 2012 19:55:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Moving a WordPress Site with Bash Scripts</title>
		<link>http://jrjdev.net/2012/02/moving-a-wordpress-site-with-bash-scripts/</link>
		<comments>http://jrjdev.net/2012/02/moving-a-wordpress-site-with-bash-scripts/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 02:38:13 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Server Stuff]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[useful]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://jrjdev.net/?p=83</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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. <img src='http://jrjdev.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>On the Development Server</h3>
<p>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.</p>
<p class="code">
#current info<br />
CUR_URL=development.server.com<br />
CUR_DB_HOST=database_host<br />
CUR_DB_NAME=database_name<br />
CUR_DB_USER=database_user<br />
CUR_DB_PASS=database_password<br />
CUR_PATH=/path/to/site/<br />
&nbsp;<br />
#new info<br />
NEW_URL=production.server.com<br />
NEW_DB_HOST=new_database_host<br />
NEW_DB_NAME=new_database_name<br />
NEW_DB_USER=new_database_user<br />
NEW_DB_PASS=new_database_password
</p>
<p>Let&#8217;s name the SQL backup file and the site zip file</p>
<p class="code">
#make MySQL backup filename<br />
BACKUPMYSQLFILE=mysqlbackup.sql<br />
&nbsp;<br />
#Make zip file backup name<br />
BACKUPFILE=siteexport.tgz
</p>
<p>Getting into the mix, let&#8217;s back up the SQL database and save it into the same folder as all of the site files.</p>
<p class="code">
#export Database<br />
mysqldump -h localhost -u $CUR_DB_USER -p$CUR_DB_PASS $CUR_DB_NAME > $CUR_PATH$BACKUPMYSQLFILE
</p>
<p>Since we are moving this to a different domain name, let&#8217;s search for the references to the old URL in the database and replace them with the new one</p>
<p class="code">
#find and replace old URLs in SQL file<br />
sed -i &#8220;s/${CUR_URL}/${NEW_URL}/g&#8221; $CUR_PATH$BACKUPMYSQLFILE
</p>
<p>Now that that&#8217;s done, let&#8217;s replace the old connection information in the <i>wp-config.php</i> file with the new connection info</p>
<p class="code">
#find and replace old Database Info in wp-config.php file<br />
sed -i.backup -e&#8221;s/define(&#8216;DB_NAME&#8217;, &#8216;${CUR_DB_NAME}&#8217;);/define(&#8216;DB_NAME&#8217;, &#8216;${NEW_DB_NAME}&#8217;);/g&#8221; -e&#8221;s/define(&#8216;DB_USER&#8217;, &#8216;${CUR_DB_USER}&#8217;);/define(&#8216;DB_USER&#8217;, &#8216;${NEW_DB_USER}&#8217;);/g&#8221; -e&#8221;s/define(&#8216;DB_PASSWORD&#8217;, &#8216;${CUR_DB_PASS}&#8217;);/define(&#8216;DB_PASSWORD&#8217;, &#8216;${NEW_DB_PASS}&#8217;);/g&#8221; -e&#8221;s/define(&#8216;DB_HOST&#8217;, &#8216;${CUR_DB_HOST}&#8217;);/define(&#8216;DB_HOST&#8217;, &#8216;${NEW_DB_HOST}&#8217;);/g&#8221;   ${CUR_PATH}wp-config.php
</p>
<p>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.</p>
<p class="code">
#change to the directory of the site, zip it up and move it into the document root<br />
cd ${CUR_PATH} &#038;&#038; tar -czf ../${BACKUPFILE} .<br />
mv ../$BACKUPFILE $CUR_PATH</p>
<p>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.</p>
<h3>On the Production Server</h3>
<p>Now on the production server, let&#8217;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</p>
<p class="code">
OLD_URL=http://someURL.com/<br />
NEW_PATH=/path/to/site/<br />
NEW_DB_HOST=some_host<br />
NEW_DB_NAME=database_name<br />
NEW_DB_USER=database_user_name<br />
NEW_DB_PASS=database_password
</p>
<p>Now let&#8217;s get busy downloading and unzipping the backup file we created on the development server into the document root on the new server.</p>
<p class="code">
wget {$OLD_URL}siteexport.tgz<br />
tar -xsf siteexport.tgz -C NEW_PATH
</p>
<p>Now we will import the SQL file into the database.</p>
<p class="code">
mysql -h {$NEW_DB_HOST} -D {$NEW_DB_NAME} -u {$NEW_DB_USER} -p&#8217;{$NEW_DB_PASS}&#8217; < {$NEW_PATH}mysqlbackup.sql
</p>
<p>Now that everything is done, let&#8217;s get rid of that zip file and the SQL file</p>
<p class="code">
rm {$NEW_PATH}siteexport.tgz<br />
rm {$NEW_PATH}mysqlbackup.sql
</p>
<p>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 <a href="/wp-content/uploads/2012/02/bash_for_moving_wordpress_site.zip">here</a></p>
<h3>Bash Script for Development Server</h3>
<p class="code">
#!/bin/sh<br />
&nbsp;<br />
#current info<br />
CUR_URL=development.server.com<br />
CUR_DB_HOST=database_host<br />
CUR_DB_NAME=database_name<br />
CUR_DB_USER=database_user<br />
CUR_DB_PASS=database_password<br />
CUR_PATH=/path/to/site/<br />
&nbsp;<br />
#new info<br />
NEW_URL=production.server.com<br />
NEW_DB_HOST=new_database_host<br />
NEW_DB_NAME=new_database_name<br />
NEW_DB_USER=new_database_user<br />
NEW_DB_PASS=new_database_password<br />
&nbsp;<br />
#make MySQL backup filename<br />
BACKUPMYSQLFILE=mysqlbackup.sql<br />
#Make zip file backup name<br />
BACKUPFILE=siteexport.tgz<br />
&nbsp;<br />
#export Database<br />
mysqldump -h localhost -u $CUR_DB_USER -p$CUR_DB_PASS $CUR_DB_NAME > $CUR_PATH$BACKUPMYSQLFILE<br />
&nbsp;<br />
#find and replace old URLs in SQL file<br />
sed -i &#8220;s/${CUR_URL}/${NEW_URL}/g&#8221; $CUR_PATH$BACKUPMYSQLFILE<br />
&nbsp;<br />
#find and replace old Database Info in wp-config.php file<br />
sed -i.backup -e&#8221;s/define(&#8216;DB_NAME&#8217;, &#8216;${CUR_DB_NAME}&#8217;);/define(&#8216;DB_NAME&#8217;, &#8216;${NEW_DB_NAME}&#8217;);/g&#8221; -e&#8221;s/define(&#8216;DB_USER&#8217;, &#8216;${CUR_DB_USER}&#8217;);/define(&#8216;DB_USER&#8217;, &#8216;${NEW_DB_USER}&#8217;);/g&#8221; -e&#8221;s/define(&#8216;DB_PASSWORD&#8217;, &#8216;${CUR_DB_PASS}&#8217;);/define(&#8216;DB_PASSWORD&#8217;, &#8216;${NEW_DB_PASS}&#8217;);/g&#8221; -e&#8221;s/define(&#8216;DB_HOST&#8217;, &#8216;${CUR_DB_HOST}&#8217;);/define(&#8216;DB_HOST&#8217;, &#8216;${NEW_DB_HOST}&#8217;);/g&#8221;   ${CUR_PATH}wp-config.php<br />
&nbsp;<br />
#change to the directory of the site, zip it up and move it into the document root<br />
cd ${CUR_PATH} &#038;&#038; tar -czf ../${BACKUPFILE} .<br />
mv ../$BACKUPFILE $CUR_PATH<br />
&nbsp;
</p>
<h3>Bash Script for Production Server</h3>
<p class="code">
#!/bin/sh<br />
&nbsp;<br />
OLD_URL=http://someURL.com/<br />
NEW_PATH=/path/to/site/<br />
NEW_DB_HOST=some_host<br />
NEW_DB_NAME=database_name<br />
NEW_DB_USER=database_user_name<br />
NEW_DB_PASS=database_password<br />
&nbsp;<br />
#download and extract site backup zip file<br />
wget {$OLD_URL}siteexport.tgz<br />
tar -xsf siteexport.tgz -C {$NEW_PATH}<br />
&nbsp;<br />
#import data into database<br />
mysql -h {$NEW_DB_HOST} -D {$NEW_DB_NAME} -u {$NEW_DB_USER} -p&#8217;{$NEW_DB_PASS}&#8217; < {$NEW_PATH}mysqlbackup.sql<br />
&nbsp;<br />
#clean up files<br />
rm {$NEW_PATH}siteexport.tgz<br />
rm {$NEW_PATH}mysqlbackup.sql</p>
]]></content:encoded>
			<wfw:commentRss>http://jrjdev.net/2012/02/moving-a-wordpress-site-with-bash-scripts/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bash Script for Backing up your site</title>
		<link>http://jrjdev.net/2012/01/bash-script-for-backing-up-your-site/</link>
		<comments>http://jrjdev.net/2012/01/bash-script-for-backing-up-your-site/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 18:26:55 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Server Stuff]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[useful]]></category>

		<guid isPermaLink="false">http://jrjdev.net/?p=28</guid>
		<description><![CDATA[After losing a site a few weeks ago, I decided I needed to figure out how to set up an automatic backup. What follows is a script I cobbled together from scripts I found on many different sites. This happens to work for my configuration on my Ubuntu server. If this helps you too, then [...]]]></description>
			<content:encoded><![CDATA[<p>After losing a site a few weeks ago, I decided I needed to figure out how to set up an automatic backup. What follows is a script I cobbled together from scripts I found on many different sites. This happens to work for my configuration on my Ubuntu server.  If this helps you too, then great! That&#8217;s the entire reason I put it up here. Fair warning, I am no expert at Linux, so this may only work on my server. Feel free to ask questions in the comments section.</p>
<p>This tutorial outlines backing up your site database first, then backing up the site files. It also includes a handy script that will delete the old backups so you won&#8217;t fill up your hard disk.  Lastly, I show how to add it as a cron job so it will run automatically.</p>
<p>As always, use this stuff at your own risk. No one has ever called me perfect and I doubt this would qualify either. As a matter of fact, there may be a better way to do this stuff too.  All I know is that it works for me. Without further ado:</p>
<h2>Database Backup</h2>
<p>First off, we define what we want the filename to be. I like my backup files to include the date in the filename so I am using the date function to tack the date and time (in the format 2012_01_21_200) onto the front of the filename. The rest of the filename describes what the file is.</p>
<p class="code">
BACKUPFILE=$(date +%Y_%m_%d_%H%M)_mysqlbackup.sql
</p>
<p>Next, I want to go ahead and save the path to the site into a variable so I won&#8217;t have to enter it a bunch of times.</p>
<p class="code">
PATHTOSITE=&#8221;/path/to/site&#8221;
</p>
<p>Now I want to use the <i>mysqldump</i> function to create a text file with all of the database in it and save it to the <i>$PATHTOSITE/mysql_backups/</i> folder with the filename that I created above.</p>
<p class="code">
mysqldump -h hostname -u username -p&#8217;password&#8217; db_name > $PATHTOSITE/mysql_backups/$BACKUPFILE
</p>
<p>So it won&#8217;t take up too much space, Let&#8217;s zip it up with gzip</p>
<p class="code">
gzip -f $PATHTOSITE/mysql_backups/$BACKUPFILE
</p>
<h2>Site Backup</h2>
<p>Now let&#8217;s back up the entire contents of the htdocs folder.  The first line defiens what the file name should be.  The second line we are creating a tarball of the <i>htdocs</i> folder and putting it into the folder called <i>site_backups</i>. </p>
<p class="code">
BACKUPFILE=$(date +%Y_%m_%d_%H%M)_sitebackup.tgz
</p>
<p class="code">
tar -cvzpf $PATHTOSITE/site_backups/$BACKUPFILE $PATHTOSITE/htdocs
</p>
<p>As one might imagine, if your site is any significant size, these site backup files would fill up the hard disk after a while. I guess remembering to go in and delete the old ones would be a method of preventing the hard disk from filling up, but this is supposed to be about automation right? </p>
<p>The following code finds all the files in each of our backup folders and deletes the ones that are more than 7 days old.</p>
<p class="code">
find $PATHTOSITE/mysql_backups -type f -mtime +7 -exec rm {} \;
</p>
<p class="code">
find $PATHTOSITE/site_backups -type f -mtime +7 -exec rm {} \;
</p>
<p>Now we can set this up as a cron job so it will run every night. To get to the cron jobs, I type the following at the command line</p>
<p class="code">
crontab -e
</p>
<p>After it opens the file go down to the bottom of the file and add the new job in. I prefer it to run at 2am myself.</p>
<p class="code">
0         2      *       *       *       &#8220;./path/to/script/site_backup.sh&#8221;
</p>
<p>This should be on its own line and there might even be others in there already. So just put it after the existing lines.  In case it isn&#8217;t clear what this does, let me explain, this line is telling the server, &#8220;At the <b>0</b>th minute of <b>2</b> o&#8217;clock (24 hour clock btw), <b>* (every day)</b>, <b>* (every month)</b>, <b>* (every day of the week)</b>, run the script &#8220;./path/to/script/site_backup.sh&#8221;</p>
<p>This should do it.  I have the full bash script below for you to copy.  Enjoy <img src='http://jrjdev.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Full Script</h2>
<p class="code">
#!/bin/sh<br />
#FILENAME: site_backup.sh<br />
BACKUPFILE=$(date +%Y_%m_%d_%H%M)_mysqlbackup.sql<br />
PATHTOSITE=&#8221;/path/to/site&#8221;<br />
mysqldump -h hostname -u username -p&#8217;password&#8217; db_name > $PATHTOSITE/mysql_backups/$BACKUPFILE<br />
gzip -f $PATHTOSITE/mysql_backups/$BACKUPFILE<br />
BACKUPFILE=$(date +%Y_%m_%d_%H%M)_sitebackup.tgz<br />
tar -cvzpf $PATHTOSITE/site_backups/$BACKUPFILE $PATHTOSITE/htdocs<br />
find $PATHTOSITE/mysql_backups -type f -mtime +7 -exec rm {} \;<br />
find $PATHTOSITE/site_backups -type f -mtime +7 -exec rm {} \;</p>
]]></content:encoded>
			<wfw:commentRss>http://jrjdev.net/2012/01/bash-script-for-backing-up-your-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
