Shell script for cloning a WordPress installation

The shell script below will copy a WordPress installation from one server to another- including the database. The script uses the ‘sed’ command to update the new wp-config.php with the new server information, as well as replace all references to the original domain (such as in post_content) in the new DB with references to the new domain.  I wrote the script in order to create a ‘one-click’ solution to mirroring a WP installation.  It will work with both single and multisite installations.

WARNING: Use at your own risk.  I recommend that you manually back up the original WP installation until you are confident that the script is configured correctly.  It is possible to inadvertently alter or erase your original installation if you do not configure the script’s variables correctly. The script was written in the Mac OS environment and may require some alterations to work in your environment.

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
#!/bin/sh
echo "WP Clone Start"
START=$(date +%s)
 
SH_PATH="/Users/johndoe/Desktop/" # Path to this shell script
MYSQL_PATH="/usr/local/mysql-5.1.60-osx10.6-x86_64/bin" # Path to mysql and mysqldump
 
OLD_PATH="/Volumes/live" # Path to original WP
OLD_DOMAIN="live.myserver.com" # Domain for original WP
OLD_HOST="db.myserver.com" # Database host for original WP
OLD_USER="liveuser" # Database user for original WP
OLD_PASS="livepassword" # Database password for original WP
OLD_DB="wp_live" # Database name for original WP
 
NEW_PATH="/Volumes/dev" # Path to new WP, folder must already exist
NEW_DOMAIN="dev.myserver.com" # Domain for new WP
NEW_HOST="db.myserver.com" # Database host for new WP
NEW_USER="devuser" # Database user for new WP
NEW_PASS="devpassword" # Database password for new WP
NEW_DB="wp_dev" # Database name for new WP, database must already exist
 
datetime() { echo `date "+%Y-%m-%d %H:%M:%S"` ;}
 
echo $(datetime) "Dumping $OLD_DB"
 
cd "$MYSQL_PATH"
./mysqldump $OLD_DB -h$OLD_HOST -u$OLD_USER -p$OLD_PASS > "$SH_PATH/$OLD_DB.sql"
 
echo $(datetime) "Editing $OLD_DB.sql > $NEW_DB.sql"
 
cd "$SH_PATH"
sed 's/'$OLD_DOMAIN'/'$NEW_DOMAIN'/g' $OLD_DB.sql > $NEW_DB.sql
 
echo $(datetime) "Importing $NEW_DB.sql to $NEW_DB"
 
cd "$MYSQL_PATH"
./mysql $NEW_DB -h$NEW_HOST -u$NEW_USER -p$NEW_PASS < "$SH_PATH/$NEW_DB.sql"
 
echo $(datetime) "Removing $OLD_DB.sql and $NEW_DB.sql"
 
cd "$SH_PATH"
rm $OLD_DB.sql
rm $NEW_DB.sql
 
echo $(datetime) "Removing $NEW_PATH contents"
 
rm -rf "$NEW_PATH/"*
 
echo $(datetime) "Copying files from $OLD_PATH to $NEW_PATH"
 
cp -r "$OLD_PATH/"* "$NEW_PATH"
 
echo $(datetime) "Editing wp-config.php"
 
cd "$NEW_PATH"
sed 's/'$OLD_DOMAIN'/'$NEW_DOMAIN'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
sed 's/'$OLD_USER'/'$NEW_USER'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
sed 's/'$OLD_DB'/'$NEW_DB'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
sed 's/'$OLD_PASS'/'$NEW_PASS'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
 
DIFF=$(( $(date +%s) - $START ))
echo $(datetime) "WP Clone Complete in $DIFF seconds"
#!/bin/sh
echo "WP Clone Start"
START=$(date +%s)

SH_PATH="/Users/johndoe/Desktop/" # Path to this shell script
MYSQL_PATH="/usr/local/mysql-5.1.60-osx10.6-x86_64/bin" # Path to mysql and mysqldump

OLD_PATH="/Volumes/live" # Path to original WP
OLD_DOMAIN="live.myserver.com" # Domain for original WP
OLD_HOST="db.myserver.com" # Database host for original WP
OLD_USER="liveuser" # Database user for original WP
OLD_PASS="livepassword" # Database password for original WP
OLD_DB="wp_live" # Database name for original WP

NEW_PATH="/Volumes/dev" # Path to new WP, folder must already exist
NEW_DOMAIN="dev.myserver.com" # Domain for new WP
NEW_HOST="db.myserver.com" # Database host for new WP
NEW_USER="devuser" # Database user for new WP
NEW_PASS="devpassword" # Database password for new WP
NEW_DB="wp_dev" # Database name for new WP, database must already exist

datetime() { echo `date "+%Y-%m-%d %H:%M:%S"` ;}

echo $(datetime) "Dumping $OLD_DB"

cd "$MYSQL_PATH"
./mysqldump $OLD_DB -h$OLD_HOST -u$OLD_USER -p$OLD_PASS > "$SH_PATH/$OLD_DB.sql"

echo $(datetime) "Editing $OLD_DB.sql > $NEW_DB.sql"

cd "$SH_PATH"
sed 's/'$OLD_DOMAIN'/'$NEW_DOMAIN'/g' $OLD_DB.sql > $NEW_DB.sql

