jenwachter / php-rsync
2.0.0
2024-02-21 19:37 UTC
Requires
- php: >=8.1
- psr/log: ^1|^2|^3
Requires (Dev)
- phpunit/phpunit: ^10.2
README
这是一个简单的PHP rsync库,能够执行本地、远程和Akamai NetStorage同步。请注意,这个库并不包含所有使用rsync命令可以执行的操作。
需求
- PHP >= 8.0
安装
要安装此库,您需要在项目中使用Composer。
composer require jenwachter/php-rsync
使用方法
创建连接
要开始使用此库,您必须首先创建一个连接。然后将此连接传递给Rsync
类。
$connection = new PhpRsync\Connection($type); $rsync = new Rsync($connection);
有三种类型的连接
本地
当源和目标在同一台机器上时使用此类型的连接。例如:在本地机器上从一个目录传输文件到另一个目录。
$local = new PhpRsync\Connection( 'local', '/path/to/destination/root/dir' ); $rsync = new Rsync($local);
远程
当源和目标在不同的机器上时使用此类型的连接。例如:从本地机器传输文件到远程机器。
$remote = new PhpRsync\Connection( 'remote', '/path/to/destination/root/dir', 'remote.host.com', 'username', ['ssh_key' => '/path/to/ssh/key'] ); $rsync = new Rsync($remote);
或者,您也可以通过传递['password' => 'your_password']
作为第五个参数来使用密码进行身份验证。
Akamai
当目标为Akamai NetStorage时使用此类型的连接。有关如何连接到NetStorage的详细信息,请参阅NetStorage Rsync文档
$akamai = new PhpRsync\Connection( 'akamai', '/path/to/destination/root/dir', 'cpcode.rsync.upload.akamai.com', 'username', ['ssh_key' => '/path/to/ssh/key'] ); $rsync = new Rsync($akamai);
执行rsync传输
$options = []; $rsync->run( '/path/to/source/dir', 'relative/path/to/destination/dir', $options );
可用选项
- archive: 布尔值。如果为true,则添加
--archive
标志。默认:true - compress: 布尔值。如果为true,则添加
--compress
标志。默认:true - cwd: 字符串。在运行
rsync
命令之前更改当前工作目录。默认:null - delete: 布尔值。如果为true,则添加
--delete
标志。默认:false - dryrun: 布尔值。如果为true,则添加
--dry-run
和--verbose
标志。默认:false - exclude: 数组|字符串。如果传递字符串,则添加
--exclude="<string>"
标志。如果传递字符串数组,则添加多个--exclude="<string>"
标志。默认:null - include: 数组|字符串。如果传递字符串,则添加
--include="<string>"
标志。如果传递字符串数组,则添加多个--include="<string>"
标志。默认:null - relative: 布尔值。如果为true,则添加
--relative
标志。默认:false