#!/usr/bin/perl
use strict;
use DBI;
use lib '/opt/eig_linux/lib';
use BackupRestore;
use Sys::Hostname;
use JSON::XS 'decode_json';

my $DEBUG = 0;
my $MYSQLDIR = "/var/lib/mysql";
my $mustdump = 0;
my $dblastupdated = 0;
my @safetouseolddumps = qw{ MYD frm MYI opt CSV };
my $bestfilesofar = "";
my $loglocation = '/var/log/eig_backup/mysqldumper.log';
my $large_db = 0;
my $br = new BackupRestore();
my $dump_large_dbs = is_weekend();
my $hostname = hostname;

unless ($ARGV[0]) {
    print "\n\tUsage = $0 <db> <file> <mysqldump-options>\n\tExample: $0 username_blah /backup3/cpbackup/daily/username/mysql/username_blah.sql\n\n";
    exit;
}

$0 = "mysqldumper";

my $db = shift(@ARGV);
my $file = shift(@ARGV);
print "\n\nDB: $db\nFILE: $file\n" if $DEBUG;
print "OPTIONS: ".join(" ", @ARGV)."\n" if $DEBUG;

# Check that the database in question is ok to decide upon due to timestamps
opendir(DIR, "$MYSQLDIR/$db") or logger("Unable to find database folder, dumping...") and exit 1;
my @entries = readdir(DIR);
close(DIR);
check_crashed_tables();
FILECHECK: foreach my $file (@entries){
    next if $file =~ m/^\./;
    my @attrib = lstat("$MYSQLDIR/$db/$file");
    $dblastupdated = $attrib[9] if $attrib[9] > $dblastupdated;
    my $extension = substr($file,(length($file) - 3), 3);
    foreach(@safetouseolddumps){
        next FILECHECK if $extension eq $_;
    }
    print "non-safe file fourd, we must dump: $file\n" if $DEBUG;
    $mustdump = 1;
    last;
}

# Check sizes and set large db variables
if ($br->{config}->{'max_user_mysql_table_count'}){
    my $tablecount = getdbtablecount($db);
    if ($tablecount >= $br->{config}->{'max_user_mysql_table_count'}){
        print "large database detected ($tablecount tables), we are skipping dump unless it is sat night backups!\n" if $DEBUG;
        $large_db = 1;
        $mustdump = 0 unless $dump_large_dbs;
    }
}
if (!$large_db && $br->{config}->{'max_user_mysql_size'}){
    my $size = getdbsize($db);
    if ($size >= $br->{config}->{'max_user_mysql_size'}){
        print "large database detected ($size bytes), we are skipping dump unless it is sat night backups!\n" if $DEBUG;
        $large_db = 1;
        $mustdump = 0 unless $dump_large_dbs;
    }
}

# See if there is a backup we can use instead
unless($mustdump){
    print "db last updated: ".localtime($dblastupdated)."\n" if $DEBUG;
    my ($username,undef) = split('_', $db);
    # Use cached dates if possible
    my $cache_file = "/opt/eig_linux/data/backups/${username}_mysql_dates.json";
    my $dates;
    if ( -f "$cache_file" && ((stat "$cache_file")[9]) >= time - (60*60*18)){
        print "Loading backup dates from cache\n" if $DEBUG;
        $dates = decode_json(slurp($cache_file));
    }else{
        my $data = $br->backupserver_run("/opt/eig_linux/bin/get_mysql_dates --server $hostname --user $username");
        if ($data->{ssh_error} || $data->{exit_code}){
            print "Error getting dates: $data->{ssh_error} $data->{exit_code}, DUMPING\n";
            exit 1;
        }
        $dates = decode_json($data->{stdout});
        open (my $fh, ">", $cache_file);
        print $fh ($data->{stdout});
        close $fh;
    }

    # Check the available backups for the best one
    $dblastupdated = 0 if ($large_db and !$dump_large_dbs);
    if (defined $dates->{$db}) {
    foreach my $path (keys %{$dates->{$db}}) {
        if ($dates->{$db}->{$path} > $dblastupdated ){
            $bestfilesofar = $path;
            $dblastupdated = $dates->{$db}->{$path};
            }
        }
    }
}

if ($bestfilesofar eq ""){
    logger("No backup found for large db, dumping...") if ($large_db && $dump_large_dbs);
    print "==== WE ARE DUMPING! ====\n" if $DEBUG;
    exit 1;
}else{
    print "best file found: $bestfilesofar\n" if $DEBUG;
    my $cmd = "scp -i /admin/hgbackupdir/backupkey -F /admin/hgbackupdir/ssh_config root\@$br->{config}->{backup_ip}:$bestfilesofar $file";
    print "Running '$cmd'\n" if $DEBUG;
    system $cmd
}



# Subs below this point

sub getdbsize {
    my $db = shift;
    my $dsn =
            "DBI:mysql:;" .
            "mysql_read_default_file=/root/.my.cnf";
    my $dbh = DBI->connect(
            $dsn,
            undef,
            undef,
            {RaiseError => 0}
            ) or return 0;
    my $query =q{SELECT SUM(data_length + index_length) 'SIZE' FROM information_schema.TABLES WHERE table_schema=?};
    return eval { ($dbh->selectrow_array($query, {}, $db))[0]; } || 0;
}

sub getdbtablecount {
    my $db = shift;
    my $dsn =
            "DBI:mysql:;" .
            "mysql_read_default_file=/root/.my.cnf";
            my $dbh = DBI->connect(
            $dsn,
            undef,
            undef,
            {RaiseError => 0}
            ) or return 0;
    my $query =q{SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND table_schema=?};
    return eval { ($dbh->selectrow_array($query, {}, $db))[0]; } || 0;
}

# Returns if it is the weekend, so we can do long running operations
sub is_weekend {
    my $is_weekend = 0;
    my (undef,undef,$hour,undef,undef,undef,$wday) = localtime(time);
    $is_weekend = 1 if $wday == 6 and $hour >= 20;
    $is_weekend = 1 if $wday == 0 and $hour <= 6;
    return $is_weekend;
}

# Checks if there are any crashed tables in need of repair
sub check_crashed_tables {
    my $dsn =
            "DBI:mysql:;" .
            "mysql_read_default_file=/root/.my.cnf";
    my $dbh = DBI->connect(
            $dsn,
            undef,
            undef,
            {RaiseError => 0}
            ) or return 0;
    my $query =q{SELECT * FROM information_schema.tables WHERE engine IS NULL AND table_comment LIKE '%is marked as crashed%' AND table_schema=?};
    my $res = $dbh->selectall_arrayref($query, {}, $db);
    foreach my $row (@$res){
        my $table = (@$row)[2];
        my $rawsize;
        foreach my $type (qw( MYD MYI frm )){
            $rawsize += -s "/var/lib/mysql/$db/$table.$type" if (-f "/var/lib/mysql/$db/$table.$type");
        }
        my $size = sprintf "%.02f", ($rawsize/1024/1024);
        if ($size >= 2000){ #Skipping if we are over 2GB
            logger("Skipping due to crashed table $table with size $size MB");
            exit 0;
        }else{
            logger("Found crashed table $table with size $size MB");
        }
    }
}

# Logging Sub
sub logger {
    my $message = shift;
    print "$message\n" if $DEBUG;
    my $entry = localtime(time()) .  ":$db: $message\n";
    open(LOG, ">>$loglocation");
    print LOG $entry;
    close(LOG);
}

sub slurp {local$/=<>if local@ARGV=@_}