echo $(datetime) "Importing $NEW_DB.sql to $NEW_DB"

cd "$MYSQL_PATH"
./mysql $NEW_DB -h$NEW_HOST -u$NEW_USER -p$NEW_PASS < "$SH_PATH/$NEW_DB.sql"

echo $(datetime) "Removing $OLD_DB.sql and $NEW_DB.sql"

cd "$SH_PATH"
rm $OLD_DB.sql
rm $NEW_DB.sql

echo $(datetime) "Removing $NEW_PATH contents"

rm -rf "$NEW_PATH/"*

echo $(datetime) "Copying files from $OLD_PATH to $NEW_PATH"

cp -r "$OLD_PATH/"* "$NEW_PATH"

echo $(datetime) "Editing wp-config.php"

cd "$NEW_PATH"
sed 's/'$OLD_DOMAIN'/'$NEW_DOMAIN'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
sed 's/'$OLD_USER'/'$NEW_USER'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
sed 's/'$OLD_DB'/'$NEW_DB'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php
sed 's/'$OLD_PASS'/'$NEW_PASS'/g' wp-config.php > wp-config-temp.php
rm wp-config.php
mv wp-config-temp.php wp-config.php

DIFF=$(( $(date +%s) - $START ))
echo $(datetime) "WP Clone Complete in $DIFF seconds"

Multisite Dashboard Switcher for WordPress Released

The Multisite Dashboard Switcher is a plugin written for WordPress that allows multisite administrators to easily switch between Dashboards. MSDS provides convenient access to options pages across every site in the network, reducing the number of clicks necessary to manage settings. For larger networks, sites can be grouped by letter.

View the plugin on WordPress.org

Use PHP and LDAP to list members of an Active Directory group (Improved)

PHP function that gets the members of an Active Directory group, and returns the Users’ attributes as an array.

This is an improved version of the snippet posted on 2/10/2011

The Function

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
<?php
function get_members($group=FALSE) {
    // Active Directory server
    $ldap_host = "ad.domain";
 
    // Active Directory DN
    $ldap_dn = "CN=Users,DC=ad,DC=domain";
 
    // domain, for purposes of constructing $user
    $ldap_usr_dom = "@".$ldap_host;
 
    // Active Directory user
    $user = "jdoe";
    $password = "password";
 
    // User attributes we want to keep
    // List of User Object properties: http://www.dotnetactivedirectory.com/Understanding_LDAP_Active_Directory_User_Object_Properties.html
    $keep = array(
        "samaccountname",
        "distinguishedname"
    );
 
    // Connect and search AD
    $ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP");
    ldap_bind($ldap,$user.$ldap_usr_dom,$password) or die("Could not bind to LDAP");
 
    // filter by memberOf, if group is set
    if($group) $addtl = "(memberOf=CN=$group,$ldap_dn)"; else $addtl = "";
 
    $results = ldap_search($ldap,$ldap_dn,"(&(objectClass=User)$addtl)");
    $entries = ldap_get_entries($ldap, $results);
 
    // Remove first entry (it's always blank)
    array_shift($entries);
 
    $output = array(); // Declare the output array
 
    $i = 0; // Counter
    // Build output array
    foreach($entries as $u) {
        foreach($keep as $x) $output[$i][$x] = $u[$x][0];
        $i++;
    }
 
    return $output;
}
 
print_r(get_members()); // Gets all users in 'Users'
print_r(get_members("Test Group")); // Gets all members of 'Test Group'
?>
<?php
function get_members($group=FALSE) {
	// Active Directory server
	$ldap_host = "ad.domain";

	// Active Directory DN
	$ldap_dn = "CN=Users,DC=ad,DC=domain";

	// domain, for purposes of constructing $user
	$ldap_usr_dom = "@".$ldap_host;

	// Active Directory user
	$user = "jdoe";
	$password = "password";

	// User attributes we want to keep
	// List of User Object properties: http://www.dotnetactivedirectory.com/Understanding_LDAP_Active_Directory_User_Object_Properties.html
	$keep = array(
	    "samaccountname",
	    "distinguishedname"
	);

	// Connect and search AD
	$ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP");
	ldap_bind($ldap,$user.$ldap_usr_dom,$password) or die("Could not bind to LDAP");

	// filter by memberOf, if group is set
	if($group) $addtl = "(memberOf=CN=$group,$ldap_dn)"; else $addtl = "";

	$results = ldap_search($ldap,$ldap_dn,"(&(objectClass=User)$addtl)");
	$entries = ldap_get_entries($ldap, $results);

	// Remove first entry (it's always blank)
	array_shift($entries);

	$output = array(); // Declare the output array

	$i = 0; // Counter
	// Build output array
	foreach($entries as $u) {
		foreach($keep as $x) $output[$i][$x] = $u[$x][0];
		$i++;
	}

	return $output;
}

print_r(get_members()); // Gets all users in 'Users'
print_r(get_members("Test Group")); // Gets all members of 'Test Group'
?>

Example Output

Array
(
    [0] => Array
        (
            [samaccountname] => sam
            [distinguishedname] => CN=sam,CN=Users,DC=ad,DC=domain
        )

    [1] => Array
        (
            [samaccountname] => jdoe
            [distinguishedname] => CN=John Doe,CN=Users,DC=ad,DC=domain
        )

)