Rsync

Phing: Sync a complete website to remote server quickly

Submitted by Peter Majmesku on Mon, 08/29/2016 - 21:04

The famous Phing build tool (written in PHP) offers automation for repetitive tasks, which need to be executed by the Linux terminal. So you can sync an entire website (files and database) to the remote server. Here is an example of a Phing target to do the job:

    <target name="syncWebsiteToLive"
            description="Sync folder and files via Rsync to live server.">
        <echo msg="Syncing data to live server." />
        <exec command="rsync -a --progress --delete /home/peter/Websites/example.com/ --exclude=.git/ --exclude=.idea/ --exclude=sites/default/settings.php user@example.com:/var/www/example.com/"
              passthru="true"
              checkreturn="true" />
        <echo msg="Delete old database" />
        <exec command="ssh example.com 'mysql -uroot -pPASSWORD -e &quot;drop database example_com;&quot;'"
              passthru="true"
              checkreturn="true" />
        <echo msg="Create new empty database" />
        <exec command="ssh example.com 'mysql -uroot -pPASSWORD -e &quot;create database example_com;&quot;'"
              passthru="true"
              checkreturn="true" />
        <echo msg="Import database dump" />
        <exec command="ssh example.com 'mysql -uroot -pPASSWORD example_com &lt; /var/www/example.com/db_dumps/latest.sql'"
              passthru="true"
              checkreturn="true" />
    </target>

This target is later contained in your build.xml file. Afterwards you can execute the following command:

phing syncWebsiteToLive

The output will be the following:

Buildfile: /home/peter/build.xml
DevOps > syncWebsiteToLive:
     [echo] Syncing data to live server.
sending incremental file list
db_dumps/latest.sql
      7,515,285 100%  111.94MB/s    0:00:00 (xfr#1, ir-chk=1009/1040)
     [echo] Delete old database
     [echo] Create new empty database
     [echo] Import database dump
BUILD FINISHED
Total time: 5.7706 seconds

The website has been single by one single console command in an absolute minimum of time. This approach is good for a website in development stadium or a website which database can be just replaced. Then this way can save a lot of time compared to other approaches like ftp and manual database dump import. 

Rsync: Sync Two Folders via SSH in Both Directions

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:04

After started writing about rsync from local to live. Here's a post about syncing in two directions.

Helpful in rsync is, that also the permissions- and owner information is transfered ("-a" parameter). The "--progress" parameter shows the progress and "--exclude" allows to exclude directories. Be carefully about "--delete". Mind hidden folders like ".git" and ".vagrant" etc.
 

SSH session on local system (developer desktop) to sync files from (stage/live) server to local system

Here we assume you are in a ssh session on your local development environment and you want to sync from the stage/live server to your local development environment.

rsync -a --progress --delete source/ destination-host:/destination-directory/

Example:

rsync -a --progress --delete www/ --exclude=sites/default/files/ --exclude=.idea/ --exclude=.vagrant/ --exclude=provisioning/ --exclude=.git ssh-username@host.com:/srv/www/destination-folder/

From (stage/live) server to local system (developer desktop)

Here we assume you are in a ssh session on your stage/live server and you want to sync from your local development environment to stage/live server.

Example:

rsync -chavzP --stats --progress --delete username@host.com:/srv/www/some-folder/ /var/www/some-folder/

explainshell.com 

Detailed explanation for the commands can be found at explainshell.com. The rsync command above is explained here.

Happy syncing.

Rsync without changing permissions

The following command will sync the files and folders. It will also delete. But it will not change the permissions. It can be helpful, if you want to ensure, that your webserver is still able to read and execute the files. Like in a case, where I needed this command.

rsync -a --no-perms --progress --delete ./ user@host:/var/www/some-directory/

Just simulate, without real sync

Use the --dry-run option, to execute the command without any real file transfer. Then this command shows you, what "would" be transferred.

Tags
Subscribe to Rsync