wiki:faq/domain/make-dev-site-live

Version 2 (modified by Jamie McClelland, 9 years ago) (diff)

--

How do I copy my Drupal dev site to my Drupal live site?

Many of us maintain two different Drupal web sites, one is the live site (in the faq I will refer to that site as www.example.org) and one is a development site (dev.example.org). The development site is used to test out new code or a new design before it is ready to go live.

At a certain point, when your dev site is ready to go live, you need to copy your dev site to your live. Once you are done, your live site will be identical to your dev site. And, you can now continuing developing your dev site for the next iteration of your site.

Below are directions for creating a script that will make a backup copy of your live site and copy the dev site to the live site. These direcitons assume a familariaty with ssh. By creating a script, you can easily build a process that can be repeated in the future.

You can create the script on your local computer and sftp the script to your server, or if you are comfortable with command line editors like nano or vim, you can create directly from a ssh session.

In this example, let's call the script: sync-dev-to-live. It should be placed in the home directory of the user that controls the live site.

It should have the following contents:

#!/bin/bash

### Change the values below
path_to_live_site="/home/members/MEMBERSHORTNAME/sites/LIVEDOMAIN/web"
path_to_dev_site="/home/members/MEMBERSHORTNAME/sites/DEVDOMAIN/web"
live_site_db_name=
dev_site_db_user=
dev_site_db_pass=
dev_site_db_name=

### Optionally change the value below - by default the script will create
### a directory in the live-user's home directory called sync-staging which
### will hold temporary copies of the various sites
path_to_sync_staging="~/sync-staging"

### Optionally change the path below if your Drupal settings.php file is in a
### different location
settings_file="sites/default/settings.php"

### leave these values commented out UNLESS your dev site is on a different 
### server then your live site
# dev_ssh_login="user@host.mayfirst.org:"

### No need to change any other values

if [ ! -f ~/.my.cnf ]; then
	echo "I didn't find a .my.cnf file - which means "
	echo "I won't be able to automatically login to your "
	echo "database. Please create a .my.cnf file with your "
	echo "database details or request help from support.mayfirst.org"
	exit
fi

# remove trailing slash from web variables
path_to_live_site=$(echo $path_to_live_site|sed "s/\\/$//")
path_to_dev_site=$(echo $path_to_dev_site|sed "s/\\/$//")

live_backup="$path_to_sync_staging/live-backup"
dev_staging="$path_to_sync_staging/dev-staging"

if [ ! -d "$path_to_sync_staging" ]; then
	echo "Creating $path_to_sync_staging"
	mkdir "$path_to_sync_staging"
fi

if [ ! -d "$live-backup" ]; then
	echo "Creating $live-backup"
	mkdir "$live-backup"
fi

if [ ! -d "$dev_staging" ]; then
	echo "Creating $dev_staging"
	mkdir "$dev_staging"
fi

function interpret_reply() {
	if [ "$REPLY" = "y" ] || [ "$REPLY" == "Y" ] || [ -z "$REPLY" ]; then
		echo "Executing...."
		REPLY=y
	else
		echo "Not executing"
		REPLY=n
	fi
}

read -p "Back up live site files? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rsync -av "$path_to_live_site" "$live_backup/"
fi

read -p "Back up live site database? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	mysqldump "$live_site_name" > "$live_backup/${live_site_name}.sql"
fi

read -p "Synchronizing files from dev site to staging area? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rsync -av --exclude '/*settings.php' "${path_to_dev_site}" "${path_to_sync_staging}/"
fi

read -p "Dump dev database to staging area? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	mysqldump -u "$dev_db_user" -p${dev_db_pass} "$dev_db_name" > "${path_to_sync_staging/${dev_db_name}.sql"
fi

read -p "Copy live site settings.php to dev staging area? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	cp "${path_to_live_site}/$settings_file" "${path_to_sync_staging/web/$settings_file"
fi

echo "Your live site has been backed up and the dev site is staged to be moved"
echo "Please carefully examine $path_to_sync_staging to make sure everything is in place"
read -p "Press any key to make the dev site live or hit ctl-cancel to abort"

read -p "Delete live web site files? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rm -rf $path_to_live_site/*	
	rm $path_to_live_site/.htaccess
fi

read -p "Copy in dev  files? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	rsync -av "${path_to_sync_staging}/web/" "$path_to_live_site/"
fi

read -p "Import database? [Yn] "
interpret_reply
if [ "$REPLY" = "y" ]; then
	mysql < "$path_to_sync_staging/${dev_db_name}.sql"
fi

echo "All done."

Attachments (1)

Download all attachments as: .zip