2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP MODULE
|
|
|
|
####################################################################################################################################
|
2014-06-08 00:29:11 +03:00
|
|
|
package BackRest::Backup;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
use threads;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Carp;
|
2014-07-28 01:13:23 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
use File::Basename;
|
|
|
|
use File::Path qw(remove_tree);
|
|
|
|
use Scalar::Util qw(looks_like_number);
|
|
|
|
use Thread::Queue;
|
|
|
|
|
|
|
|
use lib dirname($0);
|
2014-06-22 17:30:17 +03:00
|
|
|
use BackRest::Utility;
|
2015-01-27 18:44:23 +02:00
|
|
|
use BackRest::Exception;
|
2014-12-16 19:41:54 +02:00
|
|
|
use BackRest::Config;
|
2015-01-07 17:59:43 +02:00
|
|
|
use BackRest::Manifest;
|
2014-06-22 17:30:17 +03:00
|
|
|
use BackRest::File;
|
|
|
|
use BackRest::Db;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
|
2015-01-31 16:05:05 +02:00
|
|
|
our @EXPORT = qw(backup_init backup_cleanup backup_thread_kill archive_push archive_xfer archive_get archive_compress
|
2014-12-18 18:14:30 +02:00
|
|
|
backup backup_expire archive_list_get);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
my $oDb;
|
|
|
|
my $oFile;
|
2014-10-14 15:27:50 +03:00
|
|
|
my $strType; # Type of backup: full, differential (diff), incremental (incr)
|
2014-06-24 02:08:36 +03:00
|
|
|
my $bCompress;
|
2014-03-06 03:53:13 +03:00
|
|
|
my $bHardLink;
|
|
|
|
my $iThreadMax;
|
|
|
|
my $iThreadLocalMax;
|
|
|
|
my $iThreadThreshold = 10;
|
|
|
|
my $iSmallFileThreshold = 65536;
|
|
|
|
my $bArchiveRequired;
|
2014-09-20 00:51:51 +03:00
|
|
|
my $bNoStartStop;
|
|
|
|
my $bForce;
|
2014-03-06 03:53:13 +03:00
|
|
|
my $iThreadTimeout;
|
|
|
|
|
|
|
|
# Thread variables
|
|
|
|
my @oThread;
|
|
|
|
my @oThreadQueue;
|
|
|
|
my @oMasterQueue;
|
|
|
|
my %oFileCopyMap;
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_INIT
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_init
|
|
|
|
{
|
|
|
|
my $oDbParam = shift;
|
|
|
|
my $oFileParam = shift;
|
|
|
|
my $strTypeParam = shift;
|
2014-06-24 02:08:36 +03:00
|
|
|
my $bCompressParam = shift;
|
2014-03-06 03:53:13 +03:00
|
|
|
my $bHardLinkParam = shift;
|
|
|
|
my $iThreadMaxParam = shift;
|
|
|
|
my $bArchiveRequiredParam = shift;
|
|
|
|
my $iThreadTimeoutParam = shift;
|
2014-09-20 00:51:51 +03:00
|
|
|
my $bNoStartStopParam = shift;
|
|
|
|
my $bForceParam = shift;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
$oDb = $oDbParam;
|
|
|
|
$oFile = $oFileParam;
|
|
|
|
$strType = $strTypeParam;
|
2014-06-24 02:08:36 +03:00
|
|
|
$bCompress = $bCompressParam;
|
2014-03-06 03:53:13 +03:00
|
|
|
$bHardLink = $bHardLinkParam;
|
2015-01-27 18:44:23 +02:00
|
|
|
$bArchiveRequired = $bArchiveRequiredParam;
|
2014-03-06 03:53:13 +03:00
|
|
|
$iThreadMax = $iThreadMaxParam;
|
|
|
|
$iThreadTimeout = $iThreadTimeoutParam;
|
2014-09-20 00:51:51 +03:00
|
|
|
$bNoStartStop = $bNoStartStopParam;
|
|
|
|
$bForce = $bForceParam;
|
|
|
|
|
|
|
|
# If no-start-stop is specified then archive-required must be false
|
|
|
|
if ($bNoStartStop)
|
|
|
|
{
|
|
|
|
$bArchiveRequired = false;
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (!defined($iThreadMax))
|
|
|
|
{
|
|
|
|
$iThreadMax = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iThreadMax < 1 || $iThreadMax > 32)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'thread_max must be between 1 and 32');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 03:16:21 +02:00
|
|
|
####################################################################################################################################
|
2015-01-31 16:05:05 +02:00
|
|
|
# BACKUP_CLEANUP
|
2015-01-31 03:16:21 +02:00
|
|
|
####################################################################################################################################
|
2015-01-31 16:05:05 +02:00
|
|
|
sub backup_cleanup
|
2015-01-31 03:16:21 +02:00
|
|
|
{
|
|
|
|
undef($oFile);
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# THREAD_INIT
|
|
|
|
####################################################################################################################################
|
|
|
|
sub thread_init
|
|
|
|
{
|
|
|
|
my $iThreadRequestTotal = shift; # Number of threads that were requested
|
|
|
|
|
|
|
|
my $iThreadActualTotal; # Number of actual threads assigned
|
|
|
|
|
|
|
|
if (!defined($iThreadRequestTotal))
|
|
|
|
{
|
|
|
|
$iThreadActualTotal = $iThreadMax;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$iThreadActualTotal = $iThreadRequestTotal < $iThreadMax ? $iThreadRequestTotal : $iThreadMax;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($iThreadActualTotal < 1)
|
|
|
|
{
|
|
|
|
$iThreadActualTotal = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadActualTotal; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
$oThreadQueue[$iThreadIdx] = Thread::Queue->new();
|
|
|
|
$oMasterQueue[$iThreadIdx] = Thread::Queue->new();
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
return $iThreadActualTotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_THREAD_KILL
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_thread_kill
|
|
|
|
{
|
|
|
|
my $iTotal = 0;
|
|
|
|
|
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < scalar @oThread; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
if (defined($oThread[$iThreadIdx]))
|
|
|
|
{
|
|
|
|
if ($oThread[$iThreadIdx]->is_running())
|
|
|
|
{
|
|
|
|
$oThread[$iThreadIdx]->kill('KILL')->join();
|
|
|
|
}
|
|
|
|
elsif ($oThread[$iThreadIdx]->is_joinable())
|
|
|
|
{
|
|
|
|
$oThread[$iThreadIdx]->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
undef($oThread[$iThreadIdx]);
|
|
|
|
$iTotal++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return($iTotal);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_THREAD_COMPLETE
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_thread_complete
|
|
|
|
{
|
|
|
|
my $iTimeout = shift;
|
|
|
|
my $bConfessOnError = shift;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (!defined($bConfessOnError))
|
|
|
|
{
|
|
|
|
$bConfessOnError = true;
|
|
|
|
}
|
|
|
|
|
2014-06-28 18:47:21 +03:00
|
|
|
# if (!defined($iTimeout))
|
|
|
|
# {
|
|
|
|
# &log(WARN, "no thread timeout was set");
|
|
|
|
# }
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Wait for all threads to complete and handle errors
|
|
|
|
my $iThreadComplete = 0;
|
|
|
|
my $lTimeBegin = time();
|
|
|
|
|
|
|
|
# Rejoin the threads
|
|
|
|
while ($iThreadComplete < $iThreadLocalMax)
|
|
|
|
{
|
|
|
|
# If a timeout has been defined, make sure we have not been running longer than that
|
|
|
|
if (defined($iTimeout))
|
|
|
|
{
|
|
|
|
if (time() - $lTimeBegin >= $iTimeout)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "threads have been running more than ${iTimeout} seconds, exiting...");
|
|
|
|
|
|
|
|
#backup_thread_kill();
|
|
|
|
|
|
|
|
#confess &log(WARN, "all threads have exited, aborting...");
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadLocalMax; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
if (defined($oThread[$iThreadIdx]))
|
|
|
|
{
|
|
|
|
if (defined($oThread[$iThreadIdx]->error()))
|
|
|
|
{
|
|
|
|
backup_thread_kill();
|
|
|
|
|
|
|
|
if ($bConfessOnError)
|
|
|
|
{
|
2014-06-28 21:32:34 +03:00
|
|
|
confess &log(ERROR, 'error in thread ' . (${iThreadIdx} + 1) . ': check log for details');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($oThread[$iThreadIdx]->is_joinable())
|
|
|
|
{
|
|
|
|
&log(DEBUG, "thread ${iThreadIdx} exited");
|
|
|
|
$oThread[$iThreadIdx]->join();
|
|
|
|
&log(TRACE, "thread ${iThreadIdx} object undef");
|
|
|
|
undef($oThread[$iThreadIdx]);
|
|
|
|
$iThreadComplete++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 21:22:59 +02:00
|
|
|
|
|
|
|
# Sleep before trying again
|
2015-01-25 19:20:12 +02:00
|
|
|
hsleep(.1);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(DEBUG, 'all threads exited');
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-30 01:16:08 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# ARCHIVE_GET
|
|
|
|
####################################################################################################################################
|
|
|
|
sub archive_get
|
|
|
|
{
|
2014-09-30 01:51:02 +03:00
|
|
|
my $strDbClusterPath = shift;
|
2014-03-30 01:16:08 +03:00
|
|
|
my $strSourceArchive = shift;
|
|
|
|
my $strDestinationFile = shift;
|
|
|
|
|
2014-09-30 01:51:02 +03:00
|
|
|
# If the destination file path is not absolute then it is relative to the data path
|
|
|
|
if (index($strDestinationFile, '/',) != 0)
|
|
|
|
{
|
|
|
|
if (!defined($strDbClusterPath))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'database path must be set if relative xlog paths are used');
|
|
|
|
}
|
|
|
|
|
|
|
|
$strDestinationFile = "${strDbClusterPath}/${strDestinationFile}";
|
|
|
|
}
|
|
|
|
|
2014-04-13 16:51:51 +03:00
|
|
|
# Determine the path where the requested archive file is located
|
2014-03-30 01:16:08 +03:00
|
|
|
my $strArchivePath = dirname($oFile->path_get(PATH_BACKUP_ARCHIVE, $strSourceArchive));
|
|
|
|
|
2014-04-13 16:51:51 +03:00
|
|
|
# Get the name of the requested archive file (may have hash info and compression extension)
|
2014-06-22 21:51:28 +03:00
|
|
|
my @stryArchiveFile = $oFile->list(PATH_BACKUP_ABSOLUTE, $strArchivePath,
|
2014-10-02 17:55:38 +03:00
|
|
|
"^${strSourceArchive}(-[0-f]+){0,1}(\\.$oFile->{strCompressExtension}){0,1}\$", undef, true);
|
2014-03-30 01:16:08 +03:00
|
|
|
|
2014-04-13 16:51:51 +03:00
|
|
|
# If there is more than one matching archive file then there is a serious issue - likely a bug in the archiver
|
|
|
|
if (scalar @stryArchiveFile > 1)
|
2014-03-30 01:16:08 +03:00
|
|
|
{
|
2014-06-04 18:58:30 +03:00
|
|
|
confess &log(ASSERT, (scalar @stryArchiveFile) . " archive files found for ${strSourceArchive}.");
|
2014-04-13 16:51:51 +03:00
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-04-13 16:51:51 +03:00
|
|
|
# If there are no matching archive files then there are two possibilities:
|
|
|
|
# 1) The end of the archive stream has been reached, this is normal and a 1 will be returned
|
|
|
|
# 2) There is a hole in the archive stream so return a hard error (!!! However if turns out that due to race conditions this
|
|
|
|
# is harder than it looks. Postponed and added to the backlog. For now treated as case #1.)
|
|
|
|
elsif (scalar @stryArchiveFile == 0)
|
|
|
|
{
|
2014-06-04 18:58:30 +03:00
|
|
|
&log(INFO, "${strSourceArchive} was not found in the archive repository");
|
2014-04-13 16:51:51 +03:00
|
|
|
|
|
|
|
return 1;
|
2014-03-30 01:16:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&log(DEBUG, "archive_get: cp ${stryArchiveFile[0]} ${strDestinationFile}");
|
|
|
|
|
2014-07-17 02:15:56 +03:00
|
|
|
# Determine if the source file is already compressed
|
|
|
|
my $bSourceCompressed = $stryArchiveFile[0] =~ "^.*\.$oFile->{strCompressExtension}\$" ? true : false;
|
|
|
|
|
2014-04-13 16:51:51 +03:00
|
|
|
# Copy the archive file to the requested location
|
2014-07-17 02:15:56 +03:00
|
|
|
$oFile->copy(PATH_BACKUP_ARCHIVE, $stryArchiveFile[0], # Source file
|
|
|
|
PATH_DB_ABSOLUTE, $strDestinationFile, # Destination file
|
|
|
|
$bSourceCompressed, # Source compression based on detection
|
|
|
|
false); # Destination is not compressed
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-04-13 16:51:51 +03:00
|
|
|
return 0;
|
2014-03-30 01:16:08 +03:00
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# ARCHIVE_PUSH
|
|
|
|
####################################################################################################################################
|
|
|
|
sub archive_push
|
|
|
|
{
|
2014-06-24 01:54:00 +03:00
|
|
|
my $strDbClusterPath = shift;
|
2014-03-06 03:53:13 +03:00
|
|
|
my $strSourceFile = shift;
|
2014-06-24 01:54:00 +03:00
|
|
|
|
|
|
|
# If the source file path is not absolute then it is relative to the data path
|
|
|
|
if (index($strSourceFile, '/',) != 0)
|
|
|
|
{
|
|
|
|
if (!defined($strDbClusterPath))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'database path must be set if relative xlog paths are used');
|
2014-06-24 01:54:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$strSourceFile = "${strDbClusterPath}/${strSourceFile}";
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Get the destination file
|
|
|
|
my $strDestinationFile = basename($strSourceFile);
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Determine if this is an archive file (don't want to do compression or checksum on .backup files)
|
|
|
|
my $bArchiveFile = basename($strSourceFile) =~ /^[0-F]{24}$/ ? true : false;
|
|
|
|
|
|
|
|
# Append the checksum (if requested)
|
2015-03-01 03:31:56 +02:00
|
|
|
if ($bArchiveFile)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strDestinationFile .= '-' . $oFile->hash(PATH_DB_ABSOLUTE, $strSourceFile);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
# Append compression extension
|
|
|
|
if ($bArchiveFile && $bCompress)
|
|
|
|
{
|
|
|
|
$strDestinationFile .= ".$oFile->{strCompressExtension}";
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Copy the archive file
|
2014-06-24 01:54:00 +03:00
|
|
|
$oFile->copy(PATH_DB_ABSOLUTE, $strSourceFile, # Source file
|
|
|
|
PATH_BACKUP_ARCHIVE, $strDestinationFile, # Destination file
|
|
|
|
false, # Source is not compressed
|
2014-07-13 02:03:39 +03:00
|
|
|
$bArchiveFile && $bCompress, # Destination compress is configurable
|
2014-06-24 01:54:00 +03:00
|
|
|
undef, undef, undef, # Unused params
|
|
|
|
true); # Create path if it does not exist
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-07-17 02:15:56 +03:00
|
|
|
# ARCHIVE_XFER
|
2014-03-06 03:53:13 +03:00
|
|
|
####################################################################################################################################
|
2014-07-13 17:37:16 +03:00
|
|
|
sub archive_xfer
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $strArchivePath = shift;
|
|
|
|
my $strStopFile = shift;
|
|
|
|
my $strCommand = shift;
|
|
|
|
my $iArchiveMaxMB = shift;
|
|
|
|
|
|
|
|
# Load the archive manifest - all the files that need to be pushed
|
2014-07-13 02:03:39 +03:00
|
|
|
my %oManifestHash;
|
|
|
|
$oFile->manifest(PATH_DB_ABSOLUTE, $strArchivePath, \%oManifestHash);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Get all the files to be transferred and calculate the total size
|
|
|
|
my @stryFile;
|
|
|
|
my $lFileSize = 0;
|
|
|
|
my $lFileTotal = 0;
|
|
|
|
|
|
|
|
foreach my $strFile (sort(keys $oManifestHash{name}))
|
|
|
|
{
|
2015-01-31 20:48:09 +02:00
|
|
|
if ($strFile =~ /^[0-F]{16}\/[0-F]{24}.*/ || $strFile =~ /^[0-F]{8}\.history$/)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
push @stryFile, $strFile;
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
$lFileSize += $oManifestHash{name}{"${strFile}"}{size};
|
2014-03-06 03:53:13 +03:00
|
|
|
$lFileTotal++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($iArchiveMaxMB))
|
|
|
|
{
|
|
|
|
if ($iArchiveMaxMB < int($lFileSize / 1024 / 1024))
|
|
|
|
{
|
|
|
|
&log(ERROR, "local archive store has exceeded limit of ${iArchiveMaxMB}MB, archive logs will be discarded");
|
|
|
|
|
|
|
|
my $hStopFile;
|
2014-09-16 15:55:40 +03:00
|
|
|
open($hStopFile, '>', $strStopFile) or confess &log(ERROR, "unable to create stop file file ${strStopFile}");
|
2014-03-06 03:53:13 +03:00
|
|
|
close($hStopFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lFileTotal == 0)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(DEBUG, 'no archive logs to be copied to backup');
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
$0 = "${strCommand} archive-push-async " . substr($stryFile[0], 17, 24) . '-' . substr($stryFile[scalar @stryFile - 1], 17, 24);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Output files to be moved to backup
|
|
|
|
&log(INFO, "archive to be copied to backup total ${lFileTotal}, size " . file_size_format($lFileSize));
|
|
|
|
|
|
|
|
# # Init the thread variables
|
|
|
|
# $iThreadLocalMax = thread_init(int($lFileTotal / $iThreadThreshold) + 1);
|
|
|
|
# my $iThreadIdx = 0;
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# &log(DEBUG, "actual threads ${iThreadLocalMax}/${iThreadMax}");
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# # Distribute files among the threads
|
|
|
|
# foreach my $strFile (sort @stryFile)
|
|
|
|
# {
|
|
|
|
# $oThreadQueue[$iThreadIdx]->enqueue($strFile);
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# $iThreadIdx = ($iThreadIdx + 1 == $iThreadLocalMax) ? 0 : $iThreadIdx + 1;
|
|
|
|
# }
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# # End each thread queue and start the thread
|
|
|
|
# for ($iThreadIdx = 0; $iThreadIdx < $iThreadLocalMax; $iThreadIdx++)
|
|
|
|
# {
|
|
|
|
# $oThreadQueue[$iThreadIdx]->enqueue(undef);
|
|
|
|
# $oThread[$iThreadIdx] = threads->create(\&archive_pull_copy_thread, $iThreadIdx, $strArchivePath);
|
|
|
|
# }
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# backup_thread_complete($iThreadTimeout);
|
|
|
|
|
|
|
|
# Transfer each file
|
|
|
|
foreach my $strFile (sort @stryFile)
|
|
|
|
{
|
2014-07-13 02:03:39 +03:00
|
|
|
# Construct the archive filename to backup
|
|
|
|
my $strArchiveFile = "${strArchivePath}/${strFile}";
|
|
|
|
|
|
|
|
# Determine if the source file is already compressed
|
2014-07-13 17:37:16 +03:00
|
|
|
my $bSourceCompressed = $strArchiveFile =~ "^.*\.$oFile->{strCompressExtension}\$" ? true : false;
|
2014-07-13 02:03:39 +03:00
|
|
|
|
|
|
|
# Determine if this is an archive file (don't want to do compression or checksum on .backup files)
|
2014-07-13 17:37:16 +03:00
|
|
|
my $bArchiveFile = basename($strFile) =~
|
|
|
|
"^[0-F]{24}(-[0-f]+){0,1}(\\.$oFile->{strCompressExtension}){0,1}\$" ? true : false;
|
2014-07-13 02:03:39 +03:00
|
|
|
|
|
|
|
# Figure out whether the compression extension needs to be added or removed
|
|
|
|
my $bDestinationCompress = $bArchiveFile && $bCompress;
|
|
|
|
my $strDestinationFile = basename($strFile);
|
|
|
|
|
|
|
|
if (!$bSourceCompressed && $bDestinationCompress)
|
|
|
|
{
|
|
|
|
$strDestinationFile .= ".$oFile->{strCompressExtension}";
|
|
|
|
}
|
|
|
|
elsif ($bSourceCompressed && !$bDestinationCompress)
|
|
|
|
{
|
|
|
|
$strDestinationFile = substr($strDestinationFile, 0, length($strDestinationFile) - 3);
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-07-13 17:37:16 +03:00
|
|
|
&log(DEBUG, "backup archive file ${strFile}, archive ${bArchiveFile}, source_compressed = ${bSourceCompressed}, " .
|
|
|
|
"destination_compress ${bDestinationCompress}, default_compress = ${bCompress}");
|
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
# Copy the archive file
|
|
|
|
$oFile->copy(PATH_DB_ABSOLUTE, $strArchiveFile, # Source file
|
|
|
|
PATH_BACKUP_ARCHIVE, $strDestinationFile, # Destination file
|
|
|
|
$bSourceCompressed, # Source is not compressed
|
|
|
|
$bDestinationCompress, # Destination compress is configurable
|
|
|
|
undef, undef, undef, # Unused params
|
|
|
|
true); # Create path if it does not exist
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Remove the source archive file
|
|
|
|
unlink($strArchiveFile) or confess &log(ERROR, "unable to remove ${strArchiveFile}");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find the archive paths that need to be removed
|
|
|
|
my $strPathMax = substr((sort {$b cmp $a} @stryFile)[0], 0, 16);
|
|
|
|
|
|
|
|
&log(DEBUG, "local archive path max = ${strPathMax}");
|
|
|
|
|
2014-06-22 21:51:28 +03:00
|
|
|
foreach my $strPath ($oFile->list(PATH_DB_ABSOLUTE, $strArchivePath, "^[0-F]{16}\$"))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
if ($strPath lt $strPathMax)
|
|
|
|
{
|
|
|
|
&log(DEBUG, "removing local archive path ${strPath}");
|
2014-09-16 15:55:40 +03:00
|
|
|
rmdir($strArchivePath . '/' . $strPath) or &log(WARN, "unable to remove archive path ${strPath}, is it empty?");
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-04-01 17:49:37 +03:00
|
|
|
# If the dir is not empty check if the files are in the manifest
|
|
|
|
# If they are error - there has been some issue
|
|
|
|
# If not, they are new - continue processing without error - they'll be picked up on the next run
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return number of files indicating that processing should continue
|
|
|
|
return $lFileTotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
# sub archive_pull_copy_thread
|
|
|
|
# {
|
|
|
|
# my @args = @_;
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# my $iThreadIdx = $args[0];
|
|
|
|
# my $strArchivePath = $args[1];
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# my $oFileThread = $oFile->clone($iThreadIdx); # Thread local file object
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# # When a KILL signal is received, immediately abort
|
|
|
|
# $SIG{'KILL'} = sub {threads->exit();};
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# while (my $strFile = $oThreadQueue[$iThreadIdx]->dequeue())
|
|
|
|
# {
|
|
|
|
# &log(INFO, "thread ${iThreadIdx} backing up archive file ${strFile}");
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# my $strArchiveFile = "${strArchivePath}/${strFile}";
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# # Copy the file
|
|
|
|
# $oFileThread->file_copy(PATH_DB_ABSOLUTE, $strArchiveFile,
|
|
|
|
# PATH_BACKUP_ARCHIVE, basename($strFile),
|
|
|
|
# undef, undef,
|
|
|
|
# undef); # cannot set permissions remotely yet $oFile->{strDefaultFilePermission});
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# # Remove the source archive file
|
|
|
|
# unlink($strArchiveFile) or confess &log(ERROR, "unable to remove ${strArchiveFile}");
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
|
|
|
|
sub archive_compress
|
|
|
|
{
|
|
|
|
my $strArchivePath = shift;
|
|
|
|
my $strCommand = shift;
|
|
|
|
my $iFileCompressMax = shift;
|
|
|
|
|
|
|
|
# Load the archive manifest - all the files that need to be pushed
|
|
|
|
my %oManifestHash = $oFile->manifest_get(PATH_DB_ABSOLUTE, $strArchivePath);
|
|
|
|
|
|
|
|
# Get all the files to be compressed and calculate the total size
|
|
|
|
my @stryFile;
|
|
|
|
my $lFileSize = 0;
|
|
|
|
my $lFileTotal = 0;
|
|
|
|
|
|
|
|
foreach my $strFile (sort(keys $oManifestHash{name}))
|
|
|
|
{
|
|
|
|
if ($strFile =~ /^[0-F]{16}\/[0-F]{24}(\-[0-f]+){0,1}$/)
|
|
|
|
{
|
|
|
|
push @stryFile, $strFile;
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
$lFileSize += $oManifestHash{name}{"${strFile}"}{size};
|
2014-03-06 03:53:13 +03:00
|
|
|
$lFileTotal++;
|
|
|
|
|
|
|
|
if ($lFileTotal >= $iFileCompressMax)
|
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lFileTotal == 0)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(DEBUG, 'no archive logs to be compressed');
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
$0 = "${strCommand} archive-compress-async " . substr($stryFile[0], 17, 24) . '-' . substr($stryFile[scalar @stryFile - 1], 17, 24);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Output files to be compressed
|
|
|
|
&log(INFO, "archive to be compressed total ${lFileTotal}, size " . file_size_format($lFileSize));
|
|
|
|
|
|
|
|
# Init the thread variables
|
|
|
|
$iThreadLocalMax = thread_init(int($lFileTotal / $iThreadThreshold) + 1);
|
|
|
|
my $iThreadIdx = 0;
|
|
|
|
|
|
|
|
# Distribute files among the threads
|
|
|
|
foreach my $strFile (sort @stryFile)
|
|
|
|
{
|
|
|
|
$oThreadQueue[$iThreadIdx]->enqueue($strFile);
|
|
|
|
|
|
|
|
$iThreadIdx = ($iThreadIdx + 1 == $iThreadLocalMax) ? 0 : $iThreadIdx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# End each thread queue and start the thread
|
|
|
|
for ($iThreadIdx = 0; $iThreadIdx < $iThreadLocalMax; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
$oThreadQueue[$iThreadIdx]->enqueue(undef);
|
|
|
|
$oThread[$iThreadIdx] = threads->create(\&archive_pull_compress_thread, $iThreadIdx, $strArchivePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Complete the threads
|
|
|
|
backup_thread_complete($iThreadTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub archive_pull_compress_thread
|
|
|
|
{
|
|
|
|
my @args = @_;
|
|
|
|
|
|
|
|
my $iThreadIdx = $args[0];
|
|
|
|
my $strArchivePath = $args[1];
|
|
|
|
|
|
|
|
my $oFileThread = $oFile->clone($iThreadIdx); # Thread local file object
|
|
|
|
|
|
|
|
# When a KILL signal is received, immediately abort
|
|
|
|
$SIG{'KILL'} = sub {threads->exit();};
|
|
|
|
|
|
|
|
while (my $strFile = $oThreadQueue[$iThreadIdx]->dequeue())
|
|
|
|
{
|
|
|
|
&log(INFO, "thread ${iThreadIdx} compressing archive file ${strFile}");
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Compress the file
|
|
|
|
$oFileThread->file_compress(PATH_DB_ABSOLUTE, "${strArchivePath}/${strFile}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_REGEXP_GET - Generate a regexp depending on the backups that need to be found
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_regexp_get
|
|
|
|
{
|
|
|
|
my $bFull = shift;
|
|
|
|
my $bDifferential = shift;
|
|
|
|
my $bIncremental = shift;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (!$bFull && !$bDifferential && !$bIncremental)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'one parameter must be true');
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
my $strDateTimeRegExp = "[0-9]{8}\\-[0-9]{6}";
|
2014-09-16 15:55:40 +03:00
|
|
|
my $strRegExp = '^';
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($bFull || $bDifferential || $bIncremental)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strRegExp .= $strDateTimeRegExp . 'F';
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($bDifferential || $bIncremental)
|
|
|
|
{
|
|
|
|
if ($bFull)
|
|
|
|
{
|
|
|
|
$strRegExp .= "(\\_";
|
|
|
|
}
|
2014-09-14 23:30:53 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$strRegExp .= "\\_";
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
$strRegExp .= $strDateTimeRegExp;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($bDifferential && $bIncremental)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strRegExp .= '(D|I)';
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
elsif ($bDifferential)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strRegExp .= 'D';
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strRegExp .= 'I';
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($bFull)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strRegExp .= '){0,1}';
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
$strRegExp .= "\$";
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-09-14 23:30:53 +03:00
|
|
|
&log(DEBUG, "backup_regexp_get($bFull, $bDifferential, $bIncremental): $strRegExp");
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
return $strRegExp;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_TYPE_FIND - Find the last backup depending on the type
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_type_find
|
|
|
|
{
|
|
|
|
my $strType = shift;
|
|
|
|
my $strBackupClusterPath = shift;
|
2015-01-22 01:37:49 +02:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
my $strDirectory;
|
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
if ($strType eq BACKUP_TYPE_INCR)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strDirectory = ($oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(1, 1, 1), 'reverse'))[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
if (!defined($strDirectory) && $strType ne BACKUP_TYPE_FULL)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strDirectory = ($oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(1, 0, 0), 'reverse'))[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
return $strDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_FILE_NOT_IN_MANIFEST - Find all files in a backup path that are not in the supplied manifest
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_file_not_in_manifest
|
|
|
|
{
|
|
|
|
my $strPathType = shift;
|
2015-01-22 01:37:49 +02:00
|
|
|
my $oManifest = shift;
|
2015-01-23 00:43:19 +02:00
|
|
|
my $oAbortedManifest = shift;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-14 22:31:56 +03:00
|
|
|
my %oFileHash;
|
|
|
|
$oFile->manifest($strPathType, undef, \%oFileHash);
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
my @stryFile;
|
|
|
|
my $iFileTotal = 0;
|
|
|
|
|
|
|
|
foreach my $strName (sort(keys $oFileHash{name}))
|
|
|
|
{
|
|
|
|
# Ignore certain files that will never be in the manifest
|
2014-09-16 15:55:40 +03:00
|
|
|
if ($strName eq 'backup.manifest' ||
|
|
|
|
$strName eq '.')
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the base directory
|
2014-09-16 15:55:40 +03:00
|
|
|
my $strBasePath = (split('/', $strName))[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
if ($strBasePath eq $strName)
|
|
|
|
{
|
2014-10-19 23:30:16 +03:00
|
|
|
my $strSection = $strBasePath eq 'tablespace' ? 'backup:tablespace' : "${strBasePath}:path";
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-01-22 01:37:49 +02:00
|
|
|
if ($oManifest->test($strSection))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my $strPath = substr($strName, length($strBasePath) + 1);
|
|
|
|
|
|
|
|
# Create the section from the base path
|
|
|
|
my $strSection = $strBasePath;
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
if ($strSection eq 'tablespace')
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
my $strTablespace = (split('/', $strPath))[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
$strSection = $strSection . ':' . $strTablespace;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
if ($strTablespace eq $strPath)
|
|
|
|
{
|
2015-01-22 01:37:49 +02:00
|
|
|
if ($oManifest->test("${strSection}:path"))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$strPath = substr($strPath, length($strTablespace) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $cType = $oFileHash{name}{"${strName}"}{type};
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
if ($cType eq 'd')
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-01-22 01:37:49 +02:00
|
|
|
if ($oManifest->test("${strSection}:path", "${strPath}"))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-22 01:37:49 +02:00
|
|
|
if ($oManifest->test("${strSection}:file", "${strPath}"))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-01-22 01:37:49 +02:00
|
|
|
if ($oManifest->get("${strSection}:file", $strPath, MANIFEST_SUBKEY_SIZE) ==
|
2014-03-06 03:53:13 +03:00
|
|
|
$oFileHash{name}{"${strName}"}{size} &&
|
2015-01-22 01:37:49 +02:00
|
|
|
$oManifest->get("${strSection}:file", $strPath, MANIFEST_SUBKEY_MODIFICATION_TIME) ==
|
2014-03-06 03:53:13 +03:00
|
|
|
$oFileHash{name}{"${strName}"}{modification_time})
|
|
|
|
{
|
2015-01-23 00:43:19 +02:00
|
|
|
my $strChecksum = $oAbortedManifest->get("${strSection}:file", $strPath, MANIFEST_SUBKEY_CHECKSUM, false);
|
|
|
|
|
|
|
|
if (defined($strChecksum))
|
|
|
|
{
|
|
|
|
$oManifest->set("${strSection}:file", $strPath, MANIFEST_SUBKEY_CHECKSUM, $strChecksum);
|
|
|
|
}
|
|
|
|
|
2015-01-22 01:37:49 +02:00
|
|
|
$oManifest->set("${strSection}:file", $strPath, MANIFEST_SUBKEY_EXISTS, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stryFile[$iFileTotal] = $strName;
|
|
|
|
$iFileTotal++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return @stryFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_TMP_CLEAN
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# Cleans the temp directory from a previous failed backup so it can be reused
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_tmp_clean
|
|
|
|
{
|
2015-01-22 01:37:49 +02:00
|
|
|
my $oManifest = shift;
|
2015-01-23 00:43:19 +02:00
|
|
|
my $oAbortedManifest = shift;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(INFO, 'cleaning backup tmp path');
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Remove the pg_xlog directory since it contains nothing useful for the new backup
|
2014-09-16 15:55:40 +03:00
|
|
|
if (-e $oFile->path_get(PATH_BACKUP_TMP, 'base/pg_xlog'))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
remove_tree($oFile->path_get(PATH_BACKUP_TMP, 'base/pg_xlog')) or confess &log(ERROR, 'unable to delete tmp pg_xlog path');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Remove the pg_tblspc directory since it is trivial to rebuild, but hard to compare
|
2014-09-16 15:55:40 +03:00
|
|
|
if (-e $oFile->path_get(PATH_BACKUP_TMP, 'base/pg_tblspc'))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
remove_tree($oFile->path_get(PATH_BACKUP_TMP, 'base/pg_tblspc')) or confess &log(ERROR, 'unable to delete tmp pg_tblspc path');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get the list of files that should be deleted from temp
|
2015-01-23 00:43:19 +02:00
|
|
|
my @stryFile = backup_file_not_in_manifest(PATH_BACKUP_TMP, $oManifest, $oAbortedManifest);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
foreach my $strFile (sort {$b cmp $a} @stryFile)
|
|
|
|
{
|
|
|
|
my $strDelete = $oFile->path_get(PATH_BACKUP_TMP, $strFile);
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# If a path then delete it, all the files should have already been deleted since we are going in reverse order
|
|
|
|
if (-d $strDelete)
|
|
|
|
{
|
|
|
|
&log(DEBUG, "remove path ${strDelete}");
|
|
|
|
rmdir($strDelete) or confess &log(ERROR, "unable to delete path ${strDelete}, is it empty?");
|
|
|
|
}
|
|
|
|
# Else delete a file
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(DEBUG, "remove file ${strDelete}");
|
|
|
|
unlink($strDelete) or confess &log(ERROR, "unable to delete file ${strDelete}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_FILE - Performs the file level backup
|
|
|
|
#
|
|
|
|
# Uses the information in the manifest to determine which files need to be copied. Directories and tablespace links are only
|
|
|
|
# created when needed, except in the case of a full backup or if hardlinks are requested.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_file
|
|
|
|
{
|
2015-01-21 17:44:08 +02:00
|
|
|
my $strDbClusterPath = shift; # Database base data path
|
|
|
|
my $oBackupManifest = shift; # Manifest for the current backup
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Variables used for parallel copy
|
|
|
|
my $lTablespaceIdx = 0;
|
|
|
|
my $lFileTotal = 0;
|
|
|
|
my $lFileLargeSize = 0;
|
|
|
|
my $lFileLargeTotal = 0;
|
|
|
|
my $lFileSmallSize = 0;
|
|
|
|
my $lFileSmallTotal = 0;
|
|
|
|
|
|
|
|
# Decide if all the paths will be created in advance
|
2014-10-14 15:27:50 +03:00
|
|
|
my $bPathCreate = $bHardLink || $strType eq BACKUP_TYPE_FULL;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Iterate through the path sections of the manifest to backup
|
2015-01-21 17:44:08 +02:00
|
|
|
foreach my $strSectionPath ($oBackupManifest->keys())
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
# Skip non-path sections
|
2015-01-06 20:08:56 +02:00
|
|
|
if ($strSectionPath !~ /\:path$/ || $strSectionPath =~ /^backup\:path$/)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Determine the source and destination backup paths
|
|
|
|
my $strBackupSourcePath; # Absolute path to the database base directory or tablespace to backup
|
|
|
|
my $strBackupDestinationPath; # Relative path to the backup directory where the data will be stored
|
|
|
|
my $strSectionFile; # Manifest section that contains the file data
|
|
|
|
|
|
|
|
# Process the base database directory
|
|
|
|
if ($strSectionPath =~ /^base\:/)
|
|
|
|
{
|
|
|
|
$lTablespaceIdx++;
|
|
|
|
$strBackupSourcePath = $strDbClusterPath;
|
2014-09-16 15:55:40 +03:00
|
|
|
$strBackupDestinationPath = 'base';
|
|
|
|
$strSectionFile = 'base:file';
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Create the archive log directory
|
2014-09-16 15:55:40 +03:00
|
|
|
$oFile->path_create(PATH_BACKUP_TMP, 'base/pg_xlog');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
# Process each tablespace
|
|
|
|
elsif ($strSectionPath =~ /^tablespace\:/)
|
|
|
|
{
|
|
|
|
$lTablespaceIdx++;
|
2014-09-16 15:55:40 +03:00
|
|
|
my $strTablespaceName = (split(':', $strSectionPath))[1];
|
2015-01-21 17:44:08 +02:00
|
|
|
$strBackupSourcePath = $oBackupManifest->get(MANIFEST_SECTION_BACKUP_TABLESPACE, $strTablespaceName,
|
|
|
|
MANIFEST_SUBKEY_PATH);
|
2014-03-06 03:53:13 +03:00
|
|
|
$strBackupDestinationPath = "tablespace/${strTablespaceName}";
|
|
|
|
$strSectionFile = "tablespace:${strTablespaceName}:file";
|
|
|
|
|
|
|
|
# Create the tablespace directory and link
|
|
|
|
if ($bPathCreate)
|
|
|
|
{
|
|
|
|
$oFile->path_create(PATH_BACKUP_TMP, $strBackupDestinationPath);
|
|
|
|
|
|
|
|
$oFile->link_create(PATH_BACKUP_TMP, ${strBackupDestinationPath},
|
2014-06-04 18:58:30 +03:00
|
|
|
PATH_BACKUP_TMP,
|
2015-01-21 17:44:08 +02:00
|
|
|
'base/pg_tblspc/' . $oBackupManifest->get(MANIFEST_SECTION_BACKUP_TABLESPACE, $strTablespaceName,
|
|
|
|
MANIFEST_SUBKEY_LINK),
|
2014-03-06 03:53:13 +03:00
|
|
|
false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "cannot find type for section ${strSectionPath}");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create all the sub paths if this is a full backup or hardlinks are requested
|
|
|
|
if ($bPathCreate)
|
|
|
|
{
|
2015-01-21 17:44:08 +02:00
|
|
|
foreach my $strPath ($oBackupManifest->keys($strSectionPath))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-01-21 17:44:08 +02:00
|
|
|
$oFile->path_create(PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strPath}", undef, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Possible for the path section to exist with no files (i.e. empty tablespace)
|
2015-01-21 17:44:08 +02:00
|
|
|
if (!$oBackupManifest->test($strSectionFile))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Iterate through the files for each backup source path
|
2015-01-21 17:44:08 +02:00
|
|
|
foreach my $strFile ($oBackupManifest->keys($strSectionFile))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
my $strBackupSourceFile = "${strBackupSourcePath}/${strFile}";
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-10-15 04:44:42 +03:00
|
|
|
my $bProcess = false;
|
|
|
|
my $bProcessChecksumOnly = false;
|
|
|
|
|
2015-01-22 01:37:49 +02:00
|
|
|
if ($oBackupManifest->test($strSectionFile, $strFile, MANIFEST_SUBKEY_EXISTS, true))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
&log(TRACE, "file ${strFile} already exists from previous backup attempt");
|
2015-01-21 17:44:08 +02:00
|
|
|
$oBackupManifest->remove($strSectionFile, $strFile, MANIFEST_SUBKEY_EXISTS);
|
2014-10-15 04:44:42 +03:00
|
|
|
|
2015-03-01 03:31:56 +02:00
|
|
|
$bProcess = !$oBackupManifest->test($strSectionFile, $strFile, MANIFEST_SUBKEY_CHECKSUM);
|
2014-10-15 04:44:42 +03:00
|
|
|
$bProcessChecksumOnly = $bProcess;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# If the file has a reference it does not need to be copied since it can be retrieved from the referenced backup.
|
|
|
|
# However, if hard-linking is turned on the link will need to be created
|
2015-01-21 17:44:08 +02:00
|
|
|
my $strReference = $oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_REFERENCE, false);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
if (defined($strReference))
|
|
|
|
{
|
|
|
|
# If hardlinking is turned on then create a hardlink for files that have not changed since the last backup
|
|
|
|
if ($bHardLink)
|
|
|
|
{
|
|
|
|
&log(DEBUG, "hard-linking ${strBackupSourceFile} from ${strReference}");
|
|
|
|
|
|
|
|
$oFile->link_create(PATH_BACKUP_CLUSTER, "${strReference}/${strBackupDestinationPath}/${strFile}",
|
|
|
|
PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strFile}", true, false, !$bPathCreate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else copy/compress the file and generate a checksum
|
|
|
|
else
|
|
|
|
{
|
2014-10-15 04:44:42 +03:00
|
|
|
$bProcess = true;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
2014-10-15 04:44:42 +03:00
|
|
|
|
|
|
|
if ($bProcess)
|
|
|
|
{
|
2015-01-21 17:44:08 +02:00
|
|
|
my $lFileSize = $oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_SIZE);
|
2014-10-15 04:44:42 +03:00
|
|
|
|
|
|
|
# Setup variables needed for threaded copy
|
|
|
|
$lFileTotal++;
|
|
|
|
$lFileLargeSize += $lFileSize > $iSmallFileThreshold ? $lFileSize : 0;
|
|
|
|
$lFileLargeTotal += $lFileSize > $iSmallFileThreshold ? 1 : 0;
|
|
|
|
$lFileSmallSize += $lFileSize <= $iSmallFileThreshold ? $lFileSize : 0;
|
|
|
|
$lFileSmallTotal += $lFileSize <= $iSmallFileThreshold ? 1 : 0;
|
|
|
|
|
|
|
|
# Load the hash used by threaded copy
|
|
|
|
my $strKey = sprintf('ts%012x-fs%012x-fn%012x', $lTablespaceIdx,
|
|
|
|
$lFileSize, $lFileTotal);
|
|
|
|
|
|
|
|
$oFileCopyMap{"${strKey}"}{db_file} = $strBackupSourceFile;
|
|
|
|
$oFileCopyMap{"${strKey}"}{file_section} = $strSectionFile;
|
|
|
|
$oFileCopyMap{"${strKey}"}{file} = ${strFile};
|
|
|
|
$oFileCopyMap{"${strKey}"}{backup_file} = "${strBackupDestinationPath}/${strFile}";
|
|
|
|
$oFileCopyMap{"${strKey}"}{size} = $lFileSize;
|
|
|
|
$oFileCopyMap{"${strKey}"}{modification_time} =
|
2015-01-21 17:44:08 +02:00
|
|
|
$oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_MODIFICATION_TIME);
|
2014-10-15 04:44:42 +03:00
|
|
|
$oFileCopyMap{"${strKey}"}{checksum_only} = $bProcessChecksumOnly;
|
|
|
|
$oFileCopyMap{"${strKey}"}{checksum} =
|
2015-01-21 17:44:08 +02:00
|
|
|
$oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_CHECKSUM, false);
|
2014-10-15 04:44:42 +03:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Build the thread queues
|
|
|
|
$iThreadLocalMax = thread_init(int($lFileTotal / $iThreadThreshold) + 1);
|
|
|
|
&log(DEBUG, "actual threads ${iThreadLocalMax}/${iThreadMax}");
|
|
|
|
|
|
|
|
# Initialize the thread size array
|
|
|
|
my @oyThreadData;
|
|
|
|
|
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadLocalMax; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
$oyThreadData[$iThreadIdx]{size} = 0;
|
|
|
|
$oyThreadData[$iThreadIdx]{total} = 0;
|
|
|
|
$oyThreadData[$iThreadIdx]{large_size} = 0;
|
|
|
|
$oyThreadData[$iThreadIdx]{large_total} = 0;
|
|
|
|
$oyThreadData[$iThreadIdx]{small_size} = 0;
|
|
|
|
$oyThreadData[$iThreadIdx]{small_total} = 0;
|
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Assign files to each thread queue
|
|
|
|
my $iThreadFileSmallIdx = 0;
|
|
|
|
my $iThreadFileSmallTotalMax = int($lFileSmallTotal / $iThreadLocalMax);
|
|
|
|
|
|
|
|
my $iThreadFileLargeIdx = 0;
|
|
|
|
my $fThreadFileLargeSizeMax = $lFileLargeSize / $iThreadLocalMax;
|
|
|
|
|
|
|
|
&log(INFO, "file total ${lFileTotal}");
|
2014-03-30 01:16:08 +03:00
|
|
|
&log(DEBUG, "file small total ${lFileSmallTotal}, small size: " . file_size_format($lFileSmallSize) .
|
2014-09-16 15:55:40 +03:00
|
|
|
', small thread avg total ' . file_size_format(int($iThreadFileSmallTotalMax)));
|
2014-03-30 01:16:08 +03:00
|
|
|
&log(DEBUG, "file large total ${lFileLargeTotal}, large size: " . file_size_format($lFileLargeSize) .
|
2014-09-16 15:55:40 +03:00
|
|
|
', large thread avg size ' . file_size_format(int($fThreadFileLargeSizeMax)));
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
foreach my $strFile (sort (keys %oFileCopyMap))
|
|
|
|
{
|
|
|
|
my $lFileSize = $oFileCopyMap{"${strFile}"}{size};
|
|
|
|
|
|
|
|
if ($lFileSize > $iSmallFileThreshold)
|
|
|
|
{
|
|
|
|
$oThreadQueue[$iThreadFileLargeIdx]->enqueue($strFile);
|
|
|
|
|
|
|
|
$oyThreadData[$iThreadFileLargeIdx]{large_size} += $lFileSize;
|
|
|
|
$oyThreadData[$iThreadFileLargeIdx]{large_total}++;
|
|
|
|
$oyThreadData[$iThreadFileLargeIdx]{size} += $lFileSize;
|
|
|
|
|
|
|
|
if ($oyThreadData[$iThreadFileLargeIdx]{large_size} >= $fThreadFileLargeSizeMax &&
|
|
|
|
$iThreadFileLargeIdx < $iThreadLocalMax - 1)
|
|
|
|
{
|
|
|
|
$iThreadFileLargeIdx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oThreadQueue[$iThreadFileSmallIdx]->enqueue($strFile);
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
$oyThreadData[$iThreadFileSmallIdx]{small_size} += $lFileSize;
|
|
|
|
$oyThreadData[$iThreadFileSmallIdx]{small_total}++;
|
|
|
|
$oyThreadData[$iThreadFileSmallIdx]{size} += $lFileSize;
|
|
|
|
|
|
|
|
if ($oyThreadData[$iThreadFileSmallIdx]{small_total} >= $iThreadFileSmallTotalMax &&
|
|
|
|
$iThreadFileSmallIdx < $iThreadLocalMax - 1)
|
|
|
|
{
|
|
|
|
$iThreadFileSmallIdx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
if ($iThreadLocalMax > 1)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-02-28 17:23:33 +02:00
|
|
|
# End each thread queue and start the backup_file threads
|
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadLocalMax; $iThreadIdx++)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-02-28 17:23:33 +02:00
|
|
|
# Output info about how much work each thread is going to do
|
|
|
|
&log(DEBUG, "thread ${iThreadIdx} large total $oyThreadData[$iThreadIdx]{large_total}, " .
|
|
|
|
"size $oyThreadData[$iThreadIdx]{large_size}");
|
|
|
|
&log(DEBUG, "thread ${iThreadIdx} small total $oyThreadData[$iThreadIdx]{small_total}, " .
|
|
|
|
"size $oyThreadData[$iThreadIdx]{small_size}");
|
|
|
|
|
|
|
|
# Start the thread
|
2015-03-01 03:31:56 +02:00
|
|
|
$oThread[$iThreadIdx] = threads->create(\&backup_file_thread, true, $iThreadIdx, !$bPathCreate,
|
2015-02-28 17:23:33 +02:00
|
|
|
$oyThreadData[$iThreadIdx]{size}, $oBackupManifest);
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# Wait for the threads to complete
|
|
|
|
backup_thread_complete($iThreadTimeout);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# Read the messages that we passed back from the threads. These should be two types:
|
|
|
|
# 1) remove - files that were skipped because they were removed from the database during backup
|
|
|
|
# 2) checksum - file checksums calculated by the threads
|
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadLocalMax; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
while (my $strMessage = $oMasterQueue[$iThreadIdx]->dequeue_nb())
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-02-28 17:23:33 +02:00
|
|
|
&log (DEBUG, "message received in master queue: ${strMessage}");
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# Split the message. Currently using | as the split character. Not ideal, but it will do for now.
|
|
|
|
my @strSplit = split(/\|/, $strMessage);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
my $strCommand = $strSplit[0]; # Command to execute on a file
|
|
|
|
my $strFileSection = $strSplit[1]; # File section where the file is located
|
|
|
|
my $strFile = $strSplit[2]; # The file to act on
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# These three parts are required
|
|
|
|
if (!defined($strCommand) || !defined($strFileSection) || !defined($strFile))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, 'thread messages must have strCommand, strFileSection and strFile defined');
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
&log (DEBUG, "command = ${strCommand}, file_section = ${strFileSection}, file = ${strFile}");
|
|
|
|
|
|
|
|
# If command is 'remove' then mark the skipped file in the manifest
|
|
|
|
if ($strCommand eq 'remove')
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-02-28 17:23:33 +02:00
|
|
|
$oBackupManifest->remove($strFileSection, $strFile);
|
|
|
|
|
|
|
|
&log (INFO, "removed file ${strFileSection}:${strFile} from the manifest (it was removed by db during backup)");
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2015-02-28 17:23:33 +02:00
|
|
|
# If command is 'checksum' then record the checksum in the manifest
|
|
|
|
elsif ($strCommand eq 'checksum')
|
|
|
|
{
|
|
|
|
my $strChecksum = $strSplit[3]; # File checksum calculated by the thread
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# Checksum must be defined
|
|
|
|
if (!defined($strChecksum))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, 'thread checksum messages must have strChecksum defined');
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
$oBackupManifest->set($strFileSection, $strFile, MANIFEST_SUBKEY_CHECKSUM, $strChecksum);
|
|
|
|
|
|
|
|
# Log the checksum
|
|
|
|
&log (DEBUG, "write checksum ${strFileSection}:${strFile} into manifest: ${strChecksum}");
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-28 17:23:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(DEBUG, "starting backup in main process");
|
2015-03-01 03:31:56 +02:00
|
|
|
backup_file_thread(false, 0, !$bPathCreate, $oyThreadData[0]{size}, $oBackupManifest);
|
2015-02-28 17:23:33 +02:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sub backup_file_thread
|
|
|
|
{
|
2015-03-01 03:31:56 +02:00
|
|
|
my $bMulti = shift; # Is this thread one of many?
|
|
|
|
my $iThreadIdx = shift; # Defines the index of this thread
|
|
|
|
my $bPathCreate = shift; # Should paths be created automatically?
|
|
|
|
my $lSizeTotal = shift; # Total size of the files to be copied by this thread
|
|
|
|
my $oBackupManifest = shift; # Backup manifest object (only used when single-threaded)
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
my $lSize = 0; # Size of files currently copied by this thread
|
|
|
|
my $strLog; # Store the log message
|
2015-03-01 02:07:29 +02:00
|
|
|
my $strLogProgress; # Part of the log message that shows progress
|
2015-02-28 17:23:33 +02:00
|
|
|
my $oFileThread; # Thread local file object
|
2015-03-01 02:07:29 +02:00
|
|
|
my $bCopyResult; # Copy result
|
|
|
|
my $strCopyChecksum; # Copy checksum
|
|
|
|
my $lCopySize; # Copy Size
|
2015-02-28 17:23:33 +02:00
|
|
|
|
|
|
|
# If multi-threaded, then clone the file object
|
|
|
|
if ($bMulti)
|
|
|
|
{
|
|
|
|
$oFileThread = $oFile->clone($iThreadIdx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oFileThread = $oFile;
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# When a KILL signal is received, immediately abort
|
|
|
|
$SIG{'KILL'} = sub {threads->exit();};
|
|
|
|
|
|
|
|
# Iterate through all the files in this thread's queue to be copied from the database to the backup
|
2015-02-03 03:11:32 +02:00
|
|
|
while (my $strFile = $oThreadQueue[$iThreadIdx]->dequeue_nb())
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
# Add the size of the current file to keep track of percent complete
|
|
|
|
$lSize += $oFileCopyMap{$strFile}{size};
|
|
|
|
|
2014-10-15 04:44:42 +03:00
|
|
|
if (!$oFileCopyMap{$strFile}{checksum_only})
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-10-15 04:44:42 +03:00
|
|
|
# Output information about the file to be copied
|
2015-03-01 02:07:29 +02:00
|
|
|
$strLog = "thread ${iThreadIdx} backing up file";
|
2014-10-15 04:44:42 +03:00
|
|
|
|
|
|
|
# Copy the file from the database to the backup (will return false if the source file is missing)
|
2015-03-01 02:07:29 +02:00
|
|
|
($bCopyResult, $strCopyChecksum, $lCopySize) =
|
|
|
|
$oFileThread->copy(PATH_DB_ABSOLUTE, $oFileCopyMap{$strFile}{db_file},
|
|
|
|
PATH_BACKUP_TMP, $oFileCopyMap{$strFile}{backup_file} .
|
|
|
|
($bCompress ? '.' . $oFile->{strCompressExtension} : ''),
|
|
|
|
false, # Source is not compressed since it is the db directory
|
|
|
|
$bCompress, # Destination should be compressed based on backup settings
|
|
|
|
true, # Ignore missing files
|
|
|
|
$oFileCopyMap{$strFile}{modification_time}, # Set modification time
|
|
|
|
undef, # Do not set original permissions
|
|
|
|
true); # Create the destination directory if it does not exist
|
|
|
|
|
|
|
|
if (!$bCopyResult)
|
2014-10-15 04:44:42 +03:00
|
|
|
{
|
|
|
|
# If file is missing assume the database removed it (else corruption and nothing we can do!)
|
|
|
|
&log(INFO, "thread ${iThreadIdx} skipped file removed by database: " . $oFileCopyMap{$strFile}{db_file});
|
2014-05-13 18:23:15 +03:00
|
|
|
|
2015-02-28 17:23:33 +02:00
|
|
|
# Remove file from the manifest
|
|
|
|
if ($bMulti)
|
|
|
|
{
|
|
|
|
# Write a message into the master queue to have the file removed from the manifest
|
|
|
|
$oMasterQueue[$iThreadIdx]->enqueue("remove|$oFileCopyMap{$strFile}{file_section}|".
|
|
|
|
"$oFileCopyMap{$strFile}{file}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# remove it directly
|
|
|
|
$oBackupManifest->remove($oFileCopyMap{$strFile}{file_section}, $oFileCopyMap{$strFile}{file});
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-10-15 04:44:42 +03:00
|
|
|
# Move on to the next file
|
|
|
|
next;
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-03-01 02:07:29 +02:00
|
|
|
$strLogProgress = "$oFileCopyMap{$strFile}{db_file} (" . file_size_format($lCopySize) .
|
|
|
|
($lSizeTotal > 0 ? ', ' . int($lSize * 100 / $lSizeTotal) . '%' : '') . ')';
|
|
|
|
|
2014-05-13 18:23:15 +03:00
|
|
|
# Generate checksum for file if configured
|
2015-03-01 03:31:56 +02:00
|
|
|
if ($lCopySize != 0)
|
2014-10-15 04:44:42 +03:00
|
|
|
{
|
2015-02-28 17:23:33 +02:00
|
|
|
# Store checksum in the manifest
|
|
|
|
if ($bMulti)
|
|
|
|
{
|
|
|
|
# Write the checksum message into the master queue
|
|
|
|
$oMasterQueue[$iThreadIdx]->enqueue("checksum|$oFileCopyMap{$strFile}{file_section}|" .
|
2015-03-01 02:07:29 +02:00
|
|
|
"$oFileCopyMap{$strFile}{file}|${strCopyChecksum}");
|
2015-02-28 17:23:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Write it directly
|
|
|
|
$oBackupManifest->set($oFileCopyMap{$strFile}{file_section}, $oFileCopyMap{$strFile}{file},
|
2015-03-01 02:07:29 +02:00
|
|
|
MANIFEST_SUBKEY_CHECKSUM, $strCopyChecksum);
|
2015-02-28 17:23:33 +02:00
|
|
|
}
|
2014-10-15 04:44:42 +03:00
|
|
|
|
2015-01-23 00:43:19 +02:00
|
|
|
# Output information about the file to be checksummed
|
|
|
|
if (!defined($strLog))
|
|
|
|
{
|
2015-03-01 02:07:29 +02:00
|
|
|
$strLog = "thread ${iThreadIdx} checksum-only ${strLogProgress}";
|
2015-01-23 00:43:19 +02:00
|
|
|
}
|
|
|
|
|
2015-03-01 02:07:29 +02:00
|
|
|
&log(INFO, $strLog . " checksum ${strCopyChecksum}");
|
2014-10-15 04:44:42 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-01 02:07:29 +02:00
|
|
|
&log(INFO, $strLog . ' ' . $strLogProgress);
|
2014-10-15 04:44:42 +03:00
|
|
|
}
|
2015-02-04 03:33:33 +02:00
|
|
|
|
|
|
|
&log(TRACE, "thread waiting for new file from queue");
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
&log(DEBUG, "thread ${iThreadIdx} exiting");
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# Performs the entire database backup.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup
|
|
|
|
{
|
|
|
|
my $strDbClusterPath = shift;
|
2014-03-30 01:16:08 +03:00
|
|
|
my $bStartFast = shift;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-08-13 02:04:46 +03:00
|
|
|
# Record timestamp start
|
2014-08-13 03:00:52 +03:00
|
|
|
my $strTimestampStart = timestamp_string_get();
|
2014-08-13 02:04:46 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Not supporting remote backup hosts yet
|
|
|
|
if ($oFile->is_remote(PATH_BACKUP))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'remote backup host not currently supported');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($strDbClusterPath))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'cluster data path is not defined');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&log(DEBUG, "cluster path is $strDbClusterPath");
|
|
|
|
|
|
|
|
# Create the cluster backup path
|
2014-06-22 21:51:28 +03:00
|
|
|
$oFile->path_create(PATH_BACKUP_CLUSTER, undef, undef, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-01-20 21:42:22 +02:00
|
|
|
# Build backup tmp and config
|
|
|
|
my $strBackupTmpPath = $oFile->path_get(PATH_BACKUP_TMP);
|
|
|
|
my $strBackupConfFile = $oFile->path_get(PATH_BACKUP_TMP, 'backup.manifest');
|
|
|
|
|
2014-09-18 23:42:13 +03:00
|
|
|
# Declare the backup manifest
|
2015-01-20 21:42:22 +02:00
|
|
|
my $oBackupManifest = new BackRest::Manifest($strBackupConfFile, false);
|
2014-09-18 23:42:13 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Find the previous backup based on the type
|
2015-02-03 01:33:11 +02:00
|
|
|
my $oLastManifest = undef;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
my $strBackupLastPath = backup_type_find($strType, $oFile->path_get(PATH_BACKUP_CLUSTER));
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (defined($strBackupLastPath))
|
|
|
|
{
|
2015-01-20 21:13:35 +02:00
|
|
|
$oLastManifest = new BackRest::Manifest($oFile->path_get(PATH_BACKUP_CLUSTER) . "/${strBackupLastPath}/backup.manifest");
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-03 01:33:11 +02:00
|
|
|
&log(INFO, 'last backup label: ' . $oLastManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL) .
|
|
|
|
', version ' . $oLastManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_VERSION));
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-10-14 15:27:50 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($strType eq BACKUP_TYPE_DIFF)
|
|
|
|
{
|
|
|
|
&log(WARN, 'No full backup exists, differential backup has been changed to full');
|
|
|
|
}
|
|
|
|
elsif ($strType eq BACKUP_TYPE_INCR)
|
|
|
|
{
|
|
|
|
&log(WARN, 'No prior backup exists, incremental backup has been changed to full');
|
|
|
|
}
|
|
|
|
|
|
|
|
$strType = BACKUP_TYPE_FULL;
|
|
|
|
}
|
|
|
|
|
2015-01-21 01:00:03 +02:00
|
|
|
# Backup settings
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE, undef, $strType);
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_START, undef, $strTimestampStart);
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS, undef, $bCompress ? 'y' : 'n');
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK, undef, $bHardLink ? 'y' : 'n');
|
2014-10-19 23:30:16 +03:00
|
|
|
|
2014-09-20 00:51:51 +03:00
|
|
|
# Start backup (unless no-start-stop is set)
|
|
|
|
my $strArchiveStart;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-09-20 00:51:51 +03:00
|
|
|
if ($bNoStartStop)
|
|
|
|
{
|
2014-12-19 00:05:06 +02:00
|
|
|
if ($oFile->exists(PATH_DB_ABSOLUTE, $strDbClusterPath . '/' . FILE_POSTMASTER_PID))
|
2014-09-20 00:51:51 +03:00
|
|
|
{
|
|
|
|
if ($bForce)
|
|
|
|
{
|
2014-12-19 00:05:06 +02:00
|
|
|
&log(WARN, '--no-start-stop passed and ' . FILE_POSTMASTER_PID . ' exists but --force was passed so backup will ' .
|
|
|
|
'continue though it looks like the postmaster is running and the backup will probably not be ' .
|
|
|
|
'consistent');
|
2014-09-20 00:51:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-19 00:05:06 +02:00
|
|
|
&log(ERROR, '--no-start-stop passed but ' . FILE_POSTMASTER_PID . ' exists - looks like the postmaster is ' .
|
|
|
|
'running. Shutdown the postmaster and try again, or use --force.');
|
2014-09-20 00:51:51 +03:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-21 01:00:03 +02:00
|
|
|
my $strTimestampDbStart;
|
|
|
|
|
|
|
|
($strArchiveStart, $strTimestampDbStart) =
|
|
|
|
$oDb->backup_start('pg_backrest backup started ' . $strTimestampStart, $bStartFast);
|
|
|
|
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_DB_START, undef, $strTimestampDbStart);
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_ARCHIVE_START, undef, $strArchiveStart);
|
|
|
|
&log(INFO, "archive start: ${strArchiveStart}");
|
2014-09-20 00:51:51 +03:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-01-21 01:00:03 +02:00
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_VERSION, undef, version_get());
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Build the backup manifest
|
2014-06-22 21:51:28 +03:00
|
|
|
my %oTablespaceMap;
|
2014-09-20 00:51:51 +03:00
|
|
|
|
2015-02-01 01:23:37 +02:00
|
|
|
if (!$bNoStartStop)
|
2014-09-20 00:51:51 +03:00
|
|
|
{
|
|
|
|
$oDb->tablespace_map_get(\%oTablespaceMap);
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-02-01 01:15:08 +02:00
|
|
|
$oBackupManifest->build($oFile, $strDbClusterPath, $oLastManifest, $bNoStartStop, \%oTablespaceMap);
|
2014-07-28 01:13:23 +03:00
|
|
|
&log(TEST, TEST_MANIFEST_BUILD);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
# Check if an aborted backup exists for this stanza
|
2014-03-06 03:53:13 +03:00
|
|
|
if (-e $strBackupTmpPath)
|
|
|
|
{
|
2014-10-14 15:27:50 +03:00
|
|
|
my $bUsable = false;
|
|
|
|
|
2015-01-22 17:54:02 +02:00
|
|
|
my $strType = $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE);
|
|
|
|
my $strPrior = $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR, undef, false, '<undef>');
|
|
|
|
my $strVersion = $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_VERSION);
|
|
|
|
|
|
|
|
my $strAbortedType = '<undef>';
|
|
|
|
my $strAbortedPrior = '<undef>';
|
|
|
|
my $strAbortedVersion = '<undef>';
|
2015-01-23 00:43:19 +02:00
|
|
|
my $oAbortedManifest;
|
2015-01-22 17:54:02 +02:00
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
# Attempt to read the manifest file in the aborted backup to see if the backup type and prior backup are the same as the
|
|
|
|
# new backup that is being started. If any error at all occurs then the backup will be considered unusable and a resume
|
|
|
|
# will not be attempted.
|
|
|
|
eval
|
|
|
|
{
|
2014-10-14 22:44:50 +03:00
|
|
|
# Load the aborted manifest
|
2015-01-23 00:43:19 +02:00
|
|
|
$oAbortedManifest = new BackRest::Manifest("${strBackupTmpPath}/backup.manifest");
|
2014-10-14 15:27:50 +03:00
|
|
|
|
2014-10-14 22:44:50 +03:00
|
|
|
# Default values if they are not set
|
2015-01-22 17:54:02 +02:00
|
|
|
$strAbortedType = $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE);
|
|
|
|
$strAbortedPrior = $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR, undef, false, '<undef>');
|
|
|
|
$strAbortedVersion = $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_VERSION);
|
2014-10-14 22:44:50 +03:00
|
|
|
|
|
|
|
# The backup is usable if between the current backup and the aborted backup:
|
|
|
|
# 1) The version matches
|
|
|
|
# 2) The type of both is full or the types match and prior matches
|
2015-01-22 17:54:02 +02:00
|
|
|
if ($strAbortedVersion eq $strVersion)
|
2014-10-14 15:27:50 +03:00
|
|
|
{
|
2015-01-21 01:00:03 +02:00
|
|
|
if ($strAbortedType eq BACKUP_TYPE_FULL
|
|
|
|
&& $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE) eq BACKUP_TYPE_FULL)
|
2014-10-14 22:44:50 +03:00
|
|
|
{
|
|
|
|
$bUsable = true;
|
|
|
|
}
|
2015-01-21 01:00:03 +02:00
|
|
|
elsif ($strAbortedType eq $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE) &&
|
|
|
|
$strAbortedPrior eq $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR))
|
2014-10-14 15:27:50 +03:00
|
|
|
{
|
|
|
|
$bUsable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
# If the aborted backup is usable then clean it
|
|
|
|
if ($bUsable)
|
|
|
|
{
|
|
|
|
&log(WARN, 'aborted backup of same type exists, will be cleaned to remove invalid files and resumed');
|
2015-01-22 17:54:02 +02:00
|
|
|
&log(TEST, TEST_BACKUP_RESUME);
|
2014-10-14 15:27:50 +03:00
|
|
|
|
|
|
|
# Clean the old backup tmp path
|
2015-01-23 00:43:19 +02:00
|
|
|
backup_tmp_clean($oBackupManifest, $oAbortedManifest);
|
2014-10-14 15:27:50 +03:00
|
|
|
}
|
|
|
|
# Else remove it
|
|
|
|
else
|
|
|
|
{
|
2015-01-22 17:54:02 +02:00
|
|
|
my $strReason = "new version '${strVersion}' does not match aborted version '${strVersion}'";
|
|
|
|
|
|
|
|
if ($strVersion eq $strAbortedVersion)
|
|
|
|
{
|
|
|
|
if ($strType ne $strAbortedType)
|
|
|
|
{
|
|
|
|
$strReason = "new type '${strType}' does not match aborted type '${strAbortedType}'";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strReason = "new prior '${strPrior}' does not match aborted prior '${strAbortedPrior}'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&log(WARN, "aborted backup exists, but cannot be resumed (${strReason}) - will be dropped and recreated");
|
|
|
|
&log(TEST, TEST_BACKUP_NORESUME);
|
2014-10-14 15:27:50 +03:00
|
|
|
|
|
|
|
remove_tree($oFile->path_get(PATH_BACKUP_TMP))
|
|
|
|
or confess &log(ERROR, "unable to delete tmp path: ${strBackupTmpPath}");
|
|
|
|
$oFile->path_create(PATH_BACKUP_TMP);
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
# Else create the backup tmp path
|
|
|
|
else
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(DEBUG, "creating backup path ${strBackupTmpPath}");
|
2014-03-06 03:53:13 +03:00
|
|
|
$oFile->path_create(PATH_BACKUP_TMP);
|
|
|
|
}
|
|
|
|
|
2015-01-20 21:42:22 +02:00
|
|
|
# Save the backup manifest
|
|
|
|
$oBackupManifest->save();
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Perform the backup
|
2015-01-21 17:44:08 +02:00
|
|
|
backup_file($strDbClusterPath, $oBackupManifest);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-09-20 00:51:51 +03:00
|
|
|
# Stop backup (unless no-start-stop is set)
|
|
|
|
my $strArchiveStop;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-09-20 00:51:51 +03:00
|
|
|
if (!$bNoStartStop)
|
|
|
|
{
|
2015-01-21 01:00:03 +02:00
|
|
|
my $strTimestampDbStop;
|
|
|
|
($strArchiveStop, $strTimestampDbStop) = $oDb->backup_stop();
|
|
|
|
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_DB_STOP, undef, $strTimestampDbStop);
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_ARCHIVE_STOP, undef, $strArchiveStop);
|
|
|
|
|
|
|
|
&log(INFO, 'archive stop: ' . $strArchiveStop);
|
2014-09-20 00:51:51 +03:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-06-04 18:58:30 +03:00
|
|
|
# If archive logs are required to complete the backup, then fetch them. This is the default, but can be overridden if the
|
2014-03-06 03:53:13 +03:00
|
|
|
# archive logs are going to a different server. Be careful here because there is no way to verify that the backup will be
|
2014-06-04 18:58:30 +03:00
|
|
|
# consistent - at least not in this routine.
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($bArchiveRequired)
|
|
|
|
{
|
2015-01-20 21:42:22 +02:00
|
|
|
# Save the backup manifest a second time - before getting archive logs in case that fails
|
|
|
|
$oBackupManifest->save();
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-01-27 18:44:23 +02:00
|
|
|
# Create the modification time for the archive logs
|
|
|
|
my $lModificationTime = time();
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# After the backup has been stopped, need to make a copy of the archive logs need to make the db consistent
|
|
|
|
&log(DEBUG, "retrieving archive logs ${strArchiveStart}:${strArchiveStop}");
|
2014-08-10 01:35:55 +03:00
|
|
|
my @stryArchive = archive_list_get($strArchiveStart, $strArchiveStop, $oDb->db_version_get() < 9.3);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
foreach my $strArchive (@stryArchive)
|
|
|
|
{
|
|
|
|
my $strArchivePath = dirname($oFile->path_get(PATH_BACKUP_ARCHIVE, $strArchive));
|
|
|
|
|
|
|
|
wait_for_file($strArchivePath, "^${strArchive}(-[0-f]+){0,1}(\\.$oFile->{strCompressExtension}){0,1}\$", 600);
|
|
|
|
|
2014-06-22 21:51:28 +03:00
|
|
|
my @stryArchiveFile = $oFile->list(PATH_BACKUP_ABSOLUTE, $strArchivePath,
|
2014-03-06 03:53:13 +03:00
|
|
|
"^${strArchive}(-[0-f]+){0,1}(\\.$oFile->{strCompressExtension}){0,1}\$");
|
|
|
|
|
|
|
|
if (scalar @stryArchiveFile != 1)
|
|
|
|
{
|
2014-06-04 18:58:30 +03:00
|
|
|
confess &log(ERROR, "Zero or more than one file found for glob: ${strArchivePath}");
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&log(DEBUG, "archiving: ${strArchive} (${stryArchiveFile[0]})");
|
|
|
|
|
2015-01-27 18:44:23 +02:00
|
|
|
# Copy the log file from the archive repo to the backup
|
|
|
|
my $strDestinationFile = "base/pg_xlog/${strArchive}" . ($bCompress ? ".$oFile->{strCompressExtension}" : '');
|
|
|
|
|
2015-03-01 03:31:56 +02:00
|
|
|
my ($bCopyResult, $strCopyChecksum, $lCopySize) =
|
|
|
|
$oFile->copy(PATH_BACKUP_ARCHIVE, $stryArchiveFile[0],
|
|
|
|
PATH_BACKUP_TMP, $strDestinationFile,
|
|
|
|
$stryArchiveFile[0] =~ "^.*\.$oFile->{strCompressExtension}\$",
|
|
|
|
$bCompress, undef, $lModificationTime);
|
2015-01-27 18:44:23 +02:00
|
|
|
|
|
|
|
# Add the archive file to the manifest so it can be part of the restore and checked in validation
|
|
|
|
my $strPathSection = 'base:path';
|
|
|
|
my $strPathLog = 'pg_xlog';
|
|
|
|
my $strFileSection = 'base:file';
|
|
|
|
my $strFileLog = "pg_xlog/${strArchive}";
|
|
|
|
|
2015-03-01 03:31:56 +02:00
|
|
|
# Compare the checksum against the one already in the archive log name
|
|
|
|
if ($stryArchiveFile[0] =~ "^${strArchive}-[0-f]+(\\.$oFile->{strCompressExtension}){0,1}\$" &&
|
|
|
|
$stryArchiveFile[0] !~ "^${strArchive}-${strCopyChecksum}(\\.$oFile->{strCompressExtension}){0,1}\$")
|
2015-01-27 18:44:23 +02:00
|
|
|
{
|
2015-03-01 03:31:56 +02:00
|
|
|
confess &log(ERROR, "error copying log '$stryArchiveFile[0]' to backup - checksum recorded with file does " .
|
|
|
|
"not match actual checksum of '${strCopyChecksum}'", ERROR_CHECKSUM);
|
2015-01-27 18:44:23 +02:00
|
|
|
}
|
|
|
|
|
2015-03-01 03:31:56 +02:00
|
|
|
# Set manifest values
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_USER,
|
|
|
|
$oBackupManifest->get($strPathSection, $strPathLog, MANIFEST_SUBKEY_USER));
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_GROUP,
|
|
|
|
$oBackupManifest->get($strPathSection, $strPathLog, MANIFEST_SUBKEY_GROUP));
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_MODE, '0700');
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_MODIFICATION_TIME, $lModificationTime);
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_SIZE, $lCopySize);
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_CHECKSUM, $strCopyChecksum);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 02:17:16 +03:00
|
|
|
# Create the path for the new backup
|
|
|
|
my $strBackupPath;
|
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
if ($strType eq BACKUP_TYPE_FULL || !defined($strBackupLastPath))
|
2014-08-13 02:17:16 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strBackupPath = timestamp_file_string_get() . 'F';
|
2014-10-14 15:27:50 +03:00
|
|
|
$strType = BACKUP_TYPE_FULL;
|
2014-08-13 02:17:16 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strBackupPath = substr($strBackupLastPath, 0, 16);
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
$strBackupPath .= '_' . timestamp_file_string_get();
|
2014-08-13 02:17:16 +03:00
|
|
|
|
2014-10-14 15:27:50 +03:00
|
|
|
if ($strType eq BACKUP_TYPE_DIFF)
|
2014-08-13 02:17:16 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strBackupPath .= 'D';
|
2014-08-13 02:17:16 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
$strBackupPath .= 'I';
|
2014-08-13 02:17:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 02:04:46 +03:00
|
|
|
# Record timestamp stop in the config
|
2015-01-21 01:00:03 +02:00
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_STOP, undef, timestamp_string_get());
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL, undef, $strBackupPath);
|
2014-08-13 02:04:46 +03:00
|
|
|
|
2015-01-20 21:42:22 +02:00
|
|
|
# Save the backup manifest final time
|
|
|
|
$oBackupManifest->save();
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2014-08-13 02:17:16 +03:00
|
|
|
&log(INFO, "new backup label: ${strBackupPath}");
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Rename the backup tmp path to complete the backup
|
|
|
|
&log(DEBUG, "moving ${strBackupTmpPath} to " . $oFile->path_get(PATH_BACKUP_CLUSTER, $strBackupPath));
|
2014-06-24 01:54:00 +03:00
|
|
|
$oFile->move(PATH_BACKUP_TMP, undef, PATH_BACKUP_CLUSTER, $strBackupPath);
|
2014-12-18 18:14:30 +02:00
|
|
|
|
|
|
|
# Create a link to the most recent backup
|
|
|
|
$oFile->remove(PATH_BACKUP_CLUSTER, "latest");
|
|
|
|
$oFile->link_create(PATH_BACKUP_CLUSTER, $strBackupPath, PATH_BACKUP_CLUSTER, "latest", undef, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# ARCHIVE_LIST_GET
|
|
|
|
#
|
|
|
|
# Generates a range of archive log file names given the start and end log file name. For pre-9.3 databases, use bSkipFF to exclude
|
|
|
|
# the FF that prior versions did not generate.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub archive_list_get
|
|
|
|
{
|
|
|
|
my $strArchiveStart = shift;
|
|
|
|
my $strArchiveStop = shift;
|
|
|
|
my $bSkipFF = shift;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# strSkipFF default to false
|
|
|
|
$bSkipFF = defined($bSkipFF) ? $bSkipFF : false;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($bSkipFF)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(TRACE, 'archive_list_get: pre-9.3 database, skipping log FF');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(TRACE, 'archive_list_get: post-9.3 database, including log FF');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-07-27 21:03:21 +03:00
|
|
|
# Get the timelines and make sure they match
|
2014-03-06 03:53:13 +03:00
|
|
|
my $strTimeline = substr($strArchiveStart, 0, 8);
|
|
|
|
my @stryArchive;
|
|
|
|
my $iArchiveIdx = 0;
|
|
|
|
|
|
|
|
if ($strTimeline ne substr($strArchiveStop, 0, 8))
|
|
|
|
{
|
2014-07-27 21:03:21 +03:00
|
|
|
confess &log(ERROR, "Timelines between ${strArchiveStart} and ${strArchiveStop} differ");
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-07-27 21:03:21 +03:00
|
|
|
# Iterate through all archive logs between start and stop
|
2014-03-06 03:53:13 +03:00
|
|
|
my $iStartMajor = hex substr($strArchiveStart, 8, 8);
|
|
|
|
my $iStartMinor = hex substr($strArchiveStart, 16, 8);
|
|
|
|
|
|
|
|
my $iStopMajor = hex substr($strArchiveStop, 8, 8);
|
|
|
|
my $iStopMinor = hex substr($strArchiveStop, 16, 8);
|
|
|
|
|
|
|
|
$stryArchive[$iArchiveIdx] = uc(sprintf("${strTimeline}%08x%08x", $iStartMajor, $iStartMinor));
|
|
|
|
$iArchiveIdx += 1;
|
|
|
|
|
|
|
|
while (!($iStartMajor == $iStopMajor && $iStartMinor == $iStopMinor))
|
|
|
|
{
|
|
|
|
$iStartMinor += 1;
|
|
|
|
|
|
|
|
if ($bSkipFF && $iStartMinor == 255 || !$bSkipFF && $iStartMinor == 256)
|
|
|
|
{
|
|
|
|
$iStartMajor += 1;
|
|
|
|
$iStartMinor = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stryArchive[$iArchiveIdx] = uc(sprintf("${strTimeline}%08x%08x", $iStartMajor, $iStartMinor));
|
|
|
|
$iArchiveIdx += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
&log(TRACE, " archive_list_get: $strArchiveStart:$strArchiveStop (@stryArchive)");
|
|
|
|
|
|
|
|
return @stryArchive;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BACKUP_EXPIRE
|
2014-06-04 18:58:30 +03:00
|
|
|
#
|
2014-03-06 03:53:13 +03:00
|
|
|
# Removes expired backups and archive logs from the backup directory. Partial backups are not counted for expiration, so if full
|
|
|
|
# or differential retention is set to 2, there must be three complete backups before the oldest one can be deleted.
|
|
|
|
#
|
|
|
|
# iFullRetention - Optional, must be greater than 0 when supplied.
|
|
|
|
# iDifferentialRetention - Optional, must be greater than 0 when supplied.
|
|
|
|
# strArchiveRetention - Optional, must be (full,differential/diff,incremental/incr) when supplied
|
|
|
|
# iArchiveRetention - Required when strArchiveRetention is supplied. Must be greater than 0.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub backup_expire
|
|
|
|
{
|
|
|
|
my $strBackupClusterPath = shift; # Base path to cluster backup
|
|
|
|
my $iFullRetention = shift; # Number of full backups to keep
|
|
|
|
my $iDifferentialRetention = shift; # Number of differential backups to keep
|
|
|
|
my $strArchiveRetentionType = shift; # Type of backup to base archive retention on
|
|
|
|
my $iArchiveRetention = shift; # Number of backups worth of archive to keep
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
my $strPath;
|
|
|
|
my @stryPath;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Find all the expired full backups
|
|
|
|
if (defined($iFullRetention))
|
|
|
|
{
|
|
|
|
# Make sure iFullRetention is valid
|
|
|
|
if (!looks_like_number($iFullRetention) || $iFullRetention < 1)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'full_rentention must be a number >= 1');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
my $iIndex = $iFullRetention;
|
2014-09-16 15:55:40 +03:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(1, 0, 0), 'reverse');
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
while (defined($stryPath[$iIndex]))
|
|
|
|
{
|
|
|
|
# Delete all backups that depend on the full backup. Done in reverse order so that remaining backups will still
|
|
|
|
# be consistent if the process dies
|
2014-09-16 15:55:40 +03:00
|
|
|
foreach $strPath ($oFile->list(PATH_BACKUP_CLUSTER, undef, '^' . $stryPath[$iIndex] . '.*', 'reverse'))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
system("rm -rf ${strBackupClusterPath}/${strPath}") == 0 or confess &log(ERROR, "unable to delete backup ${strPath}");
|
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(INFO, 'removed expired full backup: ' . $stryPath[$iIndex]);
|
2014-03-30 01:16:08 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
$iIndex++;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Find all the expired differential backups
|
|
|
|
if (defined($iDifferentialRetention))
|
|
|
|
{
|
|
|
|
# Make sure iDifferentialRetention is valid
|
|
|
|
if (!looks_like_number($iDifferentialRetention) || $iDifferentialRetention < 1)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'differential_rentention must be a number >= 1');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(0, 1, 0), 'reverse');
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-14 23:48:33 +03:00
|
|
|
if (defined($stryPath[$iDifferentialRetention - 1]))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-14 23:48:33 +03:00
|
|
|
&log(DEBUG, 'differential expiration based on ' . $stryPath[$iDifferentialRetention - 1]);
|
2014-09-15 03:06:45 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Get a list of all differential and incremental backups
|
2014-09-16 15:55:40 +03:00
|
|
|
foreach $strPath ($oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(0, 1, 1), 'reverse'))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-14 23:48:33 +03:00
|
|
|
&log(DEBUG, "checking ${strPath} for differential expiration");
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Remove all differential and incremental backups before the oldest valid differential
|
2014-09-14 23:48:33 +03:00
|
|
|
if ($strPath lt $stryPath[$iDifferentialRetention - 1])
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
system("rm -rf ${strBackupClusterPath}/${strPath}") == 0 or confess &log(ERROR, "unable to delete backup ${strPath}");
|
|
|
|
&log(INFO, "removed expired diff/incr backup ${strPath}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# If no archive retention type is set then exit
|
|
|
|
if (!defined($strArchiveRetentionType))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(INFO, 'archive rentention type not set - archive logs will not be expired');
|
2014-03-06 03:53:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Determine which backup type to use for archive retention (full, differential, incremental)
|
2014-10-14 15:27:50 +03:00
|
|
|
if ($strArchiveRetentionType eq BACKUP_TYPE_FULL)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-14 22:55:27 +03:00
|
|
|
if (!defined($iArchiveRetention))
|
|
|
|
{
|
|
|
|
$iArchiveRetention = $iFullRetention;
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(1, 0, 0), 'reverse');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-10-14 15:27:50 +03:00
|
|
|
elsif ($strArchiveRetentionType eq BACKUP_TYPE_DIFF)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-14 22:55:27 +03:00
|
|
|
if (!defined($iArchiveRetention))
|
|
|
|
{
|
|
|
|
$iArchiveRetention = $iDifferentialRetention;
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(1, 1, 0), 'reverse');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-10-14 15:27:50 +03:00
|
|
|
elsif ($strArchiveRetentionType eq BACKUP_TYPE_INCR)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backup_regexp_get(1, 1, 1), 'reverse');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unknown archive_retention_type '${strArchiveRetentionType}'");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure that iArchiveRetention is set and valid
|
|
|
|
if (!defined($iArchiveRetention))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'archive_rentention must be set if archive_retention_type is set');
|
2014-03-06 03:53:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!looks_like_number($iArchiveRetention) || $iArchiveRetention < 1)
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
confess &log(ERROR, 'archive_rentention must be a number >= 1');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# if no backups were found then preserve current archive logs - too scary to delete them!
|
|
|
|
my $iBackupTotal = scalar @stryPath;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if ($iBackupTotal == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# See if enough backups exist for expiration to start
|
|
|
|
my $strArchiveRetentionBackup = $stryPath[$iArchiveRetention - 1];
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (!defined($strArchiveRetentionBackup))
|
|
|
|
{
|
2014-10-14 15:27:50 +03:00
|
|
|
if ($strArchiveRetentionType eq BACKUP_TYPE_FULL && scalar @stryPath > 0)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(INFO, 'fewer than required backups for retention, but since archive_retention_type = full using oldest full backup');
|
2014-03-06 03:53:13 +03:00
|
|
|
$strArchiveRetentionBackup = $stryPath[scalar @stryPath - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($strArchiveRetentionBackup))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the archive logs that need to be kept. To be cautious we will keep all the archive logs starting from this backup
|
|
|
|
# even though they are also in the pg_xlog directory (since they have been copied more than once).
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(INFO, 'archive retention based on backup ' . $strArchiveRetentionBackup);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-01-21 01:00:03 +02:00
|
|
|
my $oManifest = new BackRest::Manifest($oFile->path_get(PATH_BACKUP_CLUSTER) . "/${strArchiveRetentionBackup}/backup.manifest");
|
|
|
|
my $strArchiveLast = $oManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_ARCHIVE_START);
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
if (!defined($strArchiveLast))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "invalid archive location retrieved ${strArchiveRetentionBackup}");
|
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(INFO, 'archive retention starts at ' . $strArchiveLast);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# Remove any archive directories or files that are out of date
|
2014-06-22 21:51:28 +03:00
|
|
|
foreach $strPath ($oFile->list(PATH_BACKUP_ARCHIVE, undef, "^[0-F]{16}\$"))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(DEBUG, 'found major archive path ' . $strPath);
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# If less than first 16 characters of current archive file, then remove the directory
|
|
|
|
if ($strPath lt substr($strArchiveLast, 0, 16))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
my $strFullPath = $oFile->path_get(PATH_BACKUP_ARCHIVE) . "/${strPath}";
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
remove_tree($strFullPath) > 0 or confess &log(ERROR, "unable to remove ${strFullPath}");
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-09-16 15:55:40 +03:00
|
|
|
&log(DEBUG, 'removed major archive path ' . $strFullPath);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
# If equals the first 16 characters of the current archive file, then delete individual files instead
|
|
|
|
elsif ($strPath eq substr($strArchiveLast, 0, 16))
|
|
|
|
{
|
|
|
|
my $strSubPath;
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Look for archive files in the archive directory
|
2014-06-22 21:51:28 +03:00
|
|
|
foreach $strSubPath ($oFile->list(PATH_BACKUP_ARCHIVE, $strPath, "^[0-F]{24}.*\$"))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
# Delete if the first 24 characters less than the current archive file
|
|
|
|
if ($strSubPath lt substr($strArchiveLast, 0, 24))
|
|
|
|
{
|
2014-09-16 15:55:40 +03:00
|
|
|
unlink($oFile->path_get(PATH_BACKUP_ARCHIVE, $strSubPath)) or confess &log(ERROR, 'unable to remove ' . $strSubPath);
|
|
|
|
&log(DEBUG, 'removed expired archive file ' . $strSubPath);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:58:30 +03:00
|
|
|
1;
|