Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #100 from scasei/master
Browse files Browse the repository at this point in the history
Escape Paths Aruments on Commandline
  • Loading branch information
dizda committed Jan 25, 2016
2 parents 9b4cc2e + 887c1a5 commit 26c7e9c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Database/MySQL.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Dizda\CloudBackupBundle\Database;

use Symfony\Component\Process\ProcessUtils;

/**
* Class MySQL.
*
Expand Down Expand Up @@ -81,7 +83,8 @@ protected function getCommand()
$this->auth,
$this->database,
$this->ignoreTables,
$this->dataPath.$this->fileName);
ProcessUtils::escapeArgument($this->dataPath.$this->fileName)
);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Processor/TarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dizda\CloudBackupBundle\Processor;

use Symfony\Component\Process\ProcessUtils;

class TarProcessor extends BaseProcessor implements ProcessorInterface
{
/**
Expand All @@ -27,9 +29,9 @@ public function getCompressionCommand($archivePath, $basePath)

return sprintf('tar %s c -C %s . | gzip %s > %s',
implode(' ', $tarParams),
$basePath,
ProcessUtils::escapeArgument($basePath),
implode(' ', $zipParams),
$archivePath
ProcessUtils::escapeArgument($archivePath)
);
}

Expand Down
14 changes: 7 additions & 7 deletions Tests/Database/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function shouldDumpAllDatabases()
),
), '/var/backup/');

$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" --all-databases > /var/backup/mysql/all-databases.sql");
$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" --all-databases > '/var/backup/mysql/all-databases.sql'");
}

/**
Expand Down Expand Up @@ -55,8 +55,8 @@ public function shouldDumpSpecifiedDatabase()
),
), '/var/backup/');

$this->assertEquals($mysql1->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" dizbdd > /var/backup/mysql/dizbdd.sql");
$this->assertEquals($mysql2->getCommand(), "mysqldump --host=\"somehost\" --port=\"2222\" --user=\"mysql\" --password=\"somepwd\" somebdd > /var/backup/mysql/somebdd.sql");
$this->assertEquals($mysql1->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" dizbdd > '/var/backup/mysql/dizbdd.sql'");
$this->assertEquals($mysql2->getCommand(), "mysqldump --host=\"somehost\" --port=\"2222\" --user=\"mysql\" --password=\"somepwd\" somebdd > '/var/backup/mysql/somebdd.sql'");

// dump specified database with no auth
$mysql = new MySQLDummy(array(
Expand All @@ -70,7 +70,7 @@ public function shouldDumpSpecifiedDatabase()
),
), '/var/backup/');

$this->assertEquals($mysql->getCommand(), 'mysqldump somebdd > /var/backup/mysql/somebdd.sql');
$this->assertEquals($mysql->getCommand(), 'mysqldump somebdd > \'/var/backup/mysql/somebdd.sql\'');
}

/**
Expand All @@ -90,7 +90,7 @@ public function shouldDumpAllDatabasesWithNoAuth()
),
), '/var/backup/');

$this->assertEquals($mysql->getCommand(), 'mysqldump --all-databases > /var/backup/mysql/all-databases.sql');
$this->assertEquals($mysql->getCommand(), 'mysqldump --all-databases > \'/var/backup/mysql/all-databases.sql\'');
}

/**
Expand All @@ -110,7 +110,7 @@ public function shouldIgnoreSpecifiedTablesForSpecifiedDatabase()
),
), '/var/backup/');

$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" dizbdd --ignore-table=dizbdd.table1 --ignore-table=dizbdd.table2 > /var/backup/mysql/dizbdd.sql");
$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" dizbdd --ignore-table=dizbdd.table1 --ignore-table=dizbdd.table2 > '/var/backup/mysql/dizbdd.sql'");
}

/**
Expand All @@ -130,7 +130,7 @@ public function shouldIgnoreSpecifiedTablesForAllDatabase()
),
), '/var/backup/');

$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" --all-databases --ignore-table=db1.table1 --ignore-table=db2.table2 > /var/backup/mysql/all-databases.sql");
$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" --all-databases --ignore-table=db1.table1 --ignore-table=db2.table2 > '/var/backup/mysql/all-databases.sql'");
}

/**
Expand Down
9 changes: 5 additions & 4 deletions Tests/Processor/TarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dizda\CloudBackupBundle\Tests\Processor;

use Dizda\CloudBackupBundle\Processor\TarProcessor;
use Symfony\Component\Process\ProcessUtils;

/**
* Class TarTest.
Expand All @@ -22,28 +23,28 @@ public function testGetCompressionCommand()
$processor = new TarProcessor(array());
$this->assertEquals(
$processor->getCompressionCommand($archivePath, $outputPath),
"tar c -C $outputPath . | gzip > $archivePath"
"tar c -C ". ProcessUtils::escapeArgument($outputPath)." . | gzip > ". ProcessUtils::escapeArgument($archivePath)
);

// compress with password - password not used in tar processor
$processor = new TarProcessor(array('password' => 'qwerty'));
$this->assertEquals(
$processor->getCompressionCommand($archivePath, $outputPath),
"tar c -C $outputPath . | gzip > $archivePath"
"tar c -C ". ProcessUtils::escapeArgument($outputPath)." . | gzip > ". ProcessUtils::escapeArgument($archivePath)
);

// compress with compression rate = 0
$processor = new TarProcessor(array('compression_ratio' => 0));
$this->assertEquals(
$processor->getCompressionCommand($archivePath, $outputPath),
"tar c -C $outputPath . | gzip -0 > $archivePath"
"tar c -C ". ProcessUtils::escapeArgument($outputPath)." . | gzip -0 > ". ProcessUtils::escapeArgument($archivePath)
);

// compress with compression rate = 9
$processor = new TarProcessor(array('compression_ratio' => 9));
$this->assertEquals(
$processor->getCompressionCommand($archivePath, $outputPath),
"tar c -C $outputPath . | gzip -9 > $archivePath"
"tar c -C ". ProcessUtils::escapeArgument($outputPath)." . | gzip -9 > ". ProcessUtils::escapeArgument($archivePath)
);
}
}

0 comments on commit 26c7e9c

Please sign in to comment.