/[rserv]/bin/Replicate
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/Replicate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Wed Dec 20 17:22:35 2000 UTC (23 years, 4 months ago) by dpavlin
Branch: MAIN
Branch point for: DbP
Initial revision

1 # -*- perl -*-
2 # Replicate
3 # Vadim Mikheev, (c) 2000, PostgreSQL Inc.
4
5 eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
6 & eval 'exec perl -S $0 $argv:q'
7 if 0;
8
9 use lib "@LIBDIR@";
10
11 use IO::File;
12 use Getopt::Long;
13 use RServ;
14
15 $| = 1;
16
17 $result = GetOptions("debug!", "verbose!", "help", "snapshot=s",
18 "masterhost=s", "slavehost=s", "host=s",
19 "masteruser=s", "slaveuser=s", "user=s",
20 "masterpassword=s", "slavepassword=s", "password=s");
21
22 my $debug = $opt_debug || 0;
23 my $verbose = $opt_verbose || 0;
24 my $snapshot = $opt_snapshot || "__Snapshot";
25
26 if (defined($opt_help) || (scalar(@ARGV) < 2)) {
27 print "Usage: $0 --snapshot=file --host=name --user=name --password=string masterdb slavedb\n";
28 print "\t--masterhost=name --masteruser=name --masterpassword=string\n";
29 print "\t--slavehost=name --slaveuser=name --slavepassword=string\n";
30 exit ((scalar(@ARGV) < 2)? 1:0);
31 }
32
33 my $master = $ARGV[0] || "master";
34 my $slave = $ARGV[1] || "slave";
35 my $server = 0;
36
37 my $minfo = "dbname=$master";
38 $minfo = "$minfo host=$opt_masterhost" if (defined($opt_masterhost));
39 $minfo = "$minfo user=$opt_masteruser" if (defined($opt_masteruser));
40 $minfo = "$minfo password=$opt_masterpassword" if (defined($opt_masterpassword));
41 my $sinfo = "dbname=$slave";
42 $sinfo = "$sinfo host=$opt_slavehost" if (defined($opt_slavehost));
43 $sinfo = "$sinfo user=$opt_slaveuser" if (defined($opt_slaveuser));
44 $sinfo = "$sinfo password=$opt_slavepassword" if (defined($opt_slavepassword));
45
46 print "Master connection is $minfo\n" if ($debug);
47 print "Slave connection is $sinfo\n" if ($debug);
48
49 my $mconn = Pg::connectdb($minfo);
50 my $sconn = Pg::connectdb($sinfo);
51
52 SyncSync($mconn, $sconn);
53
54 my $outf = new IO::File;
55 open $outf, ">$snapshot";
56 print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);
57 $res = PrepareSnapshot($mconn, $outf, $server);
58 close $outf;
59 die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0;
60 if ($res == 0)
61 {
62 print "\n>>>>>>>>>>>>> DBases are sync-ed\n" if ($verbose);
63 exit(0);
64 }
65
66 my $inpf = new IO::File;
67 open $inpf, "<$snapshot";
68 print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);
69 $res = ApplySnapshot($sconn, $inpf);
70 close $inpf;
71 die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0;
72
73 if ($res > 0)
74 {
75 print "Snapshot applied\n" if ($verbose);
76 unlink $snapshot unless ($debug);
77 SyncSync($mconn, $sconn);
78 }
79
80 exit(0);
81
82 ###########################################################################
83
84 sub SyncSync
85 {
86 ($mconn, $sconn) = @_;
87
88 print "\n>>>>>>>>>>>>> Sync SyncID\n\n" if ($verbose);
89 print "Get last SyncID from Slave DB\n" if ($verbose);
90 $syncid = GetSyncID($sconn);
91 if ($syncid > 0)
92 {
93 print "Last SyncID applied: $syncid\n" if ($verbose);
94 print "Sync SyncID\n" if ($verbose);
95
96 $res = SyncSyncID($mconn, $server, $syncid);
97
98 print "Succeeded\n" if (($res > 0) && ($verbose));
99 }
100 }

  ViewVC Help
Powered by ViewVC 1.1.26