#!/usr/bin/perl -w
#########################################################################
# vServer Backup Script (c) 2009 Robert Krause <creeq@web.de>
# Version 0.20
#########################################################################

$backupDir = '/root/backup/';
@checkDirs = ('/etc/', '/home/', '/root/', '/usr/lib/cgi-bin/', '/var/lib/', '/var/log/', '/var/mail/', '/var/spool/', '/var/www/');
@excludeDirs = ('/root/backup/');
$criteria = '-size -50M';	# backup files < 50Mb

#########################################################################
$version = "0.20";
$tmpFile = '/tmp/backup.tmp';

print "vServer Backup Script v$version (c) 2009 Robert Krause <creeq\@web.de>\n\n";

@backupFiles = ();
print "Scanning files... ";
foreach $dir (@checkDirs) {
  @find = `find $dir -type f $criteria`;
  push @backupFiles, @find;
}
print "Selected $#backupFiles files for backup.\n";

print "Filtering... ";
open(TMPFILE, ">$tmpFile");
foreach $eDir (@excludeDirs) {
  foreach $bDir (@backupFiles) {
    if (substr($bDir, 0, length($eDir)) eq $eDir) { next; }
    print TMPFILE $bDir;
  }
}
print "done.\n";

$datestring = `date +%Y%m%d-%H%M%S`;
chomp($datestring);
$serverstring = `hostname`;
chomp($serverstring);
$filename = "$serverstring-$datestring.tar.bz2";

print "Creating Backup... ";
system("tar cpfjP $backupDir$filename -T $tmpFile");
unlink($tmpFile);
print "done.\n";

print "Setting secure permissions... ";
system("chown root.root $backupDir$filename");
system("chmod 640 $backupDir$filename");
print "done.\n\n";
