WordPress bash deployment script

Recently in my current company we had a client asking for a restaurant website. We’ve decided to do it with WordPress and used it as a CMS instead of a blog. Beside the fact that we needed to create the website, they also requested us to make all the maintenance of the website and their blog. What I suggested was to have a staging platform and of course the production platform.

To do make the deployment process from staging to production seamless, I’ve created a WordPress bash deployment script to easily go from staging to production. Some of you will ask me about the development platform? To do so, we’ve used a service called DeployHQ that works pretty well. It fetch all the content of your SVN repository and push it straight to the FTP. The only tricky part was for the database. I know it wasn’t clean but I made the decision to work on the same database for the staging and development part.

If you have any suggestions about improving this script do not hesitate.

Please consider the following on our installation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh
#
# Written by Emmanuel Pays <landmanu /a-t/ ekoote.com>
#
# Description : Use this script to deploy in production a Wordpress blog that already exist on the same machine
#
# staging_to_production_wordpress.sh ver. 1.0
#
# Latest version can be found at http://blog.loopion.com/
# Todo
# - DONE - Email when deployment is done
# - Execution time for the script
# - Set version on meta or anywhere else to keep track of deployment version
 
# Global config
www_path="/var/www/vhosts/${domain}/"
backup_path_db="${www_path}backup_db/"
backup_path_files="${www_path}backup_files/"
domain="yourdomain.com"
SUBJECT="${prod_uri} deployment successful"
EMAIL="your@email.com"
 
#Staging info
staging_uri="staging.${domain}"
root_staging_path="${www_path}subdomains/staging/httpdocs/"
staging_db_name="dbname"
staging_db_username="dbusername"
staging_db_pass="dbpass"
 
#Production info
prod_uri="testdeploy.${domain}"
root_prod_path="${www_path}"
prod_db_name="dbprod"
prod_db_username="dbusername"
prod_db_pass="dbpass"
 
#Create backup
backup_db_filename="database_$(date +%F_%Hh%M).bak.sql.bz2"
backup_file_filename="files_$(date +%F_%Hh%M).tar.bz2"
 
#Create files backups
tar -cjPf ${backup_path_files}prod_${backup_file_filename} ${root_prod_path}
chown ftproot:psaserv ${backup_path_files}prod_${backup_file_filename}
echo "Backup prod files done..."
tar -cjPf ${backup_path_files}staging_${backup_file_filename} ${root_staging_path}
chown ftproot:psaserv ${backup_path_files}staging_${backup_file_filename}
echo "Backup staging files done..."
 
mysqldump --add-drop-table -h ${staging_uri} -u ${staging_db_username} -p${staging_db_pass} ${staging_db_name} | bzip2 -c > ${backup_path_db}${backup_db_filename}
chown ftproot:psaserv ${backup_path_db}${backup_db_filename}
echo "All backups done..."
 
#Copy staging database to production database
mysqldump --add-drop-table -h ${staging_uri} -u ${staging_db_username} -p${staging_db_pass} ${staging_db_name} | mysql -h ${prod_uri} -u ${prod_db_username} -p${prod_db
_pass} ${prod_db_name}
echo "Database replication to production done..."
#Copy all files from staging to production
cp -rR  ${root_staging_path}. ${root_prod_path}
echo "Files copied from ${staging_uri} to ${prod_uri}"
#Update production tables
mysql -h ${prod_uri} -u ${prod_db_username} -p${prod_db_pass} ${prod_db_name}<<EOFMYSQL
UPDATE wp_posts SET post_content = REPLACE (post_content,'$staging_uri','$prod_uri');
UPDATE wp_posts SET guid = REPLACE (guid,'$staging_uri','$prod_uri');
UPDATE wp_posts SET guid = REPLACE (guid,'localhost','$prod_uri');
UPDATE wp_blc_instances SET raw_url = REPLACE (raw_url,'$staging_uri','$prod_uri');
UPDATE wp_blc_links SET url = REPLACE (url,'$staging_uri','$prod_uri');
UPDATE wp_blc_links SET final_url = REPLACE (final_url,'$staging_uri','$prod_uri');
UPDATE wp_contact_form_7 SET mail_2 = REPLACE (mail_2,'$staging_uri','$prod_uri');
UPDATE wp_contact_form_7 SET mail = REPLACE (mail,'$staging_uri','$prod_uri');
UPDATE wp_options SET option_value = REPLACE (option_value,'$staging_uri','$prod_uri');
UPDATE wp_options_bak SET option_value = REPLACE (option_value,'$staging_uri','$prod_uri');
UPDATE wp_usermeta SET meta_value = REPLACE (meta_value,'$staging_uri','$prod_uri');
UPDATE wp_sitemeta SET meta_value = REPLACE (meta_value,'$staging_uri','$prod_uri');
UPDATE wp_site SET domain = REPLACE (domain,'$staging_uri','$prod_uri');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value,'$staging_uri','$prod_uri');
UPDATE wp_blogs SET domain = REPLACE (domain,'$staging_uri','$prod_uri');
#Replace upload path
UPDATE wp_options SET option_value = REPLACE (option_value,'${root_staging_path}','${root_prod_path}');
UPDATE wp_options_bak SET option_value = REPLACE (option_value,'${root_staging_path}','${root_prod_path}');
#Blog replacement
UPDATE wp_2_posts SET post_content = REPLACE (post_content,'$staging_uri','$prod_uri');
UPDATE wp_2_posts SET guid = REPLACE (guid,'$staging_uri','$prod_uri');
UPDATE wp_2_postmeta SET meta_value = REPLACE (meta_value,'$staging_uri','$prod_uri');
UPDATE wp_2_options SET option_value = REPLACE (option_value,'$staging_uri','$prod_uri');
EOFMYSQL
echo "MySQL production database URI changed"
cp -f ${www_path}subdomains/testdeploy/conf/wp-config.php ${root_prod_path}wp-config.php
echo "wp-config.php copied"
mv ${root_prod_path}wp-content/w3tc-${staging_uri} ${root_prod_path}wp-content/w3tc-${prod_uri}
mv ${root_prod_path}wp-content/w3tc-fig-and-olive-blog.${staging_uri} ${root_prod_path}wp-content/w3tc-fig-and-olive-blog.${prod_uri}
mv ${root_prod_path}wp-content/w3-total-cache-config-${staging_uri}.php ${root_prod_path}wp-content/w3-total-cache-config-${prod_uri}.php
mv ${root_prod_path}wp-content/w3-total-cache-config-fig-and-olive-blog.${staging_uri}.php ${root_prod_path}wp-content/w3-total-cache-config-fig-and-olive-blog.${prod_uri}.php
chown ftproot:psaserv httpdocs/
 
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "Deployment on ${prod_uri} platform completed at $(date +%F_%Hh%M)"> $EMAILMESSAGE
echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail
mail -s "${SUBJECT}" "${EMAIL}" < $EMAILMESSAGE

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">