Bash script to mirror directory and database of website between servers

5. Display Functions

The following functions are purely for aesthetic reasons and help keep certain visual elements uniform across the program.

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
### Display functions
 
# Display divider
function display_divider {
    echo '------------------------------------------------------------'
}
 
# Display new line
function display_newline {
    echo
}
 
# Display output start
function display_output_start {
    echo '> Starting...'
}
 
# Display output end
function display_output_end {
    echo '> Done.'
}
 
# Display program header
function display_header {
    display_newline
    display_divider
    echo " $TITLE"
    echo " $STATUS"
    echo " $ $NAME $arg1 $arg2"
    display_divider
    display_newline
}
 
# Display usage help
function display_usage {
    echo $USAGE
    display_newline
}
### Display functions

# Display divider
function display_divider {
	echo '------------------------------------------------------------'
}

# Display new line
function display_newline {
	echo
}

# Display output start
function display_output_start {
	echo '> Starting...'
}

# Display output end
function display_output_end {
	echo '> Done.'
}

# Display program header
function display_header {
	display_newline
	display_divider
	echo " $TITLE"
	echo " $STATUS"
	echo " $ $NAME $arg1 $arg2"
	display_divider
	display_newline
}

# Display usage help
function display_usage {
	echo $USAGE
	display_newline
}

6. MySQL Functions

There is only one function here, and it will be executed when dealing with WordPress MU sites. You can see the arguments it takes below. It will essentially replace the production domain name with the domain you’re using in the environment the program is being executed on.

### MySQL functions

# Update imported database to match local environment
# $1 	mysql hostname
# $2 	mysql username
# $3 	mysql password
# $4	mydql database
# $5 	domain name
function wp_mu_db_update {
	host=$1
	user=$2
	pass=$3
	db=$4
	domain=$5

	query="mysql --host=$host --user=$user --password=$pass $db -e"

	echo "> Setting 'domain' field in 'wp_blogs' table to '$domain'..."
	$query "UPDATE wp_blogs SET domain = '$domain' WHERE blog_id = 1 AND site_id = 1";

	echo "> Setting 'site_url' field in 'wp_options' table to 'http://$domain/wordpress'..."
	$query "UPDATE wp_options SET option_value = 'http://$domain/wordpress' WHERE option_name = 'siteurl'";

	echo "> Setting 'home' field in 'wp_options' to 'http://$domain'..."
	$query "UPDATE wp_options SET option_value = 'http://$domain/' WHERE option_name = 'home'";

	echo "> Setting 'domain' field in 'wp_site' to '$domain'..."
	$query "UPDATE wp_site SET domain = '$domain' WHERE id = 1";

	echo "> Setting 'site_url' field in 'wp_sitemeta' table to 'http://$domain/'..."
	$query "UPDATE wp_sitemeta SET meta_value = 'http://$domain/' WHERE meta_key = 'siteurl' AND site_id = 1";
}

You could add more functions that perform different actions for different platforms.

2 thoughts on “Bash script to mirror directory and database of website between servers

  1. mahender

    Hi,

    We have site for library.We will keep on updating the site.We are planing for mirror site for it.
    I have understood with the help of rsync we can update the /var/www folder of mirror site.
    How mysql database of mirror site will be in sync with production server.Could you please tell me how it can be achieved.

    Thanks in advance,
    Raja

    Reply
    1. Ryan Sechrest Post author

      The key commands are rsync (to synchronize files), mysqldump to export a database to a file, and mysql used to import a file into another database. With those three commands, all showcased in the above code, you can mirror files and databases between servers, or more specifically, keep one server (primary) synchronized with another (secondary).

      If you look at my development kit page, toward the very bottom are links to the individual documentation of those commands.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *