export_dbs Database Export Script

#!/usr/bin/perl

# --------------------------------------------------------------
# export-dbs
# Copyright 2009-2013 ]project-open[
# Frank Bergmann <frank.bergmann@project-open.com>
# Licensed under GPL V2.0 or higher
# --------------------------------------------------------------

# Constants, variables and parameters
#
my $debug =     1;
my $psql =      "/usr/bin/psql";
my $pg_dump =   "/usr/bin/pg_dump";
my $bzip2 =     "/usr/bin/bzip2";

my $exportdir = "/var/backup";
my $logdir =    "/var/log/backup";

my $pg_owner =  "postgres";
my $computer_name = `hostname`;

my $time =      `/bin/date +\%Y\%m\%d.\%H\%M`;
my $weekday =   `/bin/date +%w`;

chomp($computer_name);
chomp($time);
chomp($weekday);

open(DBS, "su - $pg_owner -c '$psql -l' |");
while (my $db_line=<DBS>) {

        chomp($db_line);
        $db_line =~ /^\s*(\w*)/;
        my $db_name = $1;

        next if (length($db_name) < 2);
        next if ($db_name =~ /^\s$/);
        next if ($db_name =~ /^List$/);
        next if ($db_name =~ /^Name$/);

        next if ($db_name =~ /^postgres$/);
        next if ($db_name =~ /^template0$/);
        next if ($db_name =~ /^template1$/);

        my $file = "$exportdir/pgback.$computer_name.$db_name.$time.sql";
        my $log_file = "$logdir/export-dbs.$db_name.$time.log";
        my $cmd = "su - $pg_owner --command='$pg_dump $db_name -c -O -F p -f $file'";
        print "export-dbs: $cmd\n" if ($debug);
        system $cmd;

        my $cmd2 = "su - $pg_owner --command='$bzip2 $file'";
        print "export-dbs: $cmd2\n" if ($debug);
        system $cmd2;

        # Tar the entire web server to backup area, except for packages and filestorage backup.
        my $file9 = "$exportdir/webback.$computer_name.$db_name.$time.tgz";
        my $cmd9 = "tar --exclude='/web/$db_name/log' --exclude='/web/$db_name/filestorage/backup' -c -z -f $file9 /web/$db_name/";
        print "export-dbs: $cmd9\n" if ($debug);
        system $cmd9;
}
close(DBS);


  Contact Us
  Project Open Business Solutions S.L.

Calle Aprestadora 19, 12o-2a

E-08907 Hospitalet de Llobregat (Barcelona)

 Tel Europe: +34 932 202 088
 Tel US: +1 415 429 5995
 Mail: info@project-open.com