Added help text.
[prj-utils.git] / prj-upload-db.pl
1 #!/usr/bin/env perl
2
3 # Uploads local database to the remote server using ssh
4
5 use strict;
6 use warnings;
7 use Config::General;
8 use Net::SSH::Perl;
9 use Net::SCP;
10 use Growl::NotifySend;
11
12 my $conf = new Config::General("config");
13 my %config = $conf->getall;
14
15 # backup local db
16 print "Dumping Local DB...\n";
17 `mysqldump -C -u$config{local_db_user} -p$config{local_db_pass} $config{local_db_name} > $config{remote_db_name}.sql`;
18
19 print "Compressing DB...\n";
20 `tar -czvf $config{remote_db_name}.tar.gz $config{remote_db_name}.sql`;
21
22 # upload db
23 print "Uploading DB...\n";
24 my $scp = Net::SCP->new($config{remote_host}, $config{remote_user});
25 $scp->put("$config{remote_db_name}.tar.gz");
26
27 print "Logging to $config{remote_host}...\n";
28 my $ssh = Net::SSH::Perl->new($config{remote_host}, debug=>0);
29 $ssh->login($config{remote_user}, $config{remote_pass});
30
31 print "Unpacking DB...\n";
32 my ($stdout, $stderr, $exit) = $ssh->cmd("tar -xzvf $config{remote_db_name}.tar.gz");
33 print $stdout;
34
35 print "Loading DB...\n";
36 ($stdout, $stderr, $exit) = $ssh->cmd("mysql -u$config{remote_db_user} -p$config{remote_db_pass} $config{remote_db_name} < $config{remote_db_name}.sql");
37
38 print "$stdout\n";
39 print "$stderr\n";
40 print "Finished!\n";
41
42 Growl::NotifySend->show(
43     summary => "DB Upload",
44     body    => "Finished",
45 );
46 #if [ $? -eq 255 ]; then
47 #  notify-send  "Aktualizacja bazy danych nieudana" \
48 #      "Skrypt aktualizacyjny bazę na serwerze zdalnym\nzakończył swoją pracę z błędem."
49 #  exit
50 #fi
51 #
52 #notify-send "Powodzenie wgrywania bazy danych" "Skrypt aktualizacyjny bazę na serwerze zdalnym\nzakończył swoją pracę z sukcesem."