2013-11-17 21:58:21 +03:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
#use strict;
|
2013-11-23 07:24:37 +03:00
|
|
|
use DBI;
|
2013-11-24 03:16:09 +03:00
|
|
|
use IPC::System::Simple qw(capture);
|
2013-12-02 22:34:37 +03:00
|
|
|
use Config::IniFiles;
|
2013-11-17 21:58:21 +03:00
|
|
|
|
2013-11-24 03:16:09 +03:00
|
|
|
sub trim
|
|
|
|
{
|
|
|
|
local($strBuffer) = @_;
|
|
|
|
|
|
|
|
$strBuffer =~ s/^\s+|\s+$//g;
|
|
|
|
|
|
|
|
return $strBuffer;
|
|
|
|
}
|
2013-11-17 21:58:21 +03:00
|
|
|
|
|
|
|
sub execute
|
|
|
|
{
|
|
|
|
local($strCommand) = @_;
|
|
|
|
my $strOutput;
|
|
|
|
|
2013-11-24 03:16:09 +03:00
|
|
|
print("$strCommand");
|
|
|
|
$strOutput = trim(capture($strCommand));
|
|
|
|
|
|
|
|
if ($strOutput eq "")
|
|
|
|
{
|
|
|
|
print(" ... complete\n\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print(" ... complete\n$strOutput\n\n");
|
|
|
|
}
|
2013-11-17 21:58:21 +03:00
|
|
|
|
2013-11-23 07:24:37 +03:00
|
|
|
return $strOutput;
|
2013-11-17 21:58:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sub pg_create
|
|
|
|
{
|
2013-11-24 03:16:09 +03:00
|
|
|
local($strPgBinPath, $strTestPath, $strTestDir, $strArchiveDir) = @_;
|
2013-11-17 21:58:21 +03:00
|
|
|
|
2013-11-24 03:16:09 +03:00
|
|
|
execute("mkdir $strTestPath");
|
|
|
|
execute($strPgBinPath . "/initdb -D $strTestPath/$strTestDir -A trust -k");
|
|
|
|
execute("mkdir $strTestPath/$strArchiveDir");
|
2013-11-17 21:58:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sub pg_start
|
|
|
|
{
|
2013-11-24 03:16:09 +03:00
|
|
|
local($strPgBinPath, $strDbPath, $strPort, $strAchiveCommand) = @_;
|
|
|
|
my $strCommand = "$strPgBinPath/pg_ctl start -o \"-c port=$strPort -c wal_level=archive -c archive_mode=on -c archive_command=\'$strAchiveCommand\'\" -D $strDbPath -l $strDbPath/postgresql.log -w -s";
|
2013-11-17 21:58:21 +03:00
|
|
|
|
|
|
|
execute($strCommand);
|
|
|
|
}
|
|
|
|
|
2013-11-23 07:24:37 +03:00
|
|
|
sub pg_password_set
|
|
|
|
{
|
2013-11-24 03:16:09 +03:00
|
|
|
local($strPgBinPath, $strPath, $strUser) = @_;
|
|
|
|
my $strCommand = "$strPgBinPath/psql --port=6000 -c \"alter user $strUser with password 'password'\" postgres";
|
2013-11-23 07:24:37 +03:00
|
|
|
|
|
|
|
execute($strCommand);
|
|
|
|
}
|
|
|
|
|
2013-11-17 21:58:21 +03:00
|
|
|
sub pg_stop
|
|
|
|
{
|
2013-11-24 03:16:09 +03:00
|
|
|
local($strPgBinPath, $strPath) = @_;
|
|
|
|
my $strCommand = "$strPgBinPath/pg_ctl stop -D $strPath -w -s";
|
2013-11-17 21:58:21 +03:00
|
|
|
|
|
|
|
execute($strCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pg_drop
|
|
|
|
{
|
2013-11-24 03:16:09 +03:00
|
|
|
local($strTestPath) = @_;
|
|
|
|
my $strCommand = "rm -rf $strTestPath";
|
2013-11-17 21:58:21 +03:00
|
|
|
|
|
|
|
execute($strCommand);
|
|
|
|
}
|
|
|
|
|
2013-11-23 07:24:37 +03:00
|
|
|
sub pg_execute
|
|
|
|
{
|
|
|
|
local($dbh, $strSql) = @_;
|
|
|
|
|
2013-11-24 04:05:04 +03:00
|
|
|
print($strSql);
|
2013-11-23 07:24:37 +03:00
|
|
|
$sth = $dbh->prepare($strSql);
|
|
|
|
$sth->execute();
|
2013-11-24 04:05:04 +03:00
|
|
|
$sth->finish();
|
|
|
|
|
|
|
|
print(" ... complete\n\n");
|
2013-11-23 07:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
my $strUser = execute('whoami');
|
2013-11-24 03:16:09 +03:00
|
|
|
|
|
|
|
my $strTestPath = "/Users/dsteele/test";
|
|
|
|
my $strDbDir = "db";
|
2013-11-23 07:24:37 +03:00
|
|
|
my $strArchiveDir = "archive";
|
|
|
|
|
2013-11-24 03:16:09 +03:00
|
|
|
my $strPgBinPath = "/Library/PostgreSQL/9.3/bin";
|
2013-11-24 04:05:04 +03:00
|
|
|
my $strPort = "6000";
|
2013-11-24 03:16:09 +03:00
|
|
|
|
|
|
|
my $strBackRestBinPath = "/Users/dsteele/pg_backrest";
|
|
|
|
my $strArchiveCommand = "$strBackRestBinPath/pg_backrest.pl archive-local %p $strTestPath/$strArchiveDir/%f";
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Stop the current test cluster if it is running and create a new one
|
|
|
|
################################################################################
|
|
|
|
eval {pg_stop($strPgBinPath, "$strTestPath/$strDbDir")};
|
|
|
|
|
|
|
|
if ($@)
|
|
|
|
{
|
|
|
|
print(" ... unable to stop pg server (ignoring): " . trim($@) . "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_drop($strTestPath);
|
|
|
|
pg_create($strPgBinPath, $strTestPath, $strDbDir, $strArchiveDir);
|
2013-11-24 04:05:04 +03:00
|
|
|
pg_start($strPgBinPath, "$strTestPath/$strDbDir", $strPort, $strArchiveCommand);
|
2013-11-24 03:16:09 +03:00
|
|
|
pg_password_set($strPgBinPath, "$strTestPath/$strDbDir", $strUser);
|
2013-11-23 07:24:37 +03:00
|
|
|
|
2013-11-24 03:16:09 +03:00
|
|
|
################################################################################
|
2013-11-24 04:05:04 +03:00
|
|
|
# Connect and start tests
|
2013-11-24 03:16:09 +03:00
|
|
|
################################################################################
|
2013-11-24 04:05:04 +03:00
|
|
|
$dbh = DBI->connect("dbi:Pg:dbname=postgres;port=6000;host=127.0.0.1", $strUser,
|
|
|
|
'password', {AutoCommit => 1});
|
|
|
|
|
2013-11-23 07:24:37 +03:00
|
|
|
pg_execute($dbh, "create table test (id int)");
|
|
|
|
|
|
|
|
pg_execute($dbh, "insert into test values (1)");
|
|
|
|
pg_execute($dbh, "select pg_switch_xlog()");
|
|
|
|
|
|
|
|
pg_execute($dbh, "insert into test values (2)");
|
|
|
|
pg_execute($dbh, "select pg_switch_xlog()");
|
|
|
|
|
|
|
|
pg_execute($dbh, "insert into test values (3)");
|
|
|
|
pg_execute($dbh, "select pg_switch_xlog()");
|
2013-11-23 01:29:01 +03:00
|
|
|
|
2013-11-24 04:05:04 +03:00
|
|
|
$dbh->disconnect();
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Stop the server
|
|
|
|
################################################################################
|
|
|
|
pg_stop($strPgBinPath, "$strTestPath/$strDbDir");
|