forked from cvicente/Netdot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump-db.pl
More file actions
executable file
·93 lines (74 loc) · 2.84 KB
/
dump-db.pl
File metadata and controls
executable file
·93 lines (74 loc) · 2.84 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
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
#!<<Make:PERL>>
#
# Dump the Netdot database
# Useful for simple cron'd backups
#
#
use strict;
use lib "<<Make:LIB>>";
use Netdot;
use Getopt::Long;
use Data::Dumper;
my %self;
$self{dbtype} = Netdot->config->get('DB_TYPE');
$self{dbuser} = Netdot->config->get('DB_DBA');
$self{dbhost} = Netdot->config->get('DB_HOST');
$self{dbdatabase} = Netdot->config->get('DB_DATABASE');
$self{dbpass} = Netdot->config->get('DB_DBA_PASSWORD');
$self{dir} = '.';
my $USAGE = <<EOF;
usage: $0 [options]
--dbtype <type> Database Type [mysql|Pg] (default: $self{dbtype})
--dbuser <username> Database DBA user (default: $self{dbuser})
--dbpass <password> Database DBA password
--dir <path> Directory where files should be written (default: $self{dir})
--master-data Include master data in dump (MySQL)
--help Display this message
EOF
my $result = GetOptions( "dbtype=s" => \$self{dbtype},
"dbuser=s" => \$self{dbuser},
"dbpass=s" => \$self{dbpass},
"dir:s" => \$self{dir},
"master-data" => \$self{master_data},
"help=s" => \$self{help},
"debug" => \$self{debug},
);
if( ! $result || $self{help} ) {
print $USAGE;
exit 0;
}
print Dumper(%self) if $self{debug};
my $hostname = `hostname`;
chomp($hostname);
my ($seconds, $minutes, $hours, $day_of_month, $month, $year,
$wday, $yday, $isdst) = localtime;
my $date = sprintf("%04d-%02d-%02d-%02d%02d",
$year+1900, $month+1, $day_of_month, , $hours, $minutes);
my $file = $self{dir}."/$hostname-$date.sql";
## Dump the database
if ($self{dbtype} eq 'mysql'){
my @args = ('--opt', "-u$self{dbuser}", "-p$self{dbpass}");
push @args, '--master-data' if $self{master_data};
my $dump_args = join ' ', @args;
system ("mysqldump $dump_args netdot >$file");
}elsif ($self{dbtype} eq 'Pg'){
system ("export PGPASSWORD=\"$self{dbpass}\"; pg_dump -h $self{dbhost} -U $self{dbuser} -w $self{dbdatabase} >$file");
}else{
die "$self{dbtype} not yet implemented";
}
=head1 AUTHOR
Carlos Vicente, C<< <cvicente at ns.uoregon.edu> >>
=head1 COPYRIGHT & LICENSE
Copyright 2009 University of Oregon, all rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=cut