toward renaming mig to emig and tweaking the directory layout
[migration-tools.git] / emig.d / bin / mig-dump
1 #!/usr/bin/perl
2
3 ###############################################################################
4 =pod
5
6 =head1 NAME
7
8 mig-dump 
9
10 A wrapper around the pg_dump command that saves a table in the mig schema with a time stamp in the working directory.
11
12 =head1 SYNOPSIS
13
14 B<mig-dump> [arguments...]
15
16 =cut
17
18 ###############################################################################
19
20 use strict;
21 use warnings;
22
23 use Env qw(
24     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
25     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
26 );
27 use Pod::Usage;
28 use Switch;
29 use Cwd 'abs_path';
30 use Cwd qw(getcwd);
31 use FindBin;
32 my $mig_bin = "$FindBin::Bin/";
33 use lib "$FindBin::Bin/";
34 use Mig;
35 use open ':encoding(utf8)';
36
37 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
38 pod2usage(-verbose => 1) if ! $ARGV[1];
39
40 my $fh;
41 my $outfile;
42
43 my $table;
44 my $next_arg_is_table;
45
46 foreach my $arg (@ARGV) {
47     if ($arg eq '--table') {
48         $next_arg_is_table = 1;
49         next;
50     }
51     if ($next_arg_is_table) {
52         $table = $arg;
53         $next_arg_is_table = 0;
54         next;
55     }
56 }
57
58 my $outfilewpath = create_dumpfile_name($table);
59
60 my $syscmd = 'pg_dump --format plain --data-only --file ' . $outfilewpath . ' --table ' . $MIGSCHEMA . '.' . $table . ' ' . $PGUSER;
61
62 print "pgdump command: \n";
63 print "$syscmd\n";
64
65 system($syscmd);
66
67 ####### beyond here be functions 
68
69 sub create_dumpfile_name {
70     my $table_name = shift;
71         $table_name =~ s/\./_/;
72     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
73     $year += 1900;
74     my $date = $year . '-' . $mon . '-' . $mday;
75     my $dump_file = $table_name . ' ' . $date . '.pg'; 
76     $dump_file =~ s/ /_/g;
77         $dump_file = $MIGGITDIR . $dump_file;
78         print "$dump_file \n";
79     return $dump_file;
80 }
81
82 sub abort {
83     my $msg = shift;
84     print STDERR "$0: $msg", "\n";
85     exit 1;
86 }
87
88