Jump to content

Working on a Webproject with PW


Bergy
 Share

Recommended Posts

@Bergy Sure, no problem.

The scripts make some assumptions:

1. you have ssh access to your server
2. you not only have access to your document root (-> mostly /var/www ...etc), but also to your /home/user directory
3. your /home/user is at the same time your ssh user
4. your rsa public key is located under /home/user/.ssh/o2u_rsa
5. All scripts are located under /home/user/

For more comfort register a (RSA) key of your client computer to the development remote server (with keygen you can easily create a key on your machine under linux) you also can generate a key under windows with putty (with some plugins, google therefor putty generate ssh key, ssh authentification without password).

# explanation regarding the use of  placeholders in the scripts:
# all placeholders are indicated with double braces -> {{ ... }}. Don't put your concrete names/data at all in braces, the double braces only indicate a placeholder
# {{dev dbname}} is a placeholder for your database name on the development server/webspace. EDIT: Or is it actually the db-username?
# {{prod dbname}} is a placeholder for your database name on the production server/webspace. EDIT: Or is it actually the db-username?
# {{dev server user}} is a placeholder for your username on the dev server/webspace.
# {{prod server user}} is a placeholder for your username on the prod server/webspace.
# {{prod.server.name}} is a placeholder for your production server name.


These scripts are working successfully at my hoster Uberspace
The hosting os used by my hoster is Centos Linux (6)

Feel free to correct me, or optimize things, always glad if i can learn something

Now the Following describes the scenario Deployment of a new dev version of a website to the production environment / server / webspace

The main script is deploy-dev-prod.sh. It executes 2 further scripts, like a php include.

Code deploy-dev-prod.sh

#!/bin/bash

# Deployment Script:
# Deploys the website from development environment to production environment 
# should be placed in your /home/user folder on the server

# Usage:
#
# chmod +x deploy-dev-prod.sh
# ./deploy-dev-prod.sh 


# If a sqldump-dev-backup.sql File already exists rename sqldump-dev-backup.sql to sqldump-dev-backup2.sql
if [ -e sqldump-dev-backup.sql ]
then
	mv sqldump-dev-backup.sql sqldump-dev-backup2.sql
	printf "\nSource Server: Done - rename sqldump-dev-backup.sql to sqldump-dev-backup2.sql\n\waiting for next step ...\n\n"
fi

# create MySQL dumpfile on source server
mysqldump  {{dev dbname}} > sqldump-dev-backup.sql
# Attention: mysqldump --databases {{dev dbname}} > name.sql would export the database per se, which is not desired here. In our case only the tables should be exported in order to not need to adjust the database credentials on the target server (-> site/config.php). 
if [ "$?" -eq "0" ]
then 
	printf "\nSource Server: Done - create sqldump-dev-backup.sql\n\nwaiting for next step ...\n\n"
	# if creation of sqldump-dev-backup.sql was successful, remove sqldump-dev-backup2.sql, 1 backup is sufficient
	if [ -e sqldump-dev-backup2.sql ]
	then
		rm sqldump-dev-backup2.sql
		printf "\nSource Server: Done - remove sqldump-dev-backup2.sql\n\nwaiting for next step ...\n\n"
	fi
else
	printf "\nSource Server: Error: creation of sqldump-dev-backup.sql failed\n\n"
fi

# rsync sqldump-dev-backup.sql to production server/webspace in ~ (= home folder)
rsync -e 'ssh -i /home/{{dev server user}}/.ssh/o2u_rsa' -vaH /home/{{dev server user}}/sqldump-dev-backup.sql {{prod server user}}@{{prod.server.name}}:/home/{{prod server user}}/

# Was rsync successful?
if [ "$?" = "0" ]
then
	printf "\nDone - sending file sqldump-dev-backup.sql to target server\n\nwaiting for next step ...\n\n"
else 
	printf "\nError - sending file sqldump-dev-backup.sql to target server failed\n\n"
fi

# Backup Target Server (Auth via SSH Key)
ssh -i ~/.ssh/o2u_rsa {{prod server user}}@{{prod.server.name}} 'bash -s' < backup-prod-server.sh

# rsync cms processwire site/ files from dev to prod 
rsync -e 'ssh -i /home/{{dev server user}}/.ssh/o2u_rsa' -vaH --log-file=rsync.log --exclude=config.php --exclude=assets/cache {{/absolute/path/on/your/dev/webspace/to/processwire/site/}} {{prod server user}}@{{prod.server.name}}:{{/absolute/path/on/your/prod/webspace/to/processwire/site}}
# attention: the dev path (= source) has a trailing slash, the prod path (= target) hasn't ! Because we want to copy THE CONTENT of /dev/site/ to prod/site
# Was rsync successful?
if [ "$?" = "0" ]
then
	printf "\nDone - rsync content of site folder with target server\n\nwaiting for next step ...\n\n"
else 
	printf "\nError - rsync site folder failed\n\n"
fi

# Update CMS Database Target Server (Auth via SSH Key)
ssh -i ~/.ssh/o2u_rsa {{prod server user}}@{{prod.server.name}} 'bash -s' < update-prod-server.sh

 

