Fixed too long password for mysql. Fixed no enable-drupal target. Fixed no email...
[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") or die $scp->{errstr};
26
27 print "Logging to $config{remote_host}...\n";
28 my %params;
29 $params{port} = $config{remote_port};
30 $params{debug} = 0;
31 $params{use_pty} = 1;
32 my $ssh = Net::SSH::Perl->new($config{remote_host}, %params);
33 $ssh->login($config{remote_user}, $config{remote_pass});
34
35 print "Unpacking DB...\n";
36 my ($stdout, $stderr, $exit) = $ssh->cmd("tar -xzvf $config{remote_db_name}.tar.gz");
37 print $stdout;
38 print $stderr;
39
40
41 print "Loading DB...\n";
42 ($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");
43
44 print "$stdout\n";
45 print "$stderr\n";
46 print "Finished!\n";
47
48 Growl::NotifySend->show(
49     summary => "DB Upload",
50     body    => "Finished",
51 );
52 #if [ $? -eq 255 ]; then
53 #  notify-send  "Aktualizacja bazy danych nieudana" \
54 #      "Skrypt aktualizacyjny bazę na serwerze zdalnym\nzakończył swoją pracę z błędem."
55 #  exit
56 #fi
57 #
58 #notify-send "Powodzenie wgrywania bazy danych" "Skrypt aktualizacyjny bazę na serwerze zdalnym\nzakończył swoją pracę z sukcesem."