#!/usr/bin/perl -w use DBI; my ( $user ) = "user"; my ( $password ) = "password"; my ( $pg_dump_options ) = "-d -O -R"; my ( $path ) = $ARGV[0]; if( !$path || $path eq '' ) { $path = '.'; } chdir( $path ) or die "Can't cd $path: " . $!; my $dbh = DBI->connect( "DBI:Pg:dbname=template1", $user, $password ) || die "Can't connect to the database: " . DBI->errstr; my $sth = $dbh->prepare( "SELECT datname FROM pg_database" ) || die "Can't prepare the query" . $dbh->errstr; $sth->execute || die "Can't execute the query" . $sth->errstr; my ( @data, @databases ); my $count = 0; while( @data = $sth->fetchrow_array() ) { if( !( $data[0] =~ m/template[0,1]/ ) ) { $databases[$count++] = $data[0]; } } $sth->finish; $dbh->disconnect; foreach( @databases ) { my $db = $_; for( 1 .. 6 ) { if( -e "$db.backup." . (7-$_) ) { rename( "$db.backup." . (7-$_), "$db.backup." . (7-$_+1) ); } } if( -e "$db.backup" ) { rename( "$db.backup", "$db.backup.1" ); } system( "export PGUSER=\"$user\"; export PGPASSWORD=\"$password\"; pg_dump $pg_dump_options $_ | gzip > $_.backup" ); }