Bash script to mirror directory and database of website between servers

8. Run Functions

Since we have two things (synchronize and update) the program can do, we need at least two functions to take care of this.

  1. run_sync – Get everything from production and store it in the temporary directory.
  2. run_update – Copy everything from the temporary directory into the local site.

This is the place where you define the specifics for each local site, like where the files need to be copied to, what the database name is, etc.

In addition, there is one other function below those two, which I call the main component, and it either calls run_sync or run_update, depending on the arguments the user provided.

8.1 Run Synchronize

The program will mirror the production files and database with the temporary directory.

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
# Run sync component
function run_sync {
    case $arg2 in
 
        sitename)
            display_output_start
 
            copy_from="$SSH_PRD_USERNAME@$SSH_PRD_HOSTNAME:$PRD_SITES_PATH/domain.com/www/htdocs/wp-content/uploads/"
            copy_to="$TEMP_SITES_PATH/$arg2/uploads/"
            exclude='--exclude=temp --exclude=wpcf7_uploads'
            download_files "$copy_from" "$copy_to" "$exclude"
 
            display_newline
 
            mysql_hostname=$MYSQL_PRD_HOSTNAME
            mysql_username=$MYSQL_PRD_USERNAME
            mysql_database='domain_com'
            export_to="$TEMP_SITES_PATH/$arg2"
            ignore="--ignore-table=$mysql_database.wp_w3tc_cdn_queue"
            export_database $mysql_hostname $mysql_username $mysql_database "$export_to" "$ignore"
 
            display_output_end
            ;;
 
        *)
            display_usage
            ;;
 
    esac
}
# Run sync component
function run_sync {
	case $arg2 in

		sitename)
			display_output_start

			copy_from="[email protected]$SSH_PRD_HOSTNAME:$PRD_SITES_PATH/domain.com/www/htdocs/wp-content/uploads/"
			copy_to="$TEMP_SITES_PATH/$arg2/uploads/"
			exclude='--exclude=temp --exclude=wpcf7_uploads'
			download_files "$copy_from" "$copy_to" "$exclude"

			display_newline

			mysql_hostname=$MYSQL_PRD_HOSTNAME
			mysql_username=$MYSQL_PRD_USERNAME
			mysql_database='domain_com'
			export_to="$TEMP_SITES_PATH/$arg2"
			ignore="--ignore-table=$mysql_database.wp_w3tc_cdn_queue"
			export_database $mysql_hostname $mysql_username $mysql_database "$export_to" "$ignore"

			display_output_end
			;;

		*)
			display_usage
			;;

	esac
}

Things to edit:

  • Line 5: sitename
  • Line 8: domain.com/www/htdocs/wp-content/uploads
  • Line 9: uploads
  • Line 10: –exclude=temp –exclude=wpcf7_uploads
  • Line 17: domain_com
  • Line 19: –ignore-table=$mysql_database.wp_w3tc_cdn_queue

8.2 Run Update

The program will mirror the temporary directory with the equivalent local site files and database.

Note that you’ll need to update the sitename, the path to where it should be copied to, and the different database credentials for 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
# Run update component
function run_update {
    case $arg2 in
 
        sitename)
            display_output_start
 
            copy_from="$TEMP_SITES_PATH/$arg2/uploads/"
            copy_to="$LOCAL_SITES_PATH/domain.com/www/htdocs/wp-content/uploads/"
            upload_files "$copy_from" "$copy_to"
 
            mysql_username=''
            mysql_password=''
            domain_name=''
            case $ENV in
                DEV)
                    mysql_username='username'
                    mysql_password='password'
                    domain_name='dev01.domain.com'
                    ;;
                STG)
                    mysql_username='username'
                    mysql_password='password'
                    domain_name='stg01.domain.com'
                    ;;
            esac
            if [[ (-n $mysql_username) && (-n $mysql_password) && (-n $domain_name) ]]; then
                mysql_hostname='localhost'
                mysql_database='domain_com'
                import_from="$TEMP_SITES_PATH/$arg2"
                import_database $mysql_hostname $mysql_username $mysql_password $mysql_database "$import_from"
                wp_mu_db_update $mysql_hostname $mysql_username $mysql_password $mysql_database $domain_name
            else
                echo "> Unable to obtain MySQL credentials for $ENV server"
            fi
 
            display_output_end
            ;;
 
        *)
            display_usage
            ;;
 
    esac
}
# Run update component
function run_update {
	case $arg2 in

		sitename)
			display_output_start

			copy_from="$TEMP_SITES_PATH/$arg2/uploads/"
			copy_to="$LOCAL_SITES_PATH/domain.com/www/htdocs/wp-content/uploads/"
			upload_files "$copy_from" "$copy_to"

			mysql_username=''
			mysql_password=''
			domain_name=''
			case $ENV in
				DEV)
					mysql_username='username'
					mysql_password='password'
					domain_name='dev01.domain.com'
					;;
				STG)
					mysql_username='username'
					mysql_password='password'
					domain_name='stg01.domain.com'
					;;
			esac
			if [[ (-n $mysql_username) && (-n $mysql_password) && (-n $domain_name) ]]; then
				mysql_hostname='localhost'
				mysql_database='domain_com'
				import_from="$TEMP_SITES_PATH/$arg2"
				import_database $mysql_hostname $mysql_username $mysql_password $mysql_database "$import_from"
				wp_mu_db_update $mysql_hostname $mysql_username $mysql_password $mysql_database $domain_name
			else
				echo "> Unable to obtain MySQL credentials for $ENV server"
			fi

			display_output_end
			;;

		*)
			display_usage
			;;

	esac
}

Things to edit:

  • Line 5: sitename
  • Line 8: uploads
  • Line 9: domain.com/www/htdocs/wp-content/uploads
  • Line 17: username
  • Line 18: password
  • Line 19: dev01.domain.com
  • Line 22: username
  • Line 23: password
  • Line 24: stg01.domain.com
  • Line 28: localhost
  • Line 29: domain_com

8.3 Run

This is responsible for routing the user’s request.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Run main component
function run {
    case $arg1 in
 
        sync)
            run_sync
            display_newline
            ;;
 
        update)
            run_update
            display_newline
            ;;
 
        *)
            display_usage
            ;;
 
    esac
}
# Run main component
function run {
	case $arg1 in

		sync)
			run_sync
			display_newline
			;;

		update)
			run_update
			display_newline
			;;

		*)
			display_usage
			;;

	esac
}

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 *