Newer
Older
)."\n"
);
}
# Finally close
$gz->gzclose();
}
# State clear
delete($origFileList{$path});
}
printLog(LOG_NOTICE,"BACKUP START: $source => $dest\n");
printLog(LOG_INFO,"Compression: ".$config{'compress'}." [".$config{'compress-'.$config{'compress'}}."]\n");
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# Check if we excluding system files
if ($config{'exclude-system'}) {
printLog(LOG_NOTICE,"Exclude System Base: ".join(", ",@{$config{'system-base'}})."\n");
printLog(LOG_NOTICE,"Exclude System Dirs: ".join(", ",@defaultSystemExcl,@{$config{'system-dir'}})."\n");
# If we excluding data too...
if ($config{'exclude-data'}) {
printLog(LOG_NOTICE,"Exclude Data Dirs: ".join(", ",@defaultDataExcl,@{$config{'data-dir'}})."\n");
}
}
printLog(LOG_NOTICE,"Exclude Paths: ".join(", ",@{$config{'exclude-path'}})."\n");
printLog(LOG_NOTICE,"Exclude Files: ".join(", ",@{$config{'exclude-file'}})."\n");
# We need an entry for our main dir before we start
$doBackup{""} = 0;
# This basically does our backup for us...
find (
{
preprocess => \&backup_preprocess,
wanted => \&backup_process,
postprocess => \&backup_postprocess
},
$source
);
printLog(LOG_INFO,"Processing stale directories...\n");
my @rmlist;
# Total item by item
find (
{
wanted => sub { },
# We just need post processing ...
postprocess => sub {
# Strip first part of the path
(my $path = $File::Find::dir) =~ s#^$dest/?##;
# Does this dir exist on the backup, but not on the fileserver?
if (!defined($srcFileList{$path})) {
push(@rmlist,$path);
}
},
},
$dest
);
foreach my $rmitem ( @rmlist ) {
printLog(LOG_DEBUG,"Remove path '[$dest]/($rmitem)'\n");
rmtree("$dest/$rmitem");
}
printLog(LOG_NOTICE,"BACKUP END\n");
}
# If we backing up or restoring, we need to check the compression program
if (defined($optctl{'backup'}) || defined($optctl{'restore'})) {
# One last check for to make sure the compression program exists
if (checkPATH(my $compressProgram = "compress-".$config{'compress'})) {
print(STDERR "ERROR: Compression program '$compressProgram' cannot be found in path!");
exit 1;
}
}
# Check if we backing up
if (defined($optctl{'backup'})) {
backup($sourceDir,$destDir);
} elsif (defined($optctl{'restore'})) {
restore($sourceDir,$destDir);
} else {
print(STDERR "ERROR: No command given\n\n");
displayHelp();
exit 1;
}
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# Restore function
sub restore
{
our ($source,$dest) = @_;
# State infor for current dir
our %origFileList;
our %origDirList;
our %origPathAttribs;
# Preprocess dir
sub restore_preprocess {
my @list = @_;
# Strip first part of the path
(my $path = $File::Find::dir) =~ s#^$source/?##;
# Check if we have all our source files
if ( ! -f "$source/$path/.dbackup-state" ) {
printLog(LOG_WARNING,"S: Path '[$source]/($path)' is MISSING .dbackup-state\n");
return @list;
}
# Load state file
loadStateFile("$source/$path/.dbackup-state",$path,\%origFileList,\%origDirList,\%origPathAttribs);
# 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");
delete($origDirList{$path});
delete($origFileList{$path});
return @list;
}
# During our check, we need to create the dir, we may not have a backup file
# so we do it here.
if ( ! -d "$dest/$path") {
printLog(LOG_INFO,"R: Path '[$dest]/($path)' creating directory\n");
mkpath("$dest/$path");
}
# The lack of a sequence number probably means we didn't output any tar files during backup
if (!defined($origPathAttribs{$path}->{'sequence'})) {
return @list;
}
# 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 ".
($origPathAttribs{$path}->{'sequence'}+1)."\n");
# Args for tar
my @tarArgs = ();
my $tarExt;
if ($origPathAttribs{$path}->{'compression'} eq "xz") {
push(@tarArgs,"--use-compress-program",$config{'compress-xz'});
$tarExt = ".xz";
} elsif ($origPathAttribs{$path}->{'compression'} eq "bzip2") {
push(@tarArgs,"--use-compress-program",$config{'compress-bzip2'});
$tarExt = ".bz2";
} elsif ($origPathAttribs{$path}->{'compression'} eq "gzip") {
push(@tarArgs,"--use-compress-program",$config{'compress-gzip'});
} elsif ($origPathAttribs{$path}->{'compression'} eq "lz") {
push(@tarArgs,"--use-compress-program",$config{'compress-lz'});
$tarExt = ".lz";
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
} elsif ($origPathAttribs{$path}->{'compression'} eq "none") {
$tarExt = "";
}
# Check what type of backup we did
if ( ! -f "$source/$path/dbackup$seq.tar$tarExt" ) {
# If we have any other part of the backup, show a message
if (
( -f "$source/$path/dbackup$seq.index" ||
-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");
}
return @list;
}
# 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");
return @list;
}
if ( ! -f "$source/$path/dbackup$seq.manifest" ) {
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");
push(@tarArgs,"--files-from", "$source/$path/dbackup$seq.manifest");
push(@tarArgs,"--no-unquote");
} 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'}.
"' for '[$source]/($path)', try setting the default with --manifest-format=\n");
exit 1;
}
# Sanity check
if ($tarVer < 122 && $origPathAttribs{$path}->{'manifest.format'} eq "null") {
printLog(LOG_ERROR,"Your version of tar does NOT support manifest.format of 'null'\n");
exit 1;
}
# Other tar options
if (defined($optctl{'tar-keep-newer'})) {
push(@tarArgs,"--keep-newer-files");
}
if (defined($optctl{'tar-keep-old-files'})) {
push(@tarArgs,"--keep-old-files");
}
# Change dir and start backup
system(
"tar",
"--extract",
# Output file
"--file", "$source/$path/dbackup$seq.tar$tarExt",
# cd into here first...
"--directory", "$dest",
# Do not recurse
"--no-recursion",
# Incremental options
# "--incremental",
@tarArgs
);
if ($? == -1) {
printLog(LOG_ERROR,"Failed to execute: $!\n");
exit 1;
} elsif ($? & 127) {
printLog(LOG_ERROR,"Child died with signal ".($? & 127)."\n");
exit 1;
} else {
my $retcode = $? >> 8;
# If tar died, lets die too
if ($retcode >= 2) {
printLog(LOG_ERROR,"tar died with error code $retcode\n");
# exit 1;
}
}
}
return @list;
}
# Process files
sub restore_process {
my $name = $File::Find::name;
# Strip first part of the path
(my $path = $File::Find::dir) =~ s#^$source/?##;
# Store current filename
my $item = $_;
# print(STDERR "PROCESS: Path = $path, Name = $name, Item = $item)\n");
}
# Time to do stuff
sub restore_postprocess {
# Strip first part of the path
(my $path = $File::Find::dir) =~ s#^$source/?##;
printLog(LOG_DEBUG,"R: Path '[$dest]/($path)' restoring meta-data\n");
# Loop with directories in this path
foreach my $dname (keys %{$origDirList{$path}}) {
# Set dir
my $dir = $origDirList{$path}->{$dname};
# 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");
}
if (!chmod($dir->{'mode'},$fdirname)) {
printLog(LOG_ERROR,"Failed to chmod(".$dir->{'mode'}.") '$fdirname': $!\n");
}
if (!utime($dir->{'atime'},$dir->{'mtime'},$fdirname)) {
printLog(LOG_ERROR,"Failed to utime(".$dir->{'atime'}.",".$dir->{'mtime'}.") '$fdirname': $!\n");
}
}
# Loop with files in this path
foreach my $fname (keys %{$origFileList{$path}}) {
# Set dir
my $file = $origFileList{$path}->{$fname};
# Full dirname
my $ffilename = "$dest/$path/$fname";
# Restore for links and files
if (!lchown($file->{'uid'},$file->{'gid'},$ffilename)) {
printLog(LOG_ERROR,"Failed to lchown(".$file->{'uid'}.",".$file->{'gid'}.") '$ffilename': $!\n");
}
# Ignore links for the rest...
next if (S_ISLNK($file->{'mode'}));
# Restore mode & utime only for files
if (!chmod($file->{'mode'},$ffilename)) {
printLog(LOG_ERROR,"Failed to chmod(".$file->{'mode'}.") '$ffilename': $!\n");
}
if (!utime($file->{'atime'},$file->{'mtime'},$ffilename)) {
printLog(LOG_ERROR,"Failed to utime(".$file->{'atime'}.",".$file->{'mtime'}.") '$ffilename': $!\n");
}
}
}
printLog(LOG_NOTICE,"RESTORE START: $source => $dest\n");
# This basically does our backup for us...
find (
{
preprocess => \&restore_preprocess,
wanted => \&restore_process,
postprocess => \&restore_postprocess
},
$source
);
printLog(LOG_NOTICE,"RESTORE END\n");
}
# Display log line
sub printLog {
my ($level,$msg,@args) = @_;
# Work out level txt all nicely
my $levelTxt = "UNKNOWN";
if ($level == LOG_DEBUG) {
$levelTxt = "DEBUG";
} elsif ($level == LOG_INFO) {
$levelTxt = "INFO";
} elsif ($level == LOG_NOTICE) {
$levelTxt = "NOTICE";
} elsif ($level == LOG_WARNING) {
$levelTxt = "WARNING";
} elsif ($level == LOG_ERROR) {
$levelTxt = "ERROR";
}
# Check log level
if ($level <= $config{'log-level'}) {
printf(STDERR "%s/$levelTxt: %s", strftime('%F %T',localtime()), $msg, @args);
}
}
# Display usage
sub displayHelp {
# Build some strings we need
my $systemDirStr = join (", ",@defaultSystemExcl);
my $dataDirStr = join (", ",@defaultDataExcl);
print(STDERR<<EOF);
Usage: $0 [args] <src> <dst>
General Options:
--help What you seeing now.
--config=file Config file to use.
--log-level 5 = debug, 4 = info, 3 = notice
2 = warning, 1 = error
--tar Path to tar binary.
Backing Options:
--backup Backup src to dst.
--backup-upgrade Upgrade backup to new dbackup ver.
--compress=<xz|bz2|gzip|lz|none> Compression method to use defaults
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
to using xz, or bzip2 if xz unavail.
--exclude-data Exclude all data dirs listed below.
--exclude-system Exclude system dirs listed below.
--exclude-path=pcre PCRE to exclude paths from backup.
--exclude-file=pcre PCRE to exclude files from backup.
--exclude-fs-type=fstype Filesystem type to exclude.
--data-dir=dir Add an additional data directory.
--system-dir=dir Add an additional system directory.
--system-base=path Add a system base. This defaults to /
and this option will override that.
--tar-ignore-failed-read=path This is passed to tar only. It will
not cause errors of files that cannot
be read for the path matched.
Restore Options:
--restore Restore src to dst.
--tar-keep-newer Do not overwrite newer files.
--tar-keep-old-files Don't replace existing files.
Builtins:
---------
system-dirs:
$systemDirStr
data-dirs:
$dataDirStr
EOF
}
#
# LIBRARY
#
sub loadStateFile {
my ($statefile,$key,$fileList,$dirList,$pathAttribs) = @_;
# Read in backup state file, we do it here because we may not have a backup file
# to restore
my $gz = gzopen($statefile,"rb")
or die "FATAL ERROR: Failed to open '$statefile': $!";
# Read in file list
my $line;
while ($gz->gzreadline($line) > 0) {
chomp($line);
# Pull out array of items
my @aline = split(/\0/,$line);
my $type = shift(@aline);
# Check if its defined
if (defined($type)) {
# If its a file...
if ($type eq "f") {
my ($ename,$size,$ctime,$atime,$mtime,$uid,$gid,$mode) = @aline;
my $name = decode_base64($ename);
# Setup our hash
$fileList->{$key}->{$name}->{'size'} = $size;
$fileList->{$key}->{$name}->{'ctime'} = $ctime;
$fileList->{$key}->{$name}->{'atime'} = $atime;
$fileList->{$key}->{$name}->{'mtime'} = $mtime;
$fileList->{$key}->{$name}->{'uid'} = $uid;
$fileList->{$key}->{$name}->{'gid'} = $gid;
$fileList->{$key}->{$name}->{'mode'} = $mode;
# If its a dir
} elsif ($type eq "d") {
my ($ename,$ctime,$atime,$mtime,$uid,$gid,$mode) = @aline;
my $name = decode_base64($ename);
# Setup our hash
$dirList->{$key}->{$name}->{'ctime'} = $ctime;
$dirList->{$key}->{$name}->{'atime'} = $atime;
$dirList->{$key}->{$name}->{'mtime'} = $mtime;
$dirList->{$key}->{$name}->{'uid'} = $uid;
$dirList->{$key}->{$name}->{'gid'} = $gid;
$dirList->{$key}->{$name}->{'mode'} = $mode;
# Attribute
} elsif ($type eq "a") {
my ($name,$value) = @aline;
$pathAttribs->{$key}->{$name} = $value;
# Unknown
} else {
print(STDERR "ERROR: Invalid type '$type' in '$statefile'\n");
exit 1;
}
}
}
# Close gzip file
$gz->gzclose();
}
sub removeBackups
{
my ($path,$format,$compression,$seqs) = @_;
# Loop through backup sequences
for (my $i = 0; $i <= $seqs; $i++) {
my $tarExt;
if ($compression eq "xz") {
$tarExt = ".xz";
} elsif ($compression eq "bzip2") {
$tarExt = ".bz2";
} elsif ($compression eq "gzip") {
$tarExt = ".gz";
} elsif ($compression eq "lz") {
$tarExt = ".lz";
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
} elsif ($compression eq "none") {
$tarExt = "";
}
# Unlink backup files
if (-f "$path/dbackup$i.tar$tarExt") {
unlink("$path/dbackup$i.tar$tarExt")
or die "ERROR: Failed to remove file '$path/dbackup$i.tar$tarExt': $!";
}
# Unlink manifest
if (-f "$path/dbackup$i.manifest") {
unlink("$path/dbackup$i.manifest")
or die "ERROR: Failed to remove file '$path/dbackup$i.manifest': $!";
}
# Unlink index
if (-f "$path/dbackup$i.index") {
unlink("$path/dbackup$i.index")
or die "ERROR: Failed to remove file '$path/dbackup$i.index': $!";
}
# Unlink snar
if (-f "$path/dbackup$i.snar") {
unlink("$path/dbackup$i.snar")
or die "ERROR: Failed to remove file '$path/dbackup$i.snar': $!";
}
}
}
# Convert a possible string to an array, or return the array if it is indeed an array
sub toArray
{
my $param = shift;
if (ref $param eq "ARRAY") {
return $param;
} else {
return [ split(/\n/,$param) ];
}
}
sub getTarVer
{
my $tar = shift;
# Open tar and grab its version string
open(my $ph, "-|", $config{'tar'} . " --version")
or die "FAILED to execute '".$config{'tar'}."': $!";
print(STDERR "ERROR: Failed to read tar version\n");
exit 1;
}
close($ph);
# Convert version string into integer and return it
($tarVer) = ($tarVer =~ /([0-9]+\.[0-9]+)/);
if (!defined($tarVer) || $tarVer eq "") {
print(STDERR "ERROR: Failed to parse tar version\n");
exit 1;
}
$tarVer =~ s/\.//g;
if ($tarVer < 100) {
print(STDERR "ERROR: Failed to read tar version or your version is simply too old\n");
exit 1;
}
return $tarVer;
}
# Convert a triplet version into an integer
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");
$a[2] =~ s/[a-z]$//;
return ( ($a[0]*1000000) + ($a[1]*1000) + $a[2] );
}
# Function to check if a binary is in our PATH
sub checkPATH
{
my $binary = shift;
# Loop with PATH components
for my $path (split(/:/,$ENV{PATH})) {
# And check if the binary is executable
if (-x "$path/$binary") {
return 1;
}
}
return 0;
}