Contents |
FileSyncTask is a Phing extension for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer. FileSyncTask can copy or display directory contents and copy files, optionally using compression and recursion.
Rather than using FTP or some other form of file transfer, FileSyncTask uses rsync to copy only the diffs of files that have actually changed. Only actual changed pieces of files are transferred, rather than the whole file, which results in transferring only a small amount of data and are very fast. FTP, for example, would transfer the entire file, even if only one byte changed. The tiny pieces of diffs are then compressed on the fly, further saving you file transfer time and reducing the load on the network.
FileSyncTask can be used to synchronize Website trees from staging to production servers and to backup key areas of the filesystems.
Here are some other key features of FileSyncTask:
| Name | Type | Description | Default | Required |
|---|---|---|---|---|
| sourcedir | String | Local or remote source directory. | null | Yes |
| destinationdir | String | Local or remote destination directory. | null | Yes |
| excludedir | String | Specifies the path to the sync.exclude file. | null | No |
| backupdir | String | Makes a backup of each existing destination file. | null | No |
| remoteshell | String | Allows you to choose an alternative remote shell program. | ssh | No |
| listonly | Boolean | Causes the source files to be listed instead of transferred. | false | No |
| verbose | Boolean | Increases the amount of information you are given during the transfer. | false | No |
| delete | Boolean | Deletes files that don't exist on sender. | false | No |
example.com
|-- build
| |-- build.properties
| |-- build.xml
| |-- sync.exclude
| `-- sync.properties
`-- public
`-- index.php
The SSH client called by FileSyncTask uses connection settings from the sync.properties file:
#
# Base directory
#
sync.project.dir=/home/example.com
#
# Source directory
#
sync.source.projectdir=${sync.project.dir}/public
#
# Destination directory
#
sync.destination.projectdir=${sync.project.dir}
#
# Remote connection details
#
sync.remote.host=remote-server.com
sync.remote.user=example
#
# Backup directory: Destination files are copied to a backup directory as
# each file is transferred or deleted.
#
sync.destination.backupdir=${sync.destination.projectdir}_backup
#
# Exclude patterns listed in a file: The filter rules allow for flexible selection of
# which files to to exclude. As the list of files/directories to transfer is built,
# rsync checks each name to be transferred against the list of exclude patterns in turn,
# if it is an exclude pattern, then that file is skipped.
#
sync.exclude.file=/home/example.com/build/sync.exclude
There are 4 different ways of using FileSyncTask:
The "listonly" option will cause the modified files to be listed instead of transferred. You must specify a source and a destination, one of which may be remote.
<taskdef name="sync" classname="phing.tasks.ext.FileSyncTask" /> <sync sourcedir="${sync.source.projectdir}" destinationdir="${sync.destination.projectdir}" listonly="true" verbose="true" />
To exclude files from synchronizations, open and edit the sync.exclude file under the build/ directory. Each line can contain a file, a directory, or a pattern:
*~ _* .svn .htaccess public/index.development.php public/index.staging.php public/images/uploads/* public/favicon.ico build/* log/* tmp/*
The "excludefile" option specifies the path to the sync.exclude file:
<taskdef name="sync" classname="phing.tasks.ext.FileSyncTask" /> <sync sourcedir="${sync.source.projectdir}" destinationdir="${sync.destination.projectdir}" listonly="true" excludefile="${sync.exclude.file}" verbose="true" />
The following task definition will transfer files from a local source to a local destination directory, excluding irrelevant files and providing a visual feedback on what's happening:
<taskdef name="sync" classname="phing.tasks.ext.FileSyncTask" /> <sync sourcedir="${sync.source.projectdir}" destinationdir="${sync.destination.projectdir}" excludefile="${sync.exclude.file}" verbose="true" />
The following task definition will transfer files from a local source to a remote destination:
<taskdef name="sync" classname="phing.tasks.ext.FileSyncTask" /> <sync sourcedir="${sync.source.projectdir}" destinationdir="${sync.remote.user}@${sync.remote.host}:${sync.destination.projectdir}" excludefile="${sync.exclude.file}" verbose="true" />
The following task definition will transfer files from a remote source to a local destination:
<taskdef name="sync" classname="phing.tasks.ext.FileSyncTask" /> <sync sourcedir="${sync.remote.user}@${sync.remote.host}:${sync.source.projectdir}" destinationdir="${sync.destination.projectdir}" excludefile="${sync.exclude.file}" verbose="true" />
The "backupdir" option allows you to copy destination files to a backup directory as each file is transferred or deleted:
<taskdef name="sync" classname="phing.tasks.ext.FileSyncTask" /> <sync sourcedir="${sync.remote.user}@${sync.remote.host}:${sync.source.projectdir}" destinationdir="${sync.destination.projectdir}" backupdir="${sync.destination.backupdir}" excludefile="${sync.exclude.file}" verbose="true}" />
$ svn checkout http://svn.fedecarg.com/repo/Phing/tasks/ext/