From: Rogan Hamby Date: Tue, 9 Apr 2019 20:14:17 +0000 (-0400) Subject: early version of a mig pg_dump, untested so far X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=commitdiff_plain;h=4f2c9d1c1f7be5eb3c1bb25787cb7b8597d6ded2 early version of a mig pg_dump, untested so far --- diff --git a/mig-bin/mig-dump b/mig-bin/mig-dump new file mode 100755 index 0000000..57edeab --- /dev/null +++ b/mig-bin/mig-dump @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +############################################################################### +=pod + +=head1 NAME + +mig-dump + +A wrapper around the pg_dump command that saves a table in the mig schema with a time stamp in the working directory. + +=head1 SYNOPSIS + +B [arguments...] + +=cut + +############################################################################### + +use strict; +use warnings; + +use Env qw( + HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA + MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR +); +use Pod::Usage; +use Switch; +use Cwd 'abs_path'; +use Cwd qw(getcwd); +use FindBin; +my $mig_bin = "$FindBin::Bin/"; +use lib "$FindBin::Bin/"; +use Mig; +use open ':encoding(utf8)'; + +pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help'; +pod2usage(-verbose => 1) if ! $ARGV[1]; + +my $fh; +my $outfile; + +my $table; +my $next_arg_is_table; + +foreach my $arg (@ARGV) { + if ($arg eq '--table') { + $next_arg_is_table = 1; + next; + } + if ($next_arg_is_table) { + $table = $arg; + $next_arg_is_table = 0; + next; + } +} + +my $outfilewpath = create_dumpfile_name($table); + +my $syscmd = 'pg_dump --format plain --data-only --file ' . $outfilewpath . ' --table ' . $MIGSCHEMA . '.' . $table . ' ' . $PGUSER; + +print "pgdump command: \n"; +print "$syscmd\n"; + +system($syscmd); + +####### beyond here be functions + +sub create_dumpfile_name { + my $table_name = shift; + $table_name =~ s/\./_/; + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); + $year += 1900; + my $date = $year . '-' . $mon . '-' . $mday; + my $dump_file = $table_name . ' ' . $date . '.pg'; + $dump_file =~ s/ /_/g; + $dump_file = $MIGGITDIR . $dump_file; + print "$dump_file \n"; + return $dump_file; +} + +sub abort { + my $msg = shift; + print STDERR "$0: $msg", "\n"; + exit 1; +} + +