[S] Script für komplettes DB Backup per Crono

Alonso74

www.playkidz.de
ID: 218651
L
24 April 2006
1.095
29
Ich suche ein Script das mir ein komplettes DB Backup macht von meiner Datenbank und das per Crono.
Ich hab hier zwar eins aber irgendwie macht das kein komplettes Backup
woran kann das den liegen ist die Backup Datei mit 2,5 Mb zugross ?

Kann ja nicht sein oder ?

Info oder Hilfe wären Nett
 
Jop das hab ich auch laufen aber genau da gibt es das für mich nicht erklärliche Problem wenn ich manuell ein Backup machen geht es ja aber wenn ich per Cron eins machen lasse fehlt die hälfte *rofl*
 
hi

mir scheint du setzt gerne häckchen ;)

versuche mal diese einstellungen bei allgemein.

Multipart-Backup: ja, maximale Dateigröße: 50 mb oder mehr eingeben.

Backup-Format:=> Vollständige Inserts, letzten 5 häckchen raus.

bei Crondump habe ich nichts auszusetzen.


greetz Luckyze
 
Für Umgebungen mit Perl-Interpreter und einer MySql-Datenbank:

- Dieses Script speichert ein getartes Backup der gewünschten / oder auch aller Datenbanken an und kopiert diese auf Wunsch auf einen externen Server.

[/programme/mysql_backup/mysql_backup.pl]:
Code:
#!/usr/bin/perl
#
# Backup Script by Alexander Newald 2005 @ linux.newald.de
#
############################################################# ### ##  #   #
#
# Create backup of something
#i

my ($Sekunden, $Minuten, $Stunden, $Monatstag, $Monat,
    $Jahr, $Wochentag, $Jahrestag, $Sommerzeit) = localtime(time);
my $CTIME_String = localtime(time);
$Monat+=1;
$Jahrestag+=1;
$Monat = $Monat < 10 ? $Monat = "0".$Monat : $Monat;
$Monatstag = $Monatstag < 10 ? $Monatstag = "0".$Monatstag : $Monatstag;
$Stunden = $Stunden < 10 ? $Stunden = "0".$Stunden : $Stunden;
$Minuten = $Minuten < 10 ? $Minuten = "0".$Minuten : $Minuten;
$Sekunden = $Sekunden < 10 ? $Sekunden = "0".$Sekunden : $Sekunden;
$Jahr+=1900;


my $srv1host = "backup.de";
my $srv1uid  = "user";
my $srv1pwd  = "pass";
my $srv1rem  = "/backups/$Jahr/$Monat";
#
# Optional
#
my $srv2host = "";
my $srv2uid = "";
my $srv2pwd = "";
my $srv2rem = "/Backups";

my $mysqldump = "/usr/bin/mysqldump";

my $local = "/www/backup/local";

my $sets = 40;

#
# Don't change below here!
#
use Net::FTP;
my $ip = get_ip();
$system::ip = $ip;

check_dir($local);

my $path = substr($0,0,rindex($0,"/"));

my ($mode,$id) = @ARGV;

if ($mode eq "db") {
    mesg(">   Create backup of database $id");
    check_dir("$local/dbs");
    my ($userid,$pwd) = dbpwd($path,$id);
    if (!$pwd) {
        mesg(" !  Could not find pwd for $id in $path/backup.cfg");
        exit 1;
        }
    my $mode = "";
    if ($userid eq "root") { $mode = "-A"; } else { $mode = $id; }
    mesg(">   Create mysqldump of db(s)");
    system("$mysqldump --opt -Q -u $userid --password=$pwd -h $ip $mode > $local/.$$");
    my $backupfile = "$local/.$$";
    if ($mode eq "-A") { $mode = "all_dbs"; }
    rotate("$local/dbs","$mode.sql",$backupfile,$sets);
    my $zeit = date(time,1);
    ftpstore($backupfile,"$srv1rem/$ip/db/$mode/$zeit.sql",$srv1host,$srv1uid,$srv1pwd,1);
    if ($srv2host) {
        ftpstore($backupfile,"$srv2rem/$ip/db/$mode/$zeit.sql",$srv2host,$srv2uid,$srv2pwd,1);
        }
    unlink "$local/.$$";
    }






sub rotate {
    #
    # Rotate local backup
    #
    my ($path,$file,$backupfile,$sets) = @_;
    mesg(">>  Rotate $sets sets of backups in $path");
    foreach my $i (1..($sets - 1)) {
            my $old = $sets - $i;
            my $new = $sets + 1 - $i;
            if (-e "$path/$old.$file") {
                    system("mv $path/$old.$file $path/$new.$file");
                    my $date = (stat("$path/$new.$file"))[9];
                    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($date);
                    $mon += 1;
                    if (length($mon) == 1) { $mon = "0$mon"; }
                    if (length($sec) == 1) { $sec = "0$sec"; }
                    if (length($min) == 1) { $min = "0$min"; }
                    if (length($hour) == 1) { $hour = "0$hour"; }
                    if (length($mday) == 1) { $mday = "0$mday"; }
                    $year += 1900;
                    my $size = (stat("$path/$new.$file"))[7];
                    $size = zahl($size);
                    mesg("    - moving $old $new\t$mday.$mon.$year $hour:$min:$sec\t$size");
                    }
        }
    system("cp $backupfile $path/1.$file");
    mesg(">>  Copy $backupfile into local $path");
    }

