Skip to content

Commit 127640e

Browse files
First 'rsync' sync draft
1 parent 7dd3a65 commit 127640e

File tree

2 files changed

+175
-22
lines changed

2 files changed

+175
-22
lines changed

src/Backup/Sync/Cli.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace phpbu\Backup\Sync;
3+
4+
use phpbu\Backup\Cli\Command;
5+
use phpbu\Backup\Cli\Exec;
6+
use phpbu\Backup\Target;
7+
8+
/**
9+
* Cli
10+
*
11+
* Baseclass for all cli based sync tools e.g. 'rsync'
12+
*
13+
* @package phpbu
14+
* @subpackage Backup
15+
* @author Sebastian Feldmann <sebastian@phpbu.de>
16+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
17+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
18+
* @link http://www.phpbu.de/
19+
* @since Class available since Release 1.1.0
20+
*/
21+
abstract class Cli
22+
{
23+
/**
24+
* Executes a cli command
25+
*
26+
* @return \phpbu\Cli\Result
27+
* @throws \phpbu\Backup\Sync\Exception
28+
*/
29+
protected function execute(Command $command)
30+
{
31+
$exec = new Exec();
32+
$exec->addCommand($cmd);
33+
34+
$res = $exec->execute();
35+
if ($res->getCode()) {
36+
throw new Exception('sync failed: ' . PHP_EOL . $res->getOutput());
37+
}
38+
}
39+
40+
/**
41+
* Replaces %TARGET_DIR% and %TARGET_FILE%
42+
*
43+
* @param string $args
44+
* @param Target $target
45+
* @return string
46+
*/
47+
protected function replaceTargetPlaceholder($string, Target $target)
48+
{
49+
$targetFile = $target->getFilenameCompressed();
50+
$targetDir = dirname($targetFile);
51+
$search = array('%TARGET_DIR%', '%TARGET_FILE%');
52+
$replace = array($targetDir, $targetFile);
53+
return str_replace($search, $replace, $string);
54+
}
55+
}

src/Backup/Sync/Rsync.php

Lines changed: 120 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,104 @@
33

44
use phpbu\App\Result;
55
use phpbu\Backup\Cli\Cmd;
6+
use phpbu\Backup\Cli\Exec;
67
use phpbu\Backup\Sync;
78
use phpbu\Backup\Target;
9+
use phpbu\Util;
810

9-
class Rsync implements Sync
11+
/**
12+
* Rsync
13+
*
14+
* @package phpbu
15+
* @subpackage Backup
16+
* @author Sebastian Feldmann <sebastian@phpbu.de>
17+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
18+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
19+
* @link http://www.phpbu.de/
20+
* @since Class available since Release 1.1.0
21+
*/
22+
class Rsync extends Cli implements Sync
1023
{
1124
/**
12-
* Configuration
25+
* Raw args
26+
*
27+
* @var string
28+
*/
29+
protected $args;
30+
31+
/**
32+
* Remote username
33+
*
34+
* @var string
35+
*/
36+
protected $user;
37+
38+
/**
39+
* Target host
40+
*
41+
* @var string
42+
*/
43+
protected $host;
44+
45+
/**
46+
* Target path
47+
*
48+
* @var string
49+
*/
50+
protected $path;
51+
52+
/**
53+
* Files to ignore, extracted from config string seperated by ":"
1354
*
1455
* @var array
1556
*/
16-
protected $config;
57+
protected $excludes;
58+
59+
/**
60+
* Should only the created backup be synced or the complete directory
61+
*
62+
* @var boolean
63+
*/
64+
protected $isDirSync;
65+
66+
/**
67+
* Remove deleted files remotely as well
68+
*
69+
* @var boolean
70+
*/
71+
protected $delete;
1772

1873
/**
1974
* (non-PHPdoc)
2075
* @see \phpbu\Backup\Sync::setup()
2176
*/
2277
public function setup(array $config)
2378
{
24-
$this->config = $config;
79+
if (isset($config['args'])) {
80+
$this->args = $config['args'];
81+
} else {
82+
if (empty($config['path'])) {
83+
throw new Exception('option \'path\' is missing');
84+
}
85+
$this->path = $config['path'];
86+
87+
if (isset($config['user'])) {
88+
$this->user = $config['user'];
89+
}
90+
if (isset($config['hots'])) {
91+
$this->host = $config['host'];
92+
}
93+
94+
$this->excludes = isset($config['exclude'])
95+
? array_map('trim', explode(':', $config['exclude']))
96+
: array();
97+
$this->delete = isset($config['delete'])
98+
? Util\String::toBoolean($this->config['delete'], false)
99+
: false;
100+
$this->isDirSync = isset($config['dirsync'])
101+
? Util\String::toBoolean($this->config['dirsync'], false)
102+
: false;
103+
}
25104
}
26105

27106
/**
@@ -30,38 +109,57 @@ public function setup(array $config)
30109
*/
31110
public function sync(Target $target, Result $result)
32111
{
33-
throw new Exception('NotImplementedException');
34-
$rsync = new Cmd('rsync');
112+
$rsync = new Cmd(Util\Cli::detectCmdLocation('rsync'));
113+
$targetFile = $target->getFilenameCompressed();
114+
$targetDir = dirname($targetFile);
115+
// std err > dev null
116+
$rsync->silence();
35117

36-
if (isset($this->config['args'])) {
37-
$exec = 'rsync ' . $args;
118+
if ($this->args) {
119+
// pro mode define all arguments yourself
120+
// WARNING! no escaping is done by phpbu
121+
$result->debug('WARNING: phpbu uses your rsync args without escaping');
122+
$rsync->addOption($this->replaceTargetPlaceholder($this->args), $target);
38123
} else {
39124
// use archive mode, verbose and compress if not allready done
40125
$options = '-av' . $target->shouldBeCompressed() ? '' : 'z';
41126
$rsync->addOption($options);
42-
// sync folder
43-
// --delete
44127

45-
// add target as source
46-
$rsync->addOption($target->getFilenameCompressed());
128+
if (count($this->excludes)) {
129+
foreach ($this->excludes as $ex) {
130+
$rsync->addOption('--exclude', $ex);
131+
}
132+
}
47133

48-
$syncTarget = '';
134+
// source handling
135+
if ($this->isDirSync) {
136+
// sync the whole folder
137+
// delete remote files as well?
138+
if ($this->delete) {
139+
$rsync->addOption('--delete');
140+
}
141+
$rsync->addOption($targetDir);
142+
} else {
143+
// sync just the created backup
144+
$rsync->addOption($targetFile);
145+
}
49146

147+
// target handling
148+
$syncTarget = '';
50149
// remote user
51-
if (isset($this->config['user'])) {
52-
$syncTarget .= $this->config['user'] . '@';
150+
if (null !== $this->user) {
151+
$syncTarget .= $this->user . '@';
53152
}
54-
55153
// remote host
56-
if (isset($this->config['host'])) {
57-
$syncTarget .= $this->config['host'] . ':';
154+
if (null !== $this->host) {
155+
$syncTarget .= $this->host . ':';
58156
}
59-
60157
// remote path
61-
if (isset($this->config['path'])) {
62-
$syncTarget .= $this->config['path'];
63-
}
158+
$syncTarget .= $this->path;
159+
160+
$rsync->addOption($syncTarget);
64161

162+
$this->execute($rsync);
65163
}
66164
}
67165
}

0 commit comments

Comments
 (0)