| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::DbDriver::Backup - backup writes to a db |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::DbDriver::Backup; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Log qw{:all}; |
| 13 |
use base 'Debconf::DbDriver::Copy'; |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
This driver passes all reads and writes on to another database. But |
| 18 |
copies of all writes are sent to a second database, too. |
| 19 |
|
| 20 |
=cut |
| 21 |
|
| 22 |
=head1 FIELDS |
| 23 |
|
| 24 |
=over 4 |
| 25 |
|
| 26 |
=item db |
| 27 |
|
| 28 |
The database to pass reads and writes to. |
| 29 |
|
| 30 |
In the config file, the name of the database can be used. |
| 31 |
|
| 32 |
=item backupdb |
| 33 |
|
| 34 |
The database to write the backup to. |
| 35 |
|
| 36 |
In the config file, the name of the database can be used. |
| 37 |
|
| 38 |
=back |
| 39 |
|
| 40 |
=cut |
| 41 |
|
| 42 |
use fields qw(db backupdb); |
| 43 |
|
| 44 |
=head1 METHODS |
| 45 |
|
| 46 |
=head2 init |
| 47 |
|
| 48 |
On initialization, convert db names to drivers. |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
sub init { |
| 53 |
my $this=shift; |
| 54 |
|
| 55 |
# Handle values from config file. |
| 56 |
foreach my $f (qw(db backupdb)) { |
| 57 |
if (! ref $this->{$f}) { |
| 58 |
my $db=$this->driver($this->{$f}); |
| 59 |
unless (defined $f) { |
| 60 |
$this->error("could not find a db named \"$this->{$f}\""); |
| 61 |
} |
| 62 |
$this->{$f}=$db; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
=head2 copy(item) |
| 68 |
|
| 69 |
Ensures that the given item is backed up by doing a full copy of it into |
| 70 |
the backup database. |
| 71 |
|
| 72 |
=cut |
| 73 |
|
| 74 |
sub copy { |
| 75 |
my $this=shift; |
| 76 |
my $item=shift; |
| 77 |
|
| 78 |
$this->SUPER::copy($item, $this->{db}, $this->{backupdb}); |
| 79 |
} |
| 80 |
|
| 81 |
=item shutdown |
| 82 |
|
| 83 |
Saves both databases. |
| 84 |
|
| 85 |
=cut |
| 86 |
|
| 87 |
sub shutdown { |
| 88 |
my $this=shift; |
| 89 |
|
| 90 |
$this->{backupdb}->shutdown(@_); |
| 91 |
$this->{db}->shutdown(@_); |
| 92 |
} |
| 93 |
|
| 94 |
# From here on out, the methods are of two types, as explained in |
| 95 |
# the description above. Either it's a read, which goes to the db, |
| 96 |
# or it's a write, which goes to the db, and, if that write succeeds, |
| 97 |
# goes to the backup as well. |
| 98 |
sub _query { |
| 99 |
my $this=shift; |
| 100 |
my $command=shift; |
| 101 |
shift; # this again |
| 102 |
|
| 103 |
return $this->{db}->$command(@_); |
| 104 |
} |
| 105 |
|
| 106 |
sub _change { |
| 107 |
my $this=shift; |
| 108 |
my $command=shift; |
| 109 |
shift; # this again |
| 110 |
|
| 111 |
my $ret=$this->{db}->$command(@_); |
| 112 |
if (defined $ret) { |
| 113 |
$this->{backupdb}->$command(@_); |
| 114 |
} |
| 115 |
return $ret; |
| 116 |
} |
| 117 |
|
| 118 |
sub iterator { $_[0]->_query('iterator', @_) } |
| 119 |
sub exists { $_[0]->_query('exists', @_) } |
| 120 |
sub addowner { $_[0]->_change('addowner', @_) } |
| 121 |
sub removeowner { $_[0]->_change('removeowner', @_) } |
| 122 |
sub owners { $_[0]->_query('owners', @_) } |
| 123 |
sub getfield { $_[0]->_query('getfield', @_) } |
| 124 |
sub setfield { $_[0]->_change('setfield', @_) } |
| 125 |
sub fields { $_[0]->_query('fields', @_) } |
| 126 |
sub getflag { $_[0]->_query('getflag', @_) } |
| 127 |
sub setflag { $_[0]->_change('setflag', @_) } |
| 128 |
sub flags { $_[0]->_query('flags', @_) } |
| 129 |
sub getvariable { $_[0]->_query('getvariable', @_) } |
| 130 |
sub setvariable { $_[0]->_change('setvariable', @_) } |
| 131 |
sub variables { $_[0]->_query('variables', @_) } |
| 132 |
|
| 133 |
=head1 AUTHOR |
| 134 |
|
| 135 |
Joey Hess <[email protected]> |
| 136 |
|
| 137 |
=cut |
| 138 |
|
| 139 |
1 |