sub dbpwd {
    #
    # Load pwd for db from config file
    #
    my $pwd = undef;
    my ($path,$db) = @_;
    open(FILE,"<$path/backup.cfg");
    while(<FILE>) {
        chomp($tmp = $_);
        $tmp =~ s/[\t ]{1,}/ /g;
        my ($d,$u,$p) = split(/ /,$tmp);
        if ($d eq $db) { $pwd = $p; $userid = $u; }
        }
    close(FILE);
    return ($userid,$pwd);
    }
sub check_dir {
    #
    # Check if dir exists
    #
    my $dir = $_[0];
    if (!-d $dir) {
        mesg(">>> Need to create $dir");
        my $d = undef;
        if (substr($dir,0,1) eq "/") { $dir = substr($dir,1,length($dir)-1); }
        foreach my $part (split(/\//,$dir)) {
            $d .= "/$part";
            mkdir $d;
            if (!-d $d) {
                mesg(" !  Failed to create dir $d");
                }
            }
        }
    }

sub ftpstore {
        #
        # Store a file
        #
        my ($local,$remote,$host,$userid,$pwd,$compressed) = @_;
        eval {
                mesg(">>> Transfer backup $local");
        if ($compressed) {
            mesg("    - gZip $local");
            system("gzip -9 $local");
            $local = "$local.gz";
            }
                mesg("    - connect to backup server $host as $userid");
                $ftp = Net::FTP->new($host);
                mesg("    - login to backup space");
                $ftp->login($userid,$pwd);
                $ftp->binary;
        mesg("    - create dirs");
        my $dir = substr($remote,0,rindex($remote,"/"));
        if (substr($dir,0,1) eq "/") { $dir = substr($dir,1,length($dir)-1); }
        foreach my $part (split(/\//,$dir)) {
            $d .= "/$part";
            $ftp->mkdir($d);
            }
                mesg("    - transfer backup");
                $ftp->put($local,$remote);
                mesg("    - disconnect");
                $ftp->quit();
        if ($compressed) {
            mesg("    - gUnZip $local");
            system("gunzip $local");
            }
                };
        if ($@) {
                my $error = $@;
                $error =~ s/\n//g;
                mesg(" ! Failed to store backup because of $error");
                }
        }

sub decimal {
        my $zahl = $_[0];
        $zahl =~ s/,/\./g;
        $zahl =~ s/[^0-9\.\-]{1,}//g;
        $zahl = sprintf("%.2f",$zahl);
        if ($zahl =~ /\./) {
                if (substr($zahl,length($zahl)-3,1) ne ".") {
                        $zahl .= "0";
                        }
                } else {
                $zahl .= ".00";
                }
        return $zahl;
        }
sub zahl {
        my $size = $_[0];
        my $fak = "Byte";
        if ($size > 1024) { $size = $size / 1024; $fak = "KB"; }
        if ($size > 1024) { $size = $size / 1024; $fak = "MB"; }
        if ($size > 1024) { $size = $size / 1024; $fak = "GB"; }
        return decimal($size)." $fak";
        }


sub get_ip {
    #
    # Get local ip
    #
    my $ip = undef;
    my $ifc = `/sbin/ifconfig 2>/dev/null`;
    foreach my $tmp (split(/\n/,$ifc)) {
        if ($flag == 1) {
            my $t = $tmp;
            $t =~ s/[ \t]{1,}/ /g;
            $ip = (split(/ /,$t))[2];
            $ip = (split(/:/,$ip))[1];
            $flag = 2;
            }
        if (substr($tmp,0,4) eq "eth0") {
            if (!$flag) {
                $flag = 1;
                }
            }
        }
    return $ip;
    }

sub mesg {
        my $msg = $_[0];
        my $zeit = localtime(time);
    my $tmp = " "x15;
    substr($tmp,0,length($system::ip)) = $system::ip;
        print "[$zeit @ $tmp] $msg\n";
        }

sub date
        {
        #
        # Convert unix timestamp to date
        #
        my $tmp = $_[0];
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tmp);
        $year += 1900;
        $mon +=1;
        if (length($mon) == 1) { $mon = "0$mon"; }
        if (length($sec) == 1) { $sec = "0$sec"; }
        if (length($min) == 1) { $min = "0$min"; }
        if (length($hour) == 1) { $hour = "0$hour"; }
        if (length($mday) == 1) { $mday = "0$mday"; }
        if ($_[1]) {
                return $datum = "$mday.$mon.$year"."_"."$hour:$min:$sec";
                } else {
                return $datum = "$mday.$mon.$year"."_"."$hour:$min";
                }
        }

[/programme/mysql_backup/backup.cfg]:
Code:
root root passwort

$crontab -e
0 */6 * * * perl /programme/mysql_backup/mysql_backup.pl db root > /dev/null



Läuft bei mir ganz gut ;)