Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 68 lines (47 sloc) 1.63 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#!/usr/bin/perl -w

=head1 Name

clone_sql_Pg.pl - installation script to clone an existing PG database

=head1 Description

This script is called during "make clone" to clone the Bricolage
database.

=head1 Author

Sam Tregar <stregar@about-inc.com>

=head1 See Also

L<Bric::Admin>

=cut


use FindBin;
use File::Spec::Functions qw(:ALL);

print "\n\n==> Cloning Bricolage Database <==\n\n";

our $DB;
do "./database.db" or die "Failed to read database.db: $!";

# Make sure that we don't overwrite the existing Pg.sql.
chdir 'dist';
my $file = 'inst/Pg.sql';

# Switch to postgres system user
if (my $sys_user = $DB->{system_user}) {
    print "Becoming $sys_user...\n";

    # Make sure that the user can write out inst/Pg.sql.
    my $to_chown = -e 'inst/Pg.sql' ? 'inst/Pg.sql' : 'inst';
    chown $DB->{system_user_uid}, -1, $to_chown
        or die "Cannot chown $to_chown to $DB->{system_user_uid} ($sys_user).\n";

    # Become the user.
    require Config;
    $> = $DB->{system_user_uid};
    $< = $DB->{system_user_uid} if $Config::Config{d_setruid};
    die "Failed to switch EUID to $DB->{system_user_uid} ($sys_user).\n"
        unless $> == $DB->{system_user_uid};
}

$ENV{PGHOST} = $DB->{host_name} if $DB->{host_name};
$ENV{PGPORT} = $DB->{host_port} if $DB->{host_port};

# dump out postgres database
my @pgdump = (
    catfile($DB->{bin_dir}, 'pg_dump'),
    '-U', $DB->{root_user},
    '-f', $file,
    '-O',
    '-x',
    $DB->{db_name},
);

# dump out postgres database
system( @pgdump ) and die 'Error executing `' . join(' ', @pgdump), "`\n";
print "\n\n==> Finished cloning Bricolage Database <==\n\n";
Something went wrong with that request. Please try again.