#!/usr/bin/env perl

use strict;
use warnings;
use Config::General;
use Net::SSH::Perl;
use Net::SCP;
use Growl::NotifySend;

my $conf = new Config::General("config");
my %config = $conf->getall;

print "Logging to $config{remote_host}...\n";
my $ssh = Net::SSH::Perl->new($config{remote_host}, debug=>0);
$ssh->login($config{remote_user}, $config{remote_pass});

print "Dumping DB...\n";
my ($stdout, $stderr, $exit) = $ssh->cmd("mysqldump -u$config{remote_db_user} -p$config{remote_db_pass} $config{remote_db_name} > $config{remote_db_name}.sql");
print $stdout;

print "Compressing DB...\n";
($stdout, $stderr, $exit) = $ssh->cmd("tar czvf $config{remote_db_name}.tar.gz $config{remote_db_name}.sql");
print $stdout;

# Download DB

print "Downloading DB...\n";
my $scp = Net::SCP->new($config{remote_host}, $config{remote_user});
$scp->get("$config{remote_db_name}.tar.gz");

print "Unpacking DB...\n";
`tar -xzvf $config{remote_db_name}.tar.gz`;

print "Loading DB...\n";
`mysql -u$config{local_db_user} -p$config{local_db_pass} $config{local_db_name} < $config{remote_db_name}.sql`;
print "Finished!\n";


Growl::NotifySend->show(
    summary => "DB Import",
    body    => "Finished",
);
