#!/usr/bin/perl
use strict;
use File::Find;
use File::Copy;
use Date::Format;
use MiscUtils; #take this out if you're just going to use `mkdir -p ` later on
my $backup_dir = '/backup/'.time2str('%Y/%m/%d', time());
my $logfile = '/backup/logfile';
my $inc = 1; # in days
my %dirs = (
'/shared' => '',
'/home' => [qw(/home/lackluster/Download /home/lackluster/Corrupt /home/lackluster/Incomplete)],
'/etc' => '',
'/var' => '',
'/usr' => '',
'/opt' => '',
'/boot' => '');
open (LOG, ">>$logfile") or die("could not open $logfile\n$!\n");
local_log("\n", time2str('%d/%m/%Y', time()), "\n------------------------------\n");
foreach my $prefix (keys %dirs) {
find(sub {
my $file = $File::Find::name;
return if (-d $file);
return if ((-M $file) > $inc); # was changed more than $inc days ago? skip it.
if (ref($dirs{$prefix}) eq 'ARRAY') {
foreach (@{$dirs{$prefix}}) {
return if ($file =~ m/^$_/);
}
}
my $sep = ((($backup_dir =~ m|/$|) || ($file =~ m|^/|)) ? '' : '/');
my $new = $backup_dir.$sep.$file;
mkdirs ($new); #use `mkdir -p $new` instead
copy ($file, $new) or local_log("$file could not be copied to $new because $!\n");
`bzip2 $new`; # bzip each file to save room
}, $prefix);
}
# backup mysql databases
`mysqldump --user=backup --all-databases >${backup_dir}/mysql.sql; cd $backup_dir; bzip2 mysql.sql 2>&1 >>${logfile}`;
sub local_log { print LOG @_; }