Dit is de Perl functie die ik daarvoor toevallig net in elkaar heb gezet. Hij dumpt een mysql database, checkt of de dump toevallig identiek is aan een eerdere (mbv md5sum) . Zo ja dan wist hij hem weer. Draait in een cron.hourly script onder Linux.
Hier hoort ook nog een cron.daily script bij dat alle dumps naar een tgz file schrijft.
Daarnaast draaien er een aantal rsync processen die mirrors op de secundaire schijf en op een remote server up to date houden. Op de remote server worden ook nog snapshots bijgehouden die een paar dagen/weken/maanden in het verleden teruggaan.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| sub dumpdata {
my($path,$hour,$db,$table)=@_;
system("/usr/bin/mysqldump -u root $db ".($table ? $table : '')." > $path/$db$hour.dmp");
my($md5)=`/usr/bin/md5sum $path/$db$hour.dmp` =~ /(\w+)/;
print "Saving $path/$db$hour.dmp ($md5)\n";
# Check if this is a duplicate by trying all the other hourly dumps
my $dup=0;
for my $i ('00'..'24') {
my($thismd5)=`tail -n 1 $path/$db$i.dmp 2> /dev/null` =~ /-- (\w+)/;
if ($thismd5) {
$dup=1 if $md5 eq $thismd5;
print "$i MD5=$thismd5 (DUP=$dup)\n";
}
}
# Delete if it's a dup, save md5 checksum if it isn't
if ($dup) {
print "Deleting $path/$db$hour.dmp\n";
unlink("$path/$db$hour.dmp");
} else {
print "Saving md5\n";
system("echo \"-- $md5\" >> $path/$db$hour.dmp");
}
} |