early version of a mig pg_dump, untested so far
authorRogan Hamby <rhamby@esilibrary.com>
Tue, 9 Apr 2019 20:14:17 +0000 (16:14 -0400)
committerRogan Hamby <rhamby@esilibrary.com>
Tue, 9 Apr 2019 20:14:17 +0000 (16:14 -0400)
mig-bin/mig-dump [new file with mode: 0755]

diff --git a/mig-bin/mig-dump b/mig-bin/mig-dump
new file mode 100755 (executable)
index 0000000..57edeab
--- /dev/null
@@ -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<mig-dump> [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;
+}
+
+