Skip to content

Commit 28a1ba7

Browse files
Sync with 'rsync' is now working
1 parent d85e70f commit 28a1ba7

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/Backup/Cli/Cmd.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class Cmd
3535
*/
3636
private $options = array();
3737

38+
/**
39+
* Command arguments
40+
*
41+
* @var array<string>
42+
*/
43+
private $args = array();
44+
3845
/**
3946
* Constructor
4047
*
@@ -84,12 +91,33 @@ public function addOption($option, $argument = null, $glue = '=')
8491
$this->options[] = $option . (null !== $argument ? $glue . $escapedArgument : '');
8592
}
8693

94+
/**
95+
* Add argument to list
96+
*
97+
* @param miyed<string|array> $argument
98+
* @param string $glue
99+
*/
100+
public function addArgument($argument)
101+
{
102+
if (is_array($argument)) {
103+
$argument = array_map('escapeshellarg', $argument);
104+
$glue = ' ';
105+
$escapedArgument = implode(' ', $argument);
106+
} else {
107+
$escapedArgument = escapeshellarg($argument);
108+
}
109+
$this->args[] = $escapedArgument;
110+
}
111+
87112
/**
88113
*
89114
* @return string
90115
*/
91116
public function __toString()
92117
{
93-
return $this->name . ' ' . implode(' ', $this->options) . ( $this->isSilent ? ' 2> /dev/null' : '');
118+
return $this->name
119+
. ( count($this->options) ? ' ' . implode(' ', $this->options) : '' )
120+
. ( count($this->args) ? ' ' . implode(' ', $this->args) : '' )
121+
. ( $this->isSilent ? ' 2> /dev/null' : '' );
94122
}
95123
}

src/Backup/Sync/Cli.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace phpbu\Backup\Sync;
33

4-
use phpbu\Backup\Cli\Command;
4+
use phpbu\Backup\Cli\Cmd;
55
use phpbu\Backup\Cli\Exec;
66
use phpbu\Backup\Target;
77

@@ -23,17 +23,18 @@ abstract class Cli
2323
/**
2424
* Executes a cli command
2525
*
26-
* @return \phpbu\Cli\Result
26+
* @param \phpbu\Backup\Cli\Cmd
2727
* @throws \phpbu\Backup\Sync\Exception
2828
*/
29-
protected function execute(Command $command)
29+
protected function execute(Cmd $command)
3030
{
3131
$exec = new Exec();
32-
$exec->addCommand($cmd);
32+
$exec->addCommand($command);
3333

34+
/* @var $res \phpbu\Backup\Cli\Result */
3435
$res = $exec->execute();
3536
if ($res->getCode()) {
36-
throw new Exception('sync failed: ' . PHP_EOL . $res->getOutput());
37+
throw new Exception('sync failed: ' . PHP_EOL . $res->getOutputAsString());
3738
}
3839
}
3940

@@ -46,7 +47,7 @@ protected function execute(Command $command)
4647
*/
4748
protected function replaceTargetPlaceholder($string, Target $target)
4849
{
49-
$targetFile = $target->getFilenameCompressed();
50+
$targetFile = $target->getPathnameCompressed();
5051
$targetDir = dirname($targetFile);
5152
$search = array('%TARGET_DIR%', '%TARGET_FILE%');
5253
$replace = array($targetDir, $targetFile);

src/Backup/Sync/Rsync.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public function setup(array $config)
9595
? array_map('trim', explode(':', $config['exclude']))
9696
: array();
9797
$this->delete = isset($config['delete'])
98-
? Util\String::toBoolean($this->config['delete'], false)
98+
? Util\String::toBoolean($config['delete'], false)
9999
: false;
100100
$this->isDirSync = isset($config['dirsync'])
101-
? Util\String::toBoolean($this->config['dirsync'], false)
101+
? Util\String::toBoolean($config['dirsync'], false)
102102
: false;
103103
}
104104
}
@@ -109,20 +109,22 @@ public function setup(array $config)
109109
*/
110110
public function sync(Target $target, Result $result)
111111
{
112-
$rsync = new Cmd(Util\Cli::detectCmdLocation('rsync'));
113-
$targetFile = $target->getFilenameCompressed();
114-
$targetDir = dirname($targetFile);
115-
// std err > dev null
116-
$rsync->silence();
112+
$rsync = new Cmd(Util\Cli::detectCmdLocation('rsync'));
117113

118114
if ($this->args) {
119115
// pro mode define all arguments yourself
120116
// WARNING! no escaping is done by phpbu
121117
$result->debug('WARNING: phpbu uses your rsync args without escaping');
122-
$rsync->addOption($this->replaceTargetPlaceholder($this->args), $target);
118+
$rsync->addOption($this->replaceTargetPlaceholder($this->args, $target));
123119
} else {
120+
// std err > dev null
121+
$rsync->silence();
122+
123+
$targetFile = $target->getPathnameCompressed();
124+
$targetDir = dirname($targetFile);
125+
124126
// use archive mode, verbose and compress if not allready done
125-
$options = '-av' . $target->shouldBeCompressed() ? '' : 'z';
127+
$options = '-av' . ( $target->shouldBeCompressed() ? '' : 'z' );
126128
$rsync->addOption($options);
127129

128130
if (count($this->excludes)) {
@@ -138,10 +140,10 @@ public function sync(Target $target, Result $result)
138140
if ($this->delete) {
139141
$rsync->addOption('--delete');
140142
}
141-
$rsync->addOption($targetDir);
143+
$rsync->addArgument($targetDir);
142144
} else {
143145
// sync just the created backup
144-
$rsync->addOption($targetFile);
146+
$rsync->addArgument($targetFile);
145147
}
146148

147149
// target handling
@@ -157,9 +159,11 @@ public function sync(Target $target, Result $result)
157159
// remote path
158160
$syncTarget .= $this->path;
159161

160-
$rsync->addOption($syncTarget);
161-
162-
$this->execute($rsync);
162+
$rsync->addArgument($syncTarget);
163163
}
164+
// add some debug output
165+
$result->debug((string) $rsync);
166+
167+
$this->execute($rsync);
164168
}
165169
}

0 commit comments

Comments
 (0)