1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2026-06-20 01:17:49 +02:00

Working on backup.

This commit is contained in:
David Steele
2013-12-06 20:24:14 -05:00
parent 4d42d448e9
commit 02a9a693b8
3 changed files with 125 additions and 70 deletions
+3 -3
View File
@@ -4,10 +4,10 @@ compress=gzip --stdout %file%
#checksum=sha1sum %file% | awk '{print \$1}' # Ubuntu Linux
checksum=shasum %file% | awk '{print $1}'
copy=cp %source% %destination%
manifest=/opt/local/bin/gfind %path% -printf '%A@\t%i\t%y\t%#m\t%u\t%g\t%s\t%P\t%l\n'
manifest=/opt/local/bin/gfind %path% -printf '%T@\t%i\t%y\t%m\t%u\t%g\t%s\t%P\t%l\n'
[common]
base_path=/Users/dsteele/test/backup
backup_path=/Users/dsteele/test/backup
[cluster:db]
pgdata=/Users/dsteele/test/db
pgdata=/Users/dsteele/test/db/common
+105 -60
View File
@@ -56,6 +56,88 @@ sub file_hash_get
return($strHash);
}
####################################################################################################################################
# BACKUP - Backup the base directory or a tablespace
####################################################################################################################################
sub backup
{
my $strCommandManifest = shift;
my $strBackupPath = shift;
my $strPath = shift;
my %oBackupConfig = shift;
my $strLevel = shift;
my $strCommand = $strCommandManifest;
$strCommand =~ s/\%path\%/$strPath/g;
my $strManifest = execute($strCommand);
my @stryFile = split("\n", $strManifest);
# Flag to only allow the root directory once
my $bRootDir = 0;
for (my $iFileIdx = 0; $iFileIdx < scalar @stryFile; $iFileIdx++)
{
my @stryField = split("\t", $stryFile[$iFileIdx]);
my $dfModifyTime = $stryField[0];
my $lInode = $stryField[1];
my $cType = $stryField[2];
my $strPermission = $stryField[3];
my $strUser = $stryField[4];
my $strGroup = $stryField[5];
my $strSize = $stryField[6];
my $strName = $stryField[7];
my $strLinkDestination = $stryField[8];
if (!defined($strName))
{
if ($bRootDir)
{
die "Root dir appeared twice - check manifest command"
}
$bRootDir = 1;
$strName = ".";
}
# Don't process anything in pg_xlog
if (index($strName, 'pg_xlog/') != 0)
{
# Process directories
if ($cType eq "d")
{
print "$strPath dir: $strName\n"
}
# Process symbolic links (hard links not supported)
elsif ($cType eq "l")
{
print "$strPath link: $strName -> $strLinkDestination\n";
if (index($strName, 'pg_tblspc/') == 0)
{
backup($strCommandManifest, $strBackupPath, $strLinkDestination, %oBackupConfig, $strName);
}
}
# Process files except those in pg_xlog (hard links not supported)
elsif ($cType eq "f")
{
#$oBackupConfig{"file"}{"$strName"} = $dfModifyTime;
print "$strPath file: $strName\n"
}
# Unrecognized type - fail
else
{
die "Unrecognized file type $cType for file $strName";
}
}
}
}
####################################################################################################################################
# START MAIN
####################################################################################################################################
@@ -140,11 +222,15 @@ if ($strOperation eq "archive-local")
####################################################################################################################################
# GET MORE CONFIG INFO
####################################################################################################################################
if (!defined($oConfig{common}{base_path}))
my $strBackupPath = $oConfig{common}{backup_path};
if (!defined($strBackupPath))
{
die 'undefined base path';
}
$strBackupPath .= "/backup.tmp";
####################################################################################################################################
# BACKUP
####################################################################################################################################
@@ -169,67 +255,26 @@ if ($strOperation eq "backup")
die 'undefined cluster pgdata';
}
my $strCommand = $oConfig{command}{manifest};
$strCommand =~ s/\%path\%/$oConfig{"cluster:$strCluster"}{pgdata}/g;
my $strManifest = execute($strCommand);
my @stryFile = split("\n", $strManifest);
# Flag to only allow the root directory once
my $bRootDir = 0;
for (my $iFileIdx = 0; $iFileIdx < scalar @stryFile; $iFileIdx++)
unless (-e $strBackupPath)
{
my @stryField = split("\t", $stryFile[$iFileIdx]);
my $dfTime = $stryField[0];
my $lInode = $stryField[1];
my $cType = $stryField[2];
my $strPermission = $stryField[3];
my $strUser = $stryField[4];
my $strGroup = $stryField[5];
my $strSize = $stryField[6];
my $strName = $stryField[7];
if (!defined($strName))
{
if ($bRootDir)
{
die "Root dir appeared twice - check manifest command"
}
$bRootDir = 1;
$strName = ".";
}
# Don't process anything in pg_xlog
if (index($strName, 'pg_xlog/') != 0)
{
# Process directories
if ($cType eq "d")
{
print "dir: $strName\n"
}
# Process symbolic links (hard links not supported)
elsif ($cType eq "l")
{
print "link: $strName\n"
}
# Process files except those in pg_xlog (hard links not supported)
elsif ($cType eq "f")
{
print "file: $strName\n"
}
# Unrecognized type - fail
else
{
die "Unrecognized file type $cType for file $strName";
}
}
print "Creating backup path $strBackupPath\n";
mkdir $strBackupPath or die "Unable to create backup path";
}
my %oBackupConfig;
if (-e "$strBackupPath/backup.conf")
{
tie %oBackupConfig, 'Config::IniFiles', (-file => "$strBackupPath/backup.conf") or die "Unable to open backup config";
}
else
{
tie %oBackupConfig, 'Config::IniFiles' or die 'Unable to create backup config';
}
backup($oConfig{command}{manifest}, $strBackupPath, $oConfig{"cluster:$strCluster"}{pgdata}, %oBackupConfig, "base");
tied(%oBackupConfig)->WriteConfig("$strBackupPath/backup.conf");
# print scalar @stryFile . "\n";
}
+17 -7
View File
@@ -36,11 +36,15 @@ sub execute
sub pg_create
{
local($strPgBinPath, $strTestPath, $strTestDir, $strArchiveDir) = @_;
local($strPgBinPath, $strTestPath, $strTestDir, $strArchiveDir, $strBackupDir) = @_;
execute("mkdir $strTestPath");
execute($strPgBinPath . "/initdb -D $strTestPath/$strTestDir -A trust -k");
execute("mkdir $strTestPath/$strTestDir");
execute("mkdir $strTestPath/$strTestDir/ts1");
execute("mkdir $strTestPath/$strTestDir/ts2");
execute($strPgBinPath . "/initdb -D $strTestPath/$strTestDir/common -A trust -k");
execute("mkdir $strTestPath/$strArchiveDir");
execute("mkdir $strTestPath/$strBackupDir");
}
sub pg_start
@@ -81,7 +85,7 @@ sub pg_execute
print($strSql);
$sth = $dbh->prepare($strSql);
$sth->execute();
$sth->execute() or die;
$sth->finish();
print(" ... complete\n\n");
@@ -92,6 +96,7 @@ my $strUser = execute('whoami');
my $strTestPath = "/Users/dsteele/test";
my $strDbDir = "db";
my $strArchiveDir = "archive";
my $strBackupDir = "backup";
my $strPgBinPath = "/Library/PostgreSQL/9.3/bin";
my $strPort = "6000";
@@ -110,9 +115,9 @@ if ($@)
}
pg_drop($strTestPath);
pg_create($strPgBinPath, $strTestPath, $strDbDir, $strArchiveDir);
pg_start($strPgBinPath, "$strTestPath/$strDbDir", $strPort, $strArchiveCommand);
pg_password_set($strPgBinPath, "$strTestPath/$strDbDir", $strUser);
pg_create($strPgBinPath, $strTestPath, $strDbDir, $strArchiveDir, $strBackupDir);
pg_start($strPgBinPath, "$strTestPath/$strDbDir/common", $strPort, $strArchiveCommand);
pg_password_set($strPgBinPath, "$strTestPath/$strDbDir/common", $strUser);
################################################################################
# Connect and start tests
@@ -120,7 +125,12 @@ pg_password_set($strPgBinPath, "$strTestPath/$strDbDir", $strUser);
$dbh = DBI->connect("dbi:Pg:dbname=postgres;port=6000;host=127.0.0.1", $strUser,
'password', {AutoCommit => 1});
pg_execute($dbh, "create tablespace ts1 location '$strTestPath/$strDbDir/ts1'");
pg_execute($dbh, "create tablespace ts2 location '$strTestPath/$strDbDir/ts2'");
pg_execute($dbh, "create table test (id int)");
pg_execute($dbh, "create table test_ts1 (id int) tablespace ts1");
pg_execute($dbh, "create table test_ts2 (id int) tablespace ts1");
pg_execute($dbh, "insert into test values (1)");
pg_execute($dbh, "select pg_switch_xlog()");
@@ -136,4 +146,4 @@ $dbh->disconnect();
################################################################################
# Stop the server
################################################################################
pg_stop($strPgBinPath, "$strTestPath/$strDbDir");
pg_stop($strPgBinPath, "$strTestPath/$strDbDir/common");