-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_backup.pl
executable file
·98 lines (85 loc) · 2.64 KB
/
pg_backup.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
#! /usr/bin/perl -w
# Load additional library
use strict;
use Getopt::Std;
use pg_notify;
use pg_read;
use pg_write;
our $VERSION = 0.9;
my @notify_rcpt = qw([email protected]);
my $notifier = pg_notify->new();
&run();
sub run {
if(grep(/-v/,@ARGV)){
$notifier->verbosity('DEBUG');
$notifier->notify('VERBOSE');
}
$notifier->notify('START');
my $params = &get_params();
$notifier->params($params);
if($params->{'h'}){&HELP_MESSAGE; $notifier->notify('END'); exit(0);}
my $reader = pg_read->new($notifier,$params);
my $writer = pg_write->new($notifier,$params);
$SIG{INT} = sub {
$notifier->notify('INT');
$reader->end();
$writer->end();
$notifier->report()
};
$reader->buffer($writer->buffer());
$reader->start();
$writer->start();
while($writer->state() ne 'END'){
sleep(1);
}
if($notifier->errorlevel == 1){
$notifier->notify('WARNING');
rename($writer->dumpfile(),$writer->dumpfile()."-WARNING");
$writer->dumpfile($writer->dumpfile()."-WARNING");
}
elsif($notifier->errorlevel == 0){
$notifier->notify('ERROR');
rename($writer->dumpfile(),$writer->dumpfile()."-ERROR");
$writer->dumpfile($writer->dumpfile()."-ERROR");
}else{
$writer->rotate();
}
$notifier->notify('END');
exit(0);
}
sub get_params {
$notifier->notify('GETOPTS');
my %opts;
getopts('qvhdif:D:kx:', \%opts);
if($ARGV[$#ARGV]){$opts{'ARGV'} = pop(@ARGV)}
if($ARGV[$#ARGV]){
&HELP_MESSAGE;
exit(1);
}
my $argstring;
foreach my $arg (keys(%opts)){
$argstring .= "$arg => $opts{$arg}";
}
$notifier->notify('ARGV',$argstring);
return(\%opts);
}
sub HELP_MESSAGE {
&debug('HELP');
$Getopt::Std::STANDARD_HELP_VERSION = 1;
my $fh = shift;
if(! $fh){$fh = *STDERR}
print $fh ("Usage : $0 [OPTIONS] [PATH]\n");
print $fh ("Create a full dump of a postgres database,\n");
print $fh ("handling dump rotation for long time backup purpose\n");
print $fh (" -q\t\tQuiet mode\n");
print $fh (" -h\t\tThis help\n");
print $fh (" -v\t\tVerbose mode\n");
print $fh (" -d\t\tDry run. Don't modify or write anything\n");
# print $fh (" -i\t\tForce incremental backup\n");
print $fh (" -f\t\tForce full backup\n");
print $fh (" -D base\tDatabase to backup (else all)\n");
print $fh (" -k\t\tForce to keep previous backup, even if they are expired\n");
print $fh (" -F fmt\tBackup file format (plain,custom,tar)\n");
print $fh (" -x pgparams\tSpecify pg_dump specific arguments. See man pg_dump\n");
print $fh ("Report bugs to <git\@nimporteou.net>\n");
}