Skip to content

Commit a51f455

Browse files
Merge pull request #169 from chrisrollins65
Added a dereference option to copy symbolic link target with tar
2 parents 632ed1a + 44e27f6 commit a51f455

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

doc/config/source/tar.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"compressProgram": "lbzip2",
88
"removeSourceDir": false,
99
"throttle": "5m",
10-
"pathToTar": "/path/to/custom/bin"
10+
"pathToTar": "/path/to/custom/bin",
11+
"dereference": false
1112
}
1213
}

doc/config/source/tar.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323

2424
<!-- define your custom tar executable location -->
2525
<option name="pathToTar" value="/path/to/custom/bin" />
26+
27+
<!-- optional, default false -->
28+
<option name="dereference" value="false" />
2629
</source>

src/Backup/Source/Tar.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ class Tar extends SimulatorExecutable implements Simulator
104104
*/
105105
private $pathToArchive;
106106

107+
/**
108+
* Instead of archiving symbolic links, archive the files they link to
109+
*
110+
* @var bool
111+
*/
112+
private $dereference;
113+
107114
/**
108115
* Setup.
109116
*
@@ -121,6 +128,7 @@ public function setup(array $conf = [])
121128
$this->forceLocal = Util\Str::toBoolean(Util\Arr::getValue($conf, 'forceLocal', ''), false);
122129
$this->ignoreFailedRead = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ignoreFailedRead', ''), false);
123130
$this->removeSourceDir = Util\Str::toBoolean(Util\Arr::getValue($conf, 'removeSourceDir', ''), false);
131+
$this->dereference = Util\Str::toBoolean(Util\Arr::getValue($conf, 'dereference', ''), false);
124132
}
125133

126134
/**
@@ -192,7 +200,8 @@ protected function createExecutable(Target $target) : Executable
192200
->ignoreFailedRead($this->ignoreFailedRead)
193201
->removeSourceDirectory($this->removeSourceDir)
194202
->throttle($this->throttle)
195-
->archiveTo($this->pathToArchive);
203+
->archiveTo($this->pathToArchive)
204+
->dereference($this->dereference);
196205
// add paths to exclude
197206
foreach ($this->excludes as $path) {
198207
$executable->addExclude($path);

src/Cli/Executable/Tar.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class Tar extends Abstraction implements Executable
9898
'xz' => 'J'
9999
];
100100

101+
/**
102+
* Instead of archiving symbolic links, archive the files they link to
103+
*
104+
* @var bool
105+
*/
106+
private $dereference = false;
107+
101108
/**
102109
* Constructor.
103110
*
@@ -241,6 +248,18 @@ public function removeSourceDirectory(bool $bool) : Tar
241248
return $this;
242249
}
243250

251+
/**
252+
* Instead of archiving symbolic links, archive the files they link
253+
*
254+
* @param bool $bool
255+
* @return \phpbu\App\Cli\Executable\Tar
256+
*/
257+
public function dereference(bool $bool) : Tar
258+
{
259+
$this->dereference = $bool;
260+
return $this;
261+
}
262+
244263
/**
245264
* Tar CommandLine generator.
246265
*
@@ -259,6 +278,7 @@ protected function createCommandLine() : CommandLine
259278
$this->setExcludeOptions($tar);
260279
$this->handleWarnings($tar);
261280

281+
$tar->addOptionIfNotEmpty('-h', $this->dereference, false);
262282
$tar->addOptionIfNotEmpty('--force-local', $this->local, false);
263283
$tar->addOptionIfNotEmpty('--use-compress-program', $this->compressProgram);
264284
$tar->addOption('-' . (empty($this->compressProgram) ? $this->compression : '') . $create);

tests/phpbu/Backup/Source/TarTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ public function testDefault()
5858
);
5959
}
6060

61+
/**
62+
* Tests Tar::getExecutable
63+
*/
64+
public function testDereferenceOption()
65+
{
66+
$target = $this->createTargetMock('/tmp/backup.tar');
67+
$target->method('shouldBeCompressed')->willReturn(false);
68+
$target->method('getPathname')->willReturn('/tmp/backup.tar');
69+
70+
$tar = new Tar();
71+
$tar->setup(['pathToTar' => PHPBU_TEST_BIN, 'path' => __DIR__, 'dereference' => 'true']);
72+
73+
$exec = $tar->getExecutable($target);
74+
75+
$this->assertEquals(
76+
PHPBU_TEST_BIN . '/tar -h -cf \'/tmp/backup.tar\' -C \''
77+
. dirname(__DIR__) . '\' \''
78+
. basename(__DIR__) . '\'',
79+
$exec->getCommand()
80+
);
81+
}
82+
6183
/**
6284
* Tests Tar::getExecutable
6385
*/

tests/phpbu/Cli/Executable/TarTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ public function testDefault()
3232
);
3333
}
3434

35+
/**
36+
* Tests Tar::getCommandLine
37+
*/
38+
public function testDereference()
39+
{
40+
$dir = sys_get_temp_dir();
41+
$tarC = dirname($dir);
42+
$tarD = basename($dir);
43+
$tar = new Tar(PHPBU_TEST_BIN);
44+
$tar->archiveDirectory($dir)->archiveTo('/tmp/foo.tar')->dereference(true);
45+
46+
$this->assertEquals([0], $tar->getAcceptableExitCodes());
47+
$this->assertEquals(
48+
PHPBU_TEST_BIN . '/tar -h -cf \'/tmp/foo.tar\' -C \'' . $tarC . '\' \'' . $tarD . '\'',
49+
$tar->getCommand()
50+
);
51+
}
52+
3553
/**
3654
* Tests Tar::getCommandLine
3755
*/

0 commit comments

Comments
 (0)