-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher_DBpull.pl
executable file
·206 lines (166 loc) · 4.89 KB
/
watcher_DBpull.pl
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Switch;
use lib '/usr/local/bin/opsadmin/perl/';
use DBI;
use Data::Dumper;
use Getopt::Long qw(:config no_ignore_case bundling);
use XML::Simple;
use XML::XPath;
require "ctime.pl";
our $AUTHOR = 'John_Vossler@McAfee.com';
our $VERSION = 1.1;
our $CONFIG = '/home/watcher/watcher.xml';
our %flags = ();
$ENV{'PGPASSWORD'} = 'dbG0d'; # Export the postgres password
sub get_opts();
sub usage();
&get_opts();
if($flags{'debug'})
{
print "\nflags\n";
print Dumper(\%flags);
}
##
## Constants
##
##
## variables
##
my $xp = ();
my $record_list = ();
my $record = ();
my $ip = ();
my $table = ();
my $dest = ();
my $site = ();
my $index = ();
my $nindex = ();
my $timestamp = ();
my $dom = ();
my $dow = ();
my $type = ();
##
## Make backup of entire DB
##
$dom = `/bin/date +%m`;
chomp($dom);
$dow = `/bin/date +%w`;
chomp($dow);
if ( $dom == 1 )
{
$type = "Monthly";
}
else
{
if ( $dow == 6 )
{
$type = "Weekly";
}
else
{
$type = "Daily";
}
}
if ( $flags{'verbose'} )
{
print "\n/usr/bin/pg_dump -U postgres watcher -f /raid/data/backups/watcher-$type.sql\n";
}
system("/usr/bin/pg_dump -U postgres watcher -f /raid/data/backups/watcher-$type.sql");
##
## Start the DB pull
##
#
# Get config information for all arrays
#
$xp = XML::XPath->new(filename => $CONFIG) or die "FATAL ERROR: Can't read $CONFIG as XML:\n";
if($flags{'debug'})
{
print "\nxp value\n";
print Dumper $xp;
}
$record_list = $xp->find("/watcher/DBpull/table");
foreach $record ($record_list->get_nodelist)
{
$site=$record->getAttribute("site");
$ip=$record->getAttribute("ip");
$table=$record->getAttribute("table");
if(($flags{'debug'}) || ($flags{'verbose'}))
{
print "\nsite mnemonic\n";
print Dumper $site;
print "\nDB source routable ip\n";
print Dumper $ip;
print "\nDB table name\n";
print Dumper $table;
}
$dest = "$site" . "_" . "$table";
if ( $flags{'verbose'} )
{
print "\ndestination table name\n";
print Dumper $dest;
}
if ( $flags{'debug'} )
{
print "\n drop table if exists $table\n";
print "\nssh -n $ip \"/usr/bin/pg_dump -Fc -C -U postgres -t \"$table\" watcher\" | /usr/bin/pg_restore -h localhost -U postgres -d watcher\n";
print "\ndrop table if exists $dest\n";
print "\nalter table $table rename to $dest\n";
}
else
{
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"drop table if exists $table\"") == 0 or die "FATAL ERROR: Could not drop table $table if exists :\n";
system("ssh -n $ip \"/usr/bin/pg_dump -Fc -C -U postgres -t \"$table\" watcher\" | /usr/bin/pg_restore -h localhost -U postgres -d watcher") == 0 or die "FATAL ERROR: Could not perform pg_dump for $table on $ip :\n";
$index = `/usr/bin/psql -At -h localhost -U postgres watcher -c "select indexname from pg_catalog.pg_indexes where tablename='$table'"`;
$nindex = "$site" . "_" . "$index";
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"drop table if exists $dest\"") == 0 or die "FATAL ERROR: Could not drop table $dest if exists \n";
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"alter table $table rename to $dest\"") == 0 or die "FATAL ERROR: Could not rename $table to $dest \n";
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"alter index $index rename to $nindex\"") == 0 or die "FATAL ERROR: Could not rename index $index to $nindex \n";
}
if ( $flags{'verbose'} )
{
print "\nindex name\n";
print Dumper $index;
print "\nnindex name\n";
print Dumper $nindex;
}
$timestamp = time();
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"insert into sync_time (epochtime,site,source_ip,source_table) values (to_timestamp($timestamp),'$site','$ip','$table')\"") == 0 or die "FATAL ERROR: Could not insert into sync_time for $site $ip $table :\n";
} # End of record loop
##
## End main - Begin subroutines
##
#
# Get all command line options into flags hash
#
sub get_opts()
{
use Getopt::Long qw(:config no_ignore_case bundling);
Getopt::Long::Configure("bundling");
GetOptions(
'debug|d' => \$flags{'debug'},
'verbose|v' => \$flags{'verbose'},
'graph|g' => \$flags{'graph'},
'sync|s' => \$flags{'sync'},
'help|usage|h' => sub {warn &usage; exit 1;})
or die &usage;
defined($flags{'debug'}) || ($flags{'debug'} = 0);
defined($flags{'verbose'}) || ($flags{'verbose'} = 0);
}
# Subroutine: usage
# Args: <void>
# Return Value: <void>
# Purpose: Write the appropriate usage to STDOUT.
sub usage()
{
my $usage = <<EOF;
Usage: $0 [OPTIONS]
-v, --verbose Verbose Mode
-d, --debug Debug Mode (No DB pull)
-g, --graph Only print out graphs
-h, --help Print this help
EOF
print $usage;
}