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;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-07-28 01:13:23 +03:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
use Exporter qw(import);
|
|
|
|
use Fcntl 'SEEK_CUR';
|
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);
|
2015-06-14 00:25:49 +02:00
|
|
|
use BackRest::Archive;
|
|
|
|
use BackRest::BackupCommon;
|
|
|
|
use BackRest::BackupFile;
|
|
|
|
use BackRest::BackupInfo;
|
2014-12-16 19:41:54 +02:00
|
|
|
use BackRest::Config;
|
2014-06-22 17:30:17 +03:00
|
|
|
use BackRest::Db;
|
2015-06-14 00:25:49 +02:00
|
|
|
use BackRest::Exception;
|
|
|
|
use BackRest::File;
|
|
|
|
use BackRest::Ini;
|
|
|
|
use BackRest::Manifest;
|
2015-04-07 13:34:37 +02:00
|
|
|
use BackRest::ThreadGroup;
|
2015-06-14 00:25:49 +02:00
|
|
|
use BackRest::Utility;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
our @EXPORT = qw(backup_init backup_cleanup 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;
|
2014-09-20 00:51:51 +03:00
|
|
|
my $bNoStartStop;
|
|
|
|
my $bForce;
|
2015-04-07 13:34:37 +02:00
|
|
|
my $iThreadMax;
|
2014-03-06 03:53:13 +03:00
|
|
|
my $iThreadTimeout;
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# 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 $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;
|
|
|
|
$iThreadMax = $iThreadMaxParam;
|
|
|
|
$iThreadTimeout = $iThreadTimeoutParam;
|
2014-09-20 00:51:51 +03:00
|
|
|
$bNoStartStop = $bNoStartStopParam;
|
|
|
|
$bForce = $bForceParam;
|
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
|
|
|
####################################################################################################################################
|
|
|
|
# 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
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
$strDirectory = ($oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
$strDirectory = ($oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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;
|
|
|
|
|
|
|
|
foreach my $strName (sort(keys $oFileHash{name}))
|
|
|
|
{
|
|
|
|
# Ignore certain files that will never be in the manifest
|
2015-06-14 00:25:49 +02:00
|
|
|
if ($strName eq FILE_MANIFEST ||
|
2014-09-16 15:55:40 +03:00
|
|
|
$strName eq '.')
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
if ($strName eq MANIFEST_KEY_BASE)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
if ($oManifest->test(MANIFEST_SECTION_BACKUP_PATH, $strName))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
elsif ($strName eq MANIFEST_TABLESPACE)
|
|
|
|
{
|
|
|
|
my $bFound = false;
|
|
|
|
|
|
|
|
foreach my $strPath ($oManifest->keys(MANIFEST_SECTION_BACKUP_PATH))
|
|
|
|
{
|
|
|
|
if ($strPath =~ /^$strName\//)
|
|
|
|
{
|
|
|
|
$bFound = true;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next if $bFound;
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
else
|
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strBasePath = (split('/', $strName))[0];
|
2014-03-06 03:53:13 +03:00
|
|
|
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
|
|
|
|
2015-06-14 00:25:49 +02: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;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
elsif ($cType eq 'f')
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-05-07 02:24:34 +02:00
|
|
|
if ($oManifest->test("${strSection}:file", "${strPath}") &&
|
|
|
|
!$oManifest->test("${strSection}:file", $strPath, MANIFEST_SUBKEY_REFERENCE))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-05-07 02:24:34 +02:00
|
|
|
my $strChecksum = $oAbortedManifest->get("${strSection}:file", $strPath, MANIFEST_SUBKEY_CHECKSUM, false);
|
|
|
|
|
|
|
|
if (defined($strChecksum) &&
|
2015-06-14 00:25:49 +02:00
|
|
|
$oManifest->getNumeric("${strSection}:file", $strPath, MANIFEST_SUBKEY_SIZE) ==
|
2015-05-07 02:24:34 +02:00
|
|
|
$oFileHash{name}{$strName}{size} &&
|
2015-06-14 00:25:49 +02:00
|
|
|
$oManifest->getNumeric("${strSection}:file", $strPath, MANIFEST_SUBKEY_TIMESTAMP) ==
|
2015-05-07 02:24:34 +02:00
|
|
|
$oFileHash{name}{$strName}{modification_time})
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-05-07 02:24:34 +02:00
|
|
|
$oManifest->set("${strSection}:file", $strPath, MANIFEST_SUBKEY_CHECKSUM, $strChecksum);
|
2014-03-06 03:53:13 +03:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
push @stryFile, $strName;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-04-07 13:34:37 +02:00
|
|
|
my %oFileCopyMap;
|
2014-03-06 03:53:13 +03:00
|
|
|
my $lFileTotal = 0;
|
2015-04-07 13:34:37 +02:00
|
|
|
my $lSizeTotal = 0;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
# Determine whether all paths and links will be created
|
|
|
|
my $bFullCreate = $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-04-07 13:34:37 +02:00
|
|
|
foreach my $strPathKey ($oBackupManifest->keys(MANIFEST_SECTION_BACKUP_PATH))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
|
|
|
# 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
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
$strBackupSourcePath = $oBackupManifest->get(MANIFEST_SECTION_BACKUP_PATH, $strPathKey, MANIFEST_SUBKEY_PATH);
|
|
|
|
$strBackupDestinationPath = $strPathKey;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
# Create links for tablespaces
|
|
|
|
if ($oBackupManifest->test(MANIFEST_SECTION_BACKUP_PATH, $strPathKey, MANIFEST_SUBKEY_LINK) && $bFullCreate)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
$oFile->link_create(PATH_BACKUP_TMP, $strBackupDestinationPath,
|
|
|
|
PATH_BACKUP_TMP,
|
|
|
|
'base/pg_tblspc/' . $oBackupManifest->get(MANIFEST_SECTION_BACKUP_PATH,
|
|
|
|
$strPathKey, MANIFEST_SUBKEY_LINK),
|
|
|
|
false, true, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
# If this is a full backup or hard-linked then create all paths and links
|
|
|
|
if ($bFullCreate)
|
|
|
|
{
|
|
|
|
# Create paths
|
|
|
|
my $strSectionPath = "$strPathKey:path";
|
|
|
|
|
|
|
|
if ($oBackupManifest->test($strSectionPath))
|
|
|
|
{
|
|
|
|
foreach my $strPath ($oBackupManifest->keys($strSectionPath))
|
|
|
|
{
|
|
|
|
if ($strPath ne '.')
|
|
|
|
{
|
|
|
|
$oFile->path_create(PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strPath}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create links
|
|
|
|
my $strSectionLink = "$strPathKey:link";
|
|
|
|
|
|
|
|
if ($oBackupManifest->test($strSectionLink))
|
|
|
|
{
|
|
|
|
foreach my $strLink ($oBackupManifest->keys($strSectionLink))
|
|
|
|
{
|
|
|
|
# Create links except in pg_tblspc because they have already been created
|
|
|
|
if (!($strPathKey eq 'base' && $strLink =~ /^pg_tblspc\/.*/))
|
|
|
|
{
|
|
|
|
$oFile->link_create(PATH_BACKUP_ABSOLUTE,
|
|
|
|
$oBackupManifest->get($strSectionLink, $strLink, MANIFEST_SUBKEY_DESTINATION),
|
|
|
|
PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strLink}",
|
|
|
|
false, false, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Possible for the file section to exist with no files (i.e. empty tablespace)
|
2015-05-07 02:24:34 +02:00
|
|
|
my $strSectionFile = "$strPathKey:file";
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
# 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
|
|
|
|
my $bProcess = true;
|
|
|
|
my $strReference = $oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_REFERENCE, false);
|
2014-10-15 04:44:42 +03:00
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
if (defined($strReference))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-05-07 02:24:34 +02:00
|
|
|
# If hardlinking is turned on then create a hardlink for files that have not changed since the last backup
|
|
|
|
if ($bHardLink)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
&log(DEBUG, "hardlink ${strBackupSourceFile} to ${strReference}");
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
$oFile->link_create(PATH_BACKUP_CLUSTER, "${strReference}/${strBackupDestinationPath}/${strFile}",
|
|
|
|
PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strFile}", true, false, true);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(DEBUG, "reference ${strBackupSourceFile} to ${strReference}");
|
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
|
|
|
|
$bProcess = false;
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-10-15 04:44:42 +03:00
|
|
|
|
|
|
|
if ($bProcess)
|
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
my $lFileSize = $oBackupManifest->getNumeric($strSectionFile, $strFile, MANIFEST_SUBKEY_SIZE);
|
2014-10-15 04:44:42 +03:00
|
|
|
|
|
|
|
# Setup variables needed for threaded copy
|
|
|
|
$lFileTotal++;
|
2015-04-07 13:34:37 +02:00
|
|
|
$lSizeTotal += $lFileSize;
|
|
|
|
|
2015-06-14 15:36:58 +02:00
|
|
|
my $strFileKey = sprintf("%016d-${strFile}", $lFileSize);
|
|
|
|
|
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{db_file} = $strBackupSourceFile;
|
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{file_section} = $strSectionFile;
|
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{file} = ${strFile};
|
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{backup_file} = "${strBackupDestinationPath}/${strFile}";
|
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{size} = $lFileSize;
|
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{modification_time} =
|
2015-06-14 00:25:49 +02:00
|
|
|
$oBackupManifest->getNumeric($strSectionFile, $strFile, MANIFEST_SUBKEY_TIMESTAMP, false);
|
2015-06-14 15:36:58 +02:00
|
|
|
$oFileCopyMap{$strPathKey}{$strFileKey}{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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# If there are no files to backup then we'll exit with a warning unless in test mode. The other way this could happen is if
|
|
|
|
# the database is down and backup is called with --no-start-stop twice in a row.
|
|
|
|
if ($lFileTotal == 0)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
if (!optionGet(OPTION_TEST))
|
|
|
|
{
|
2015-05-07 02:24:34 +02:00
|
|
|
confess &log(ERROR, "no files have changed since the last backup - this seems unlikely");
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Create backup and result queues
|
|
|
|
my $oResultQueue = Thread::Queue->new();
|
|
|
|
my @oyBackupQueue;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Variables used for local copy
|
2015-05-07 02:24:34 +02:00
|
|
|
my $lSizeCurrent = 0; # Running total of bytes copied
|
|
|
|
my $bCopied; # Was the file copied?
|
|
|
|
my $lCopySize; # Size reported by copy
|
|
|
|
my $strCopyChecksum; # Checksum reported by copy
|
|
|
|
|
|
|
|
# Determine how often the manifest will be saved
|
|
|
|
my $lManifestSaveCurrent = 0;
|
|
|
|
my $lManifestSaveSize = int($lSizeTotal / 100);
|
|
|
|
|
|
|
|
if ($lManifestSaveSize < optionGet(OPTION_MANIFEST_SAVE_THRESHOLD))
|
|
|
|
{
|
|
|
|
$lManifestSaveSize = optionGet(OPTION_MANIFEST_SAVE_THRESHOLD);
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Iterate all backup files
|
|
|
|
foreach my $strPathKey (sort (keys %oFileCopyMap))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
if ($iThreadMax > 1)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
$oyBackupQueue[@oyBackupQueue] = Thread::Queue->new();
|
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-06-14 15:36:58 +02:00
|
|
|
foreach my $strFileKey (sort {$b cmp $a} (keys $oFileCopyMap{$strPathKey}))
|
2015-04-07 13:34:37 +02:00
|
|
|
{
|
2015-06-14 15:36:58 +02:00
|
|
|
my $oFileCopy = $oFileCopyMap{$strPathKey}{$strFileKey};
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
if ($iThreadMax > 1)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
$oyBackupQueue[@oyBackupQueue - 1]->enqueue($oFileCopy);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2015-04-07 13:34:37 +02:00
|
|
|
else
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
# Backup the file
|
|
|
|
($bCopied, $lSizeCurrent, $lCopySize, $strCopyChecksum) =
|
|
|
|
backupFile($oFile, $$oFileCopy{db_file}, $$oFileCopy{backup_file}, $bCompress,
|
2015-05-07 02:24:34 +02:00
|
|
|
$$oFileCopy{checksum}, $$oFileCopy{modification_time},
|
2015-04-07 13:34:37 +02:00
|
|
|
$$oFileCopy{size}, $lSizeTotal, $lSizeCurrent);
|
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
$lManifestSaveCurrent = backupManifestUpdate($oBackupManifest, $$oFileCopy{file_section}, $$oFileCopy{file},
|
|
|
|
$bCopied, $lCopySize, $strCopyChecksum, $lManifestSaveSize,
|
|
|
|
$lManifestSaveCurrent);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# If multi-threaded then create threads to copy files
|
|
|
|
if ($iThreadMax > 1)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadMax; $iThreadIdx++)
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
my %oParam;
|
|
|
|
|
|
|
|
$oParam{compress} = $bCompress;
|
|
|
|
$oParam{size_total} = $lSizeTotal;
|
|
|
|
$oParam{queue} = \@oyBackupQueue;
|
|
|
|
$oParam{result_queue} = $oResultQueue;
|
|
|
|
|
|
|
|
threadGroupRun($iThreadIdx, 'backup', \%oParam);
|
2015-02-28 17:23:33 +02:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Complete thread queues
|
2015-05-07 02:24:34 +02:00
|
|
|
my $bDone = false;
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
do
|
2015-02-28 17:23:33 +02:00
|
|
|
{
|
2015-05-07 02:24:34 +02:00
|
|
|
$bDone = threadGroupComplete();
|
|
|
|
|
|
|
|
# Read the messages that are passed back from the backup threads
|
|
|
|
while (my $oMessage = $oResultQueue->dequeue_nb())
|
|
|
|
{
|
|
|
|
&log(TRACE, "message received in master queue: section = $$oMessage{file_section}, file = $$oMessage{file}" .
|
|
|
|
", copied = $$oMessage{copied}");
|
2014-05-13 18:23:15 +03:00
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
$lManifestSaveCurrent = backupManifestUpdate($oBackupManifest, $$oMessage{file_section}, $$oMessage{file},
|
|
|
|
$$oMessage{copied}, $$oMessage{size}, $$oMessage{checksum},
|
|
|
|
$lManifestSaveSize, $lManifestSaveCurrent);
|
|
|
|
}
|
2014-10-15 04:44:42 +03:00
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
while (!$bDone);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
|
2015-05-26 18:26:59 +02:00
|
|
|
&log(INFO, 'total backup size: ' . file_size_format($lSizeTotal));
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# 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
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
# Record timestamp start
|
|
|
|
my $lTimestampStart = time();
|
|
|
|
|
2015-05-26 18:26:59 +02:00
|
|
|
# Backup start
|
|
|
|
&log(INFO, "backup start: type = ${strType}");
|
|
|
|
|
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-06-14 00:25:49 +02:00
|
|
|
# Load or build backup.info
|
|
|
|
my $oBackupInfo = new BackRest::BackupInfo($oFile->path_get(PATH_BACKUP_CLUSTER));
|
|
|
|
|
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-05-05 19:08:48 +02:00
|
|
|
&log(INFO, 'last backup label = ' . $oLastManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL) .
|
2015-06-14 00:25:49 +02:00
|
|
|
', version = ' . $oLastManifest->get(INI_SECTION_BACKREST, INI_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);
|
2015-06-14 00:25:49 +02:00
|
|
|
$oBackupManifest->setNumeric(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_START, undef, $lTimestampStart);
|
|
|
|
$oBackupManifest->setBool(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS, undef, $bCompress);
|
|
|
|
$oBackupManifest->setBool(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK, undef, $bHardLink);
|
|
|
|
$oBackupManifest->setBool(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_START_STOP, undef, !$bNoStartStop);
|
|
|
|
$oBackupManifest->setBool(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_ARCHIVE_COPY, undef,
|
|
|
|
$bNoStartStop || optionGet(OPTION_BACKUP_ARCHIVE_COPY));
|
|
|
|
$oBackupManifest->setBool(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_ARCHIVE_CHECK, undef,
|
|
|
|
$bNoStartStop || optionGet(OPTION_BACKUP_ARCHIVE_CHECK));
|
|
|
|
|
|
|
|
# Database info
|
|
|
|
my ($strDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId) = $oDb->info($oFile, $strDbClusterPath);
|
|
|
|
|
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_DB_VERSION, undef, $strDbVersion);
|
|
|
|
$oBackupManifest->setNumeric(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_CONTROL, undef, $iControlVersion);
|
|
|
|
$oBackupManifest->setNumeric(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_CATALOG, undef, $iCatalogVersion);
|
|
|
|
$oBackupManifest->setNumeric(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_SYSTEM_ID, undef, $ullDbSysId);
|
|
|
|
|
|
|
|
$oBackupInfo->check($oBackupManifest);
|
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) =
|
2015-06-14 00:25:49 +02:00
|
|
|
$oDb->backup_start('pg_backrest backup started ' . timestamp_string_get(undef, $lTimestampStart), $bStartFast);
|
2015-01-21 01:00:03 +02:00
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
# Build the backup manifest
|
2015-05-22 20:49:14 +02:00
|
|
|
my $oTablespaceMap = $bNoStartStop ? undef : $oDb->tablespace_map_get();
|
2014-09-20 00:51:51 +03:00
|
|
|
|
2015-05-22 20:49:14 +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>');
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strVersion = $oBackupManifest->get(INI_SECTION_BACKREST, INI_KEY_VERSION);
|
2015-01-22 17:54:02 +02:00
|
|
|
|
|
|
|
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>');
|
2015-06-14 00:25:49 +02:00
|
|
|
$strAbortedVersion = $oAbortedManifest->get(INI_SECTION_BACKREST, INI_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
|
2015-05-07 18:29:30 +02:00
|
|
|
if ($bUsable && optionGet(OPTION_RESUME))
|
2014-10-14 15:27:50 +03:00
|
|
|
{
|
|
|
|
&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-05-07 18:29:30 +02:00
|
|
|
my $strReason = "resume is disabled";
|
2015-01-22 17:54:02 +02:00
|
|
|
|
2015-05-07 18:29:30 +02:00
|
|
|
if (optionGet(OPTION_RESUME))
|
2015-01-22 17:54:02 +02:00
|
|
|
{
|
2015-05-07 18:29:30 +02:00
|
|
|
if ($strVersion eq $strAbortedVersion)
|
2015-01-22 17:54:02 +02:00
|
|
|
{
|
2015-05-07 18:29:30 +02:00
|
|
|
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}'";
|
|
|
|
}
|
2015-01-22 17:54:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-07 18:29:30 +02:00
|
|
|
$strReason = "new version '${strVersion}' does not match aborted version '${strVersion}'";
|
2015-01-22 17:54:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&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_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
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
# If archive logs are required to complete the backup, then check 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
|
2015-04-07 13:34:37 +02:00
|
|
|
# consistent - at least not here.
|
2015-03-23 22:01:15 +02:00
|
|
|
if (!optionGet(OPTION_NO_START_STOP) && optionGet(OPTION_BACKUP_ARCHIVE_CHECK))
|
2014-03-06 03:53:13 +03:00
|
|
|
{
|
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}");
|
2015-04-01 21:58:33 +02:00
|
|
|
my $oArchive = new BackRest::Archive();
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strArchiveId = $oArchive->getCheck($oFile);
|
2015-04-01 21:58:33 +02:00
|
|
|
my @stryArchive = $oArchive->range($strArchiveStart, $strArchiveStop, $oDb->db_version_get() < 9.3);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
|
|
|
foreach my $strArchive (@stryArchive)
|
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strArchiveFile = $oArchive->walFileName($oFile, $strArchiveId, $strArchive, 600);
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-03-23 22:01:15 +02:00
|
|
|
if (optionGet(OPTION_BACKUP_ARCHIVE_COPY))
|
2015-03-16 20:45:53 +02:00
|
|
|
{
|
2015-04-01 21:58:33 +02:00
|
|
|
&log(DEBUG, "archiving: ${strArchive} (${strArchiveFile})");
|
2015-03-16 20:45:53 +02:00
|
|
|
|
|
|
|
# Copy the log file from the archive repo to the backup
|
|
|
|
my $strDestinationFile = "base/pg_xlog/${strArchive}" . ($bCompress ? ".$oFile->{strCompressExtension}" : '');
|
2015-05-22 20:49:14 +02:00
|
|
|
my $bArchiveCompressed = $strArchiveFile =~ "^.*\.$oFile->{strCompressExtension}\$";
|
2014-03-06 03:53:13 +03:00
|
|
|
|
2015-03-16 20:45:53 +02:00
|
|
|
my ($bCopyResult, $strCopyChecksum, $lCopySize) =
|
2015-06-14 00:25:49 +02:00
|
|
|
$oFile->copy(PATH_BACKUP_ARCHIVE, "${strArchiveId}/${strArchiveFile}",
|
2015-03-16 20:45:53 +02:00
|
|
|
PATH_BACKUP_TMP, $strDestinationFile,
|
2015-05-22 20:49:14 +02:00
|
|
|
$bArchiveCompressed, $bCompress,
|
|
|
|
undef, $lModificationTime, undef, true);
|
2015-01-27 18:44:23 +02:00
|
|
|
|
2015-03-16 20:45:53 +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-01-27 18:44:23 +02:00
|
|
|
|
2015-03-16 20:45:53 +02:00
|
|
|
# Compare the checksum against the one already in the archive log name
|
2015-04-01 21:58:33 +02:00
|
|
|
if ($strArchiveFile !~ "^${strArchive}-${strCopyChecksum}(\\.$oFile->{strCompressExtension}){0,1}\$")
|
2015-03-16 20:45:53 +02:00
|
|
|
{
|
2015-04-01 21:58:33 +02:00
|
|
|
confess &log(ERROR, "error copying WAL segment '${strArchiveFile}' to backup - checksum recorded with " .
|
|
|
|
"file does not match actual checksum of '${strCopyChecksum}'", ERROR_CHECKSUM);
|
2015-03-16 20:45:53 +02:00
|
|
|
}
|
2015-01-27 18:44:23 +02:00
|
|
|
|
2015-03-16 20:45:53 +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');
|
2015-06-14 00:25:49 +02:00
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_TIMESTAMP, $lModificationTime);
|
2015-03-16 20:45:53 +02:00
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_SIZE, $lCopySize);
|
|
|
|
$oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_CHECKSUM, $strCopyChecksum);
|
2015-01-27 18:44:23 +02:00
|
|
|
}
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 02:17:16 +03:00
|
|
|
# Create the path for the new backup
|
2015-06-14 00:25:49 +02:00
|
|
|
my $lTimestampStop = time();
|
2014-08-13 02:17:16 +03:00
|
|
|
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);
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
$strBackupPath .= '_' . timestamp_file_string_get(undef, $lTimestampStop);
|
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-06-14 00:25:49 +02:00
|
|
|
$oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_STOP, undef, $lTimestampStop + 0);
|
2015-01-21 01:00:03 +02:00
|
|
|
$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);
|
2015-05-26 18:26:59 +02:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
# Save backup info
|
|
|
|
$oBackupInfo->backupAdd($oFile, $oBackupManifest);
|
|
|
|
|
2015-05-26 18:26:59 +02:00
|
|
|
# Backup stop
|
|
|
|
&log(INFO, 'backup stop');
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# 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
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
# Load or build backup.info
|
|
|
|
my $oBackupInfo = new BackRest::BackupInfo($oFile->path_get(PATH_BACKUP_CLUSTER));
|
|
|
|
|
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;
|
2015-06-14 00:25:49 +02:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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
|
|
|
{
|
2015-03-18 00:31:05 +02:00
|
|
|
system("rm -rf ${strBackupClusterPath}/${strPath}") == 0
|
|
|
|
or confess &log(ERROR, "unable to delete backup ${strPath}");
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
$oBackupInfo->backupRemove($strPath);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
2014-06-04 18:58:30 +03:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
&log(INFO, 'remove 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
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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
|
2015-06-14 00:25:49 +02:00
|
|
|
foreach $strPath ($oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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
|
|
|
{
|
2015-03-18 00:31:05 +02:00
|
|
|
system("rm -rf ${strBackupClusterPath}/${strPath}") == 0
|
|
|
|
or confess &log(ERROR, "unable to delete backup ${strPath}");
|
2015-06-14 00:25:49 +02:00
|
|
|
$oBackupInfo->backupRemove($strPath);
|
|
|
|
|
|
|
|
&log(INFO, "remove expired diff/incr backup ${strPath}");
|
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 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;
|
|
|
|
}
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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;
|
|
|
|
}
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
@stryPath = $oFile->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(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
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
# Get archive info
|
|
|
|
my $oArchive = new BackRest::Archive();
|
|
|
|
my $strArchiveId = $oArchive->getCheck($oFile);
|
|
|
|
|
2014-03-06 03:53:13 +03:00
|
|
|
# Remove any archive directories or files that are out of date
|
2015-06-14 00:25:49 +02:00
|
|
|
foreach $strPath ($oFile->list(PATH_BACKUP_ARCHIVE, $strArchiveId, "^[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))
|
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strFullPath = $oFile->path_get(PATH_BACKUP_ARCHIVE, $strArchiveId) . "/${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
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
&log(DEBUG, 'remove 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
|
2015-06-14 00:25:49 +02:00
|
|
|
foreach $strSubPath ($oFile->list(PATH_BACKUP_ARCHIVE, "${strArchiveId}/${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))
|
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
unlink($oFile->path_get(PATH_BACKUP_ARCHIVE, "${strArchiveId}/${strSubPath}"))
|
2015-03-18 00:31:05 +02:00
|
|
|
or confess &log(ERROR, 'unable to remove ' . $strSubPath);
|
2015-06-14 00:25:49 +02:00
|
|
|
&log(DEBUG, 'remove expired archive file ' . $strSubPath);
|
2014-03-06 03:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:58:30 +03:00
|
|
|
1;
|