backup-prod-server.sh

 

#!/bin/bash
# This script is part of deploy-dev-prod.sh and shouldn't be executed standalone.

# if a sqldump-prod-backup.sql file already exists, rename sqldump-prod-backup.sql to sqldump-prod-backup2.sql
if [ -e sqldump-prod-backup.sql ]
then
	mv sqldump-prod-backup.sql sqldump-prod-backup2.sql
	printf "\nTarget Server: Done - rename sqldump-prod-backup.sql to sqldump-prod-backup2.sql\n\nwaiting for next step ...\n\n"
fi


# create MySQL dumpfile on target server
mysqldump  {{prod dbname}} > sqldump-prod-backup.sql
# Attention: mysqldump --databases {{prod dbname}} > name.sql would export the database per se, which is not desired here. In our case only the tables should be exported in order to not need to adjust the database credentials on the target server (-> sit/config.php).
if [ "$?" -eq "0" ]
then
	printf "\nTarget Server: Done - create sqldump-prod-backup.sql\n\nwaiting for next step ...\n\n"
	# if creation of sqldump-prod-backup.sql was successful, remove sqldump-prod-backup2.sql, 1 backup is sufficient
	if [ -e sqldump-prod-backup2.sql ]
	then
		rm sqldump-prod-backup2.sql
		printf "\nTarget Server: Done - remove sqldump-prod-backup2.sql\n\nwaiting for next step ...\n\n"
	fi
else
	printf "\nTarget Server: Error: creation of sqldump-prod-backup.sql failed\n\n"
fi

# copy {{/path/to/site/}}  as backup to ~/site-backup/
if [ ! -d site-backup ] 
then
	mkdir site-backup
fi
# always delete an existing folder before copying content into a folder with the same name
rm -R site-backup && cp -R /absolute/path/to/site/ site-backup 
# was site backup successful?
if [ "$?" = "0" ]
then
	printf "\nTarget Server: Done - backup current site folder\n\nwaiting for next step ...\n\n"
else 
	printf "\nTarget Server: Error - backup current site folder failed\n\n"
fi 

 and finally update-prod-server.sh

#!/bin/bash
# This script is part of deploy-dev-prod.sh and shouldn't be executed standalone.
#

# import database tables of source server into database of the target server
mysql  {{prod dbname}} < sqldump-dev-backup.sql
# was import of sql dumpfile successful?
if [ "$?" -eq "0" ]
then 
	printf "\nTarget Server: Done - import sqldump-dev-backup.sql\n\nDeployment finished successfully. Now reload the website"
else
	printf "\nTarget Server: Error: import of sqldump-dev-backup.sql failed\n\n"
fi

 

For updating Processwire i use pw-upgrade.sh:

#!/bin/sh

# credits to https://gist.github.com/craigrodway/66c9633ae5d865a9b090
#
# ProcessWire upgrade script
#
# Upgrades ProcessWire ./wire directory.
# Use either master or dev branch.
#
#
# Usage:
#
# chmod +x ./pw-upgrade.sh
# ./pw-upgrade.sh 

# go 1 level over document root:
cd {{/absolute/path/to/one/level/over/document-root}}
# replace this path with your actual path without curly brackets

# if processwire-master-backup exists, rename processwire-master-backup to processwire-master-backup2
if [ -e processwire-master-backup ]
then
	mv processwire-master-backup processwire-master-backup2
	printf "\nDone - rename processwire-master-backup to processwire-master-backup2\n\nwaiting for next step ...\n\n"
fi 
	
# rename processwire-master to processwire-master-backup
mv processwire-master processwire-master-backup
printf "\nDone - rename current processwire-master to processwire-master-backup\n\nwaiting for next step ...\n\n"

# download new version as tmp.zip - unzip it - and remove tmp.zip afterwards
wget -qO-  -O tmp.zip https://github.com/processwire/processwire/archive/master.zip && unzip tmp.zip && rm tmp.zip
if [ "$?" -eq "0" ]
then 
	printf "\nDone - downloaded new master\n\nwaiting for next step ...\n\n"
else
	printf "\nError - Download of new master failed\n\n"
fi 

# delete processwire-master-backup2
rm -r processwire-master-backup2
if [ "$?" -eq "0" ]
then
	printf "\nDone - removed processwire-master-backup2 because we need only one backup version\n\nwaiting for next step ...\n\n"
else
	printf "\nError - processwire-master-backup2 couldn't be removed\n\n"
fi

# in html/ delete wire and index.php and replace it with wire and index.php from new processwire-master
cd html/
rm -R wire && rm index.php && cp -R ../processwire-master/wire/ wire && cp ../processwire-master/index.php index.php
if [ "$?" -eq "0" ]
then 
	printf "\nDone - replace wire and index.php with the ones from new processwire-master\n\n"
	printf "\nUpgrade finished - now login in CMS backend and do some reloads\n\n"
fi 

 

Hope this is a bit of a help or inspiration.

Feel free to give me your opinion. The only problem is, i'll be on holidays the next 10-12 days and not available. But afterwards i have a look at this thread.

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...