Wallogit.com
2017 © Pedro Peláez
Lightweigt rsync wrapper without dependencies
PHP 5.4+, rsync, composer, (*1)
Use Composer to install the package, (*2)
From project root directory execute, (*3)
composer install, (*4)
or, (*5)
composer require codeplayr/rsyncer, (*6)
Composer will take care of autoloading. Just include the autoloader at the top of the file, (*7)
require_once __DIR__ . '/vendor/autoload.php';, (*8)
See following example:, (*9)
use \Codeplayr\Rsyncer\Option;
use \Codeplayr\Rsyncer\SSH;
use \Codeplayr\Rsyncer\Rsync;
$source = __DIR__ . '/src/';
$destination = __DIR__ . '/backup/';
$date = date('Y-m-d', time());
//rsync options
$option = new Option([
Option::FILES_FROM => __DIR__ . '/rules.txt',
Option::EXCLUDE_FROM=> __DIR__ . '/exclude-rules.txt',
Option::LOG_FILE => __DIR__ . "/logs/{$date}.log",
Option::ARCHIVE => false,
Option::LINKS => true,
Option::TIMES => true,
Option::RECURSIVE => true,
Option::VERBOSE => true,
Option::COMPRESS => true,
Option::CHECKSUM => true,
Option::DRY_RUN => false,
]);
//add additional flags
$option->addFlag('human-readable')
->addArgument('exclude', '/path/to/exclude')
->addArgument('include', '*.html')
->addArgument('include', '*.php')
->addArgument('include', '*/')
->addArgument('exclude', '*');
//optional ssh connection to remote host
$ssh = new SSH([
SSH::USERNAME => 'root',
SSH::HOST => '1.2.3.4',
SSH::PORT => 22,
SSH::IDENTITY_FILE => '/path/to/private/key',
]);
//configuration options
$conf = [
Rsync::SHOW_OUTPUT => true,
];
$rsnyc = new Rsync( $option, $ssh, $conf );
//assemble and show Command
echo $rsnyc->getCommand( $source, $destination );
//start syncing directories
if( ! $rsnyc->sync( $source, $destination ) ){
echo $rsnyc->getMessage()->toString();
}
Running the script generates following rsync command and options:, (*10)
rsync
-ltrvzc
--human-readable
--files-from="/path/to/rules.txt"
--exclude-from="/path/to/exclude-rules.txt"
--log-file="/path/to/logs/2016-10-29.log"
--exclude='/path/to/exclude'
--include="*.html"
--include="*.php"
--include="*/"
--exclude="*"
-e="ssh -i /path/to/your/private/key"
root@1.2.3.4:"/path/to/src/" "/path/to/backup/"
tests folder.composer install --dev phpunit/phpunit to install phpunitphpunit from inside the tests directory to execute testcase--coverage-text option to show code coverage report in terminalIf you like this script or have some features to add: contact me, fork this project, send pull requests, you know how it works., (*11)