Commit 984e773b authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Fixed up logging a bit

Added --backup-upgrade option to upgrade data
parent db2eb6bc
......@@ -58,11 +58,11 @@ my @defaultDataExcl = (
# Backup constants
use constant {
LOG_DEBUG => 1,
LOG_INFO => 2,
LOG_DEBUG => 5,
LOG_INFO => 4,
LOG_NOTICE => 3,
LOG_WARNING => 4,
LOG_ERROR => 5
LOG_WARNING => 2,
LOG_ERROR => 1
};
use constant {
......@@ -80,6 +80,7 @@ my %config = (
'log-level' => LOG_NOTICE,
'tar' => 'tar',
'exclude-system' => 0,
'backup-upgrade' => 0,
);
# Choose default compression method
if (-x "/bin/xz" || -x "/usr/bin/xz") {
......@@ -107,6 +108,7 @@ GetOptions(\%optctl,
"manifest-format=s",
"backup",
"backup-upgrade",
"compress:s",
"exclude-system",
"system-base=s@",
......@@ -235,6 +237,10 @@ if (!defined($config{'exclude-fs-type'})) {
# Check commandline options
#
if (defined($optctl{'backup-upgrade'})) {
$config{'backup-upgrade'} = 1;
}
if (defined($optctl{'compress'})) {
# Why use --compress with --restore?
if (defined($optctl{'restore'})) {
......@@ -426,7 +432,7 @@ sub backup
} elsif ( ! -f "$dest/$path/.dbackup-state" ) {
# First backup will never have the main dir .dbackup-state file existing
if ($path ne "") {
printLog(LOG_WARNING,"State file '$dest/$path/.dbackup-state' does not exist\n");
printLog(LOG_WARNING,"State file '[$dest]/($path)/.dbackup-state' does not exist\n");
}
$doBackup{$path} |= ST_DIR_NEW;
}
......@@ -438,9 +444,44 @@ sub backup
# Check we have at least the version attribute
if (!defined($origPathAttribs{$path}) || !defined($origPathAttribs{$path}->{'dbackup.version'})) {
printLog(LOG_ERROR,"No dbackup version information found in '$source/$path/.dbackup-state', IGNORING\n");
printLog(LOG_ERROR,"No dbackup version information found in '[$dest]/($path)/.dbackup-state', IGNORING\n");
delete($origDirList{$path});
delete($origFileList{$path});
# NK:
# Sanity check, some versions created empty'ish .dbackup-state files with no sequence < 2012-01-21
# If we have a manifest, we should have a backup, therefore a format and sequence
} elsif (
-f "$dest/$path/dbackup0.manifest" && (
!defined($origPathAttribs{$path}->{'format'}) ||
!defined($origPathAttribs{$path}->{'sequence'})
)
) {
printLog(LOG_ERROR,"No dbackup format/sequence information found in ".
"'[$dest]/($path)/.dbackup-state', IGNORING\n");
delete($origDirList{$path});
delete($origFileList{$path});
# Check if we going to upgrade...
} else {
# Are we doing backup upgrades?
if ($config{'backup-upgrade'}) {
# Pull in versions
my $a = getNumericVer($origPathAttribs{$path}->{'dbackup.version'});
my $b = getNumericVer($VERSION);
# Compare
if ($a < $b) {
# Check if end in .x
if ($VERSION =~ /\.x$/) {
printLog(LOG_WARNING,"S: Path '[$source]/($path)' ".
"cannot be upgraded, dbackup is TRUNK version\n");
} else {
printLog(LOG_INFO,"S: Path '[$source]/($path)' will be backed up, upgrading\n");
delete($origDirList{$path});
delete($origFileList{$path});
}
}
}
}
}
......@@ -654,6 +695,14 @@ sub backup
mkpath("$dest/$path");
}
# NK:
# Before we do anything, we copy our previous attributes.
# If we do not write an archive below, we still have our stuff.
# If there is something special, we still have it. Best idea? right?
foreach my $item (keys %{$origPathAttribs{$path}}) {
$newPathAttribs{$path}->{$item} = $origPathAttribs{$path}->{$item};
}
# Check if we need to backup this dir
if (($doBackup{$path} & ST_FILE_CHANGED) == ST_FILE_CHANGED) {
printLog(LOG_INFO,"B: Path '[$source]/$path' backing up\n");
......@@ -762,7 +811,7 @@ sub backup
# File list to backup
open(MANIFEST,"> $dest/$path/dbackup$seq.manifest")
or die "Failed to open '$dest/$path/dbackup$seq.manifest': $!";
or die "Failed to open '[$dest]/($path)/dbackup$seq.manifest': $!";
foreach my $item (@manifestList) {
# Sanity check to see if this filename is going to work
if ($manifestDelim eq "\n" && $item =~ /[\n"'\\]/) {
......@@ -849,18 +898,14 @@ sub backup
$newPathAttribs{$path}->{'revision'} = 0;
}
printLog(LOG_DEBUG,"B: Writing state '[$source]/$path' - revision ".$newPathAttribs{$path}->{'revision'}."\n");
# Set this as the dbackup that created this file
$newPathAttribs{$path}->{'dbackup.version'} = $VERSION;
printLog(LOG_DEBUG,"B: Writing state '[$source]/($path)' - revision ".$newPathAttribs{$path}->{'revision'}."\n");
# Write out state, we do this AFTER we have backed up, so if we stopped, we can just continue
my $gz = gzopen("$dest/$path/.dbackup-state","wb")
or die "Failed to open '$dest/$path/.dbackup-state': $!";
# Write out attributes
$gz->gzwrite(join("\0",
"a",
"dbackup.version",
$VERSION
)."\n"
);
or die "Failed to open '[$dest]/($path)/.dbackup-state': $!";
# Loop with attributes
foreach my $item (keys %{$newPathAttribs{$path}}) {
$gz->gzwrite(join("\0",
......@@ -961,7 +1006,7 @@ sub backup
$dest
);
foreach my $rmitem ( @rmlist ) {
printLog(LOG_DEBUG,"Remove path '[$dest]/$rmitem'\n");
printLog(LOG_DEBUG,"Remove path '[$dest]/($rmitem)'\n");
rmtree("$dest/$rmitem");
}
......@@ -1015,7 +1060,7 @@ sub restore
# Check we have at least the version attribute
if (!defined($origPathAttribs{$path}) || !defined($origPathAttribs{$path}->{'dbackup.version'})) {
printLog(LOG_ERROR, "No dbackup version information found in '$source/$path/.dbackup-state', IGNORING\n");
printLog(LOG_ERROR, "No dbackup version information found in '[$source]/($path)/.dbackup-state', IGNORING\n");
delete($origDirList{$path});
delete($origFileList{$path});
return @list;
......@@ -1035,7 +1080,7 @@ sub restore
# Loop through backup sequences
for (my $seq = 0; $seq <= $origPathAttribs{$path}->{'sequence'}; $seq++) {
printLog(LOG_DEBUG,"R: Path '[$dest]/$path' restoring data ".($seq+1)." of ".
printLog(LOG_DEBUG,"R: Path '[$dest]/($path)' restoring data ".($seq+1)." of ".
($origPathAttribs{$path}->{'sequence'}+1)."\n");
# Args for tar
......@@ -1063,7 +1108,7 @@ sub restore
-f "$source/$path/dbackup$seq.manifest" ||
-f "$source/$path/dbackup$seq.snar" ) && $seq > 0
) {
printLog(LOG_WARNING,"Backup file '$source/$path/dbackup$seq.tar$tarExt' not found, IGNORING\n");
printLog(LOG_WARNING,"Backup file '[$source]/($path)/dbackup$seq.tar$tarExt' not found, IGNORING\n");
}
return @list;
......@@ -1071,14 +1116,28 @@ sub restore
# Check we have files we need/created
if ( ! -f "$source/$path/dbackup$seq.index" ) {
printLog(LOG_WARNING,"Manifest file '$source/$path/dbackup$seq.index' not found\n");
printLog(LOG_WARNING,"Manifest file '[$source]/($path)/dbackup$seq.index' not found\n");
return @list;
}
if ( ! -f "$source/$path/dbackup$seq.manifest" ) {
printLog(LOG_WARNING,"Manifest file '$source/$path/dbackup$seq.manifest' not found\n");
printLog(LOG_WARNING,"Manifest file '[$source]/($path)/dbackup$seq.manifest' not found\n");
return @list;
}
# Check if manifest.format is undefined, if it is, use the default
if (!defined($origPathAttribs{$path}->{'manifest.format'})) {
# Check if we have a commandline override
if (defined($optctl{'manifest-format'})) {
$origPathAttribs{$path}->{'manifest.format'} = $optctl{'manifest-format'};
} else {
# NK:
# Default to null
# This saves us from having to regenerate manifest.format attrs and backups for older
# versions of dbackup.
$origPathAttribs{$path}->{'manifest.format'} = "null";
}
}
# Setup manifest delim & tar options
if ($origPathAttribs{$path}->{'manifest.format'} eq "null") {
push(@tarArgs,"--null");
......@@ -1087,7 +1146,8 @@ sub restore
} elsif ($origPathAttribs{$path}->{'manifest.format'} eq "newline") {
push(@tarArgs,"--files-from", "$source/$path/dbackup$seq.manifest");
} else {
printLog(LOG_ERROR,"Invalid manifest.format '".$origPathAttribs{$path}->{'manifest.format'}."'\n");
printLog(LOG_ERROR,"Invalid manifest.format '".$origPathAttribs{$path}->{'manifest.format'}.
"' for '[$source]/($path)', try setting the default with --manifest-format=\n");
exit 1;
}
......@@ -1156,7 +1216,7 @@ sub restore
# Strip first part of the path
(my $path = $File::Find::dir) =~ s#^$source/?##;
printLog(LOG_DEBUG,"R: Path '[$dest]/$path' restoring meta-data\n");
printLog(LOG_DEBUG,"R: Path '[$dest]/($path)' restoring meta-data\n");
# Loop with directories in this path
foreach my $dname (keys %{$origDirList{$path}}) {
......@@ -1166,6 +1226,12 @@ sub restore
# Full dirname
my $fdirname = "$dest/$path/$dname";
# Could be a dir with no contents of one which was ignored, in which case we must create it
if (! -d $fdirname) {
printLog(LOG_INFO,"R: Path '[$dest]/($path)/$dname' creating missing directory\n");
mkpath($fdirname);
}
# Restore attribs
if (!chown($dir->{'uid'},$dir->{'gid'},$fdirname)) {
printLog(LOG_ERROR,"Failed to chown(".$dir->{'uid'}.",".$dir->{'gid'}.") '$fdirname': $!\n");
......@@ -1240,7 +1306,7 @@ sub printLog {
}
# Check log level
if ($level >= $config{'log-level'}) {
if ($level <= $config{'log-level'}) {
printf(STDERR "%s/$levelTxt: %s", strftime('%F %T',localtime()), $msg, @args);
}
}
......@@ -1265,6 +1331,7 @@ Usage: $0 [args] <src> <dst>
Backing Options:
--backup|-b Backup src to dst.
--backup-upgrade Upgrade backup to new dbackup ver.
--compress=<xz|bz2|gz|none> Compression method to use defaults
to using xz, or bzip2 if xz unavail.
......@@ -1441,4 +1508,17 @@ sub getTarVer
return $tarVer;
}
sub getNumericVer
{
my $a = shift;
# 000 000 000
my @a = split(/\./,$a);
# Quick fix for devel versions
$a[2] = 0 if ($a[2] eq "x");
return ( ($a[0]*1000000) + ($a[1]*1000) + $a[2] );
}
# vim: ts=4
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment