Bash script to mirror directory and database of website between servers

9. Run Program

Last, but not least, this is what starts the program.

### Run Program

display_header
run
exit 0

Summary

If you save all this code in a file called site, copy it into the /usr/bin directory on the target server, give it 750 permissions (chmod 750 /usr/bin/site), then execute it:

[[email protected] ~]# site

------------------------------------------------------------
 Site v1.0.0 on DEV01.DOMAIN.COM
 10/22/2013 06:05:16 PM UTC by root
 $ site  
------------------------------------------------------------

Usage: site [sync|update] [sitename]

And then to synchronize:

[[email protected] ~]# site sync sitename

------------------------------------------------------------
 Site v1.0.0 on DEV01.DOMAIN.COM
 10/22/2013 06:07:46 PM UTC by root
 $ site sync sitename
------------------------------------------------------------

> Starting...
> Synchronizing production files with temporary directory...
  Source      : [email protected]:/var/www/domains/domain.com/www/htdocs/wp-content/uploads/
  Destination : /opt/sites/domain/uploads/
[email protected]'s password: 
receiving incremental file list
[...]
sent 770 bytes  received 1484485 bytes  118820.40 bytes/sec
total size is 1267533727  speedup is 853.41

> Exporting production database to temporary directory...
  Database    : domain_com
  Destination : /opt/sites/domain
  Filename    : domain_com.sql
Enter password: 
-- Connecting to hostname...
[...]
-- Disconnecting from hostname...
> Done.

And lastly to update:

[[email protected] ~]# site update sitename

------------------------------------------------------------
 Site v1.0.0 on DEV01.DOMAIN.COM
 10/22/2013 06:11:30 PM UTC by root
 $ site update sitename
------------------------------------------------------------

> Starting...
> Synchronizing temporary files with local site directory...
  Source      : /opt/sites/domain/uploads/
  Destination : /var/www/domains/domain.com/www/htdocs/wp-content/uploads/
> Importing temporary database export into local database...
  Filename : domain_com.sql
  Source   : /opt/sites/domain
  Database : domain_com
> Setting 'domain' field in 'wp_blogs' table to 'dev01.domain.com'...
> Setting 'site_url' field in 'wp_options' table to 'http://dev01.domain.com/wordpress'...
> Setting 'home' field in 'wp_options' to 'http://dev01.domain.com'...
> Setting 'domain' field in 'wp_site' to 'dev01.domain.com'...
> Setting 'site_url' field in 'wp_sitemeta' table to 'http://dev01.domain.com/'...
> Done.

Hopefully this is useful to someone else. If you have any questions or comments, especially improvement suggestions, please leave them below.

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 *