Debian Codegraph @

debconf-copydb

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 debconf-copydb - copy a debconf database
6
7 =cut
8
9 use warnings;
10 use strict;
11 use Getopt::Long;
12 use Debconf::Log qw{:all};
13 use Debconf::Db;
14 use Debconf::DbDriver;
15 use Debconf::DbDriver::Backup;
16
17 =head1 SYNOPSIS
18
19 debconf-copydb sourcedb destdb [--pattern=pattern] [--owner-pattern=pattern] [--config=Foo:bar]
20
21 =cut
22
23 sub usage {
24 print STDERR <<EOF;
25 Usage: debconf-copydb sourcedb destdb [--pattern=pattern] [--owner-pattern=pattern] [--config=Foo:bar]
26 EOF
27 exit(1);
28 }
29
30 =head1 DESCRIPTION
31
32 B<debconf-copydb> copies items from an existing debconf database into
33 another, possibly new database. The two databases may have different
34 formats; if so a conversion will automatically be done.
35
36 =head1 OPTIONS
37
38 =over 4
39
40 =item I<sourcedb>
41
42 The name of the source database. Typically it will be defined
43 in your debconf.conf (or .debconfrc) file.
44
45 =item I<destdb>
46
47 The name of the destination database. It may be defined in
48 debconf.conf or .debconfrc, or you might define it on the command line (see
49 below).
50
51 =item B<-p> I<pattern>, B<--pattern> I<pattern>
52
53 If this is specified, only items in I<sourcedb> whose names match the pattern
54 will be copied.
55
56 =item B<--owner-pattern> I<pattern>
57
58 If this is specified, only items in I<sourcedb> whose owners match the pattern
59 will be copied.
60
61 =item B<-c> I<foo:bar>, B<--config> I<Foo:bar>
62
63 Set option Foo to bar. This is similar to writing:
64
65 Foo: bar
66
67 In debconf.conf, except you probably want to leave off the space on the
68 command line (or quote it: "Foo: bar"). Generally must be used multiple
69 times, to build up a full configuration stanza. While blank lines are used
70 to separate stanzas in debconf.conf, this program will assume that
71 "Name:dbname" denotes the beginning of a new stanza.
72
73 =back
74
75 =head1 EXAMPLES
76
77 debconf-copydb configdb backup
78
79 Copy all of configdb to backup, assuming you already have the backup
80 database defined in debconf.conf.
81
82 debconf-copydb configdb newdb --pattern='^slrn/' \
83 --config=Name:newdb --config=Driver:File \
84 --config=Filename:newdb.dat
85
86 Copy slrn's data out of configdb, and into newdb. newdb is not defined in
87 the rc file, so the --config switches set up the database on the fly.
88
89 debconf-copydb configdb stdout -c Name:stdout -c Driver:Pipe \
90 -c InFd:none --pattern='^foo/'
91
92 Spit out all the items in the debconf database related to package foo.
93
94 debconf-copydb configdb pipe --config=Name:pipe \
95 --config=Driver:Pipe --config=InFd:none | \
96 ssh remotehost debconf-copydb pipe configdb \
97 --config=Name:pipe --config=Driver:Pipe
98
99 This uses the special purpose pipe driver to copy a database to a remote
100 system.
101
102 =head1 SEE ALSO
103
104 L<debconf.conf(5)>
105
106 =cut
107
108 my $pattern='.*';
109 my $owner_pattern;
110
111 # This hash holds config data. The sub adds a new item to the hash,
112 # and if it looks like a stanza just ended, tries to instantiate
113 # a dbdriver from the available config data first.
114 my %config;
115 sub config {
116 my ($field, $value)=split(/\s*:\s*/, $_[1], 2);
117 $field=~tr/-/_/;
118 $field=lc($field);
119 die "Parse error: \"$_[1]\"" unless defined $field and length $field;
120
121 if ($field eq 'name') {
122 if ($config{name}) {
123 Debconf::Db->makedriver(%config);
124 }
125 elsif (%config) {
126 warn "ignoring command line config data before $_[1]";
127 }
128 %config=();
129 }
130
131 $config{$field}=$value;
132 }
133
134 # Command line parsing.
135 GetOptions(
136 "pattern|p=s" => \$pattern,
137 "config|c=s" => \&config,
138 "owner-pattern=s" => \$owner_pattern,
139 ) || usage();
140 Debconf::Db->makedriver(%config) if %config;
141
142 my $srcname=shift || usage();
143 my $destname=shift || usage();
144
145 Debconf::Db->load;
146
147 my $src=Debconf::DbDriver->driver($srcname);
148 die "$0: source database, \"$srcname\" does not exist\n" unless ref $src;
149 my $dest=Debconf::DbDriver->driver($destname);
150 die "$0: destination database, \"$destname\" does not exist\n" unless ref $dest;
151
152 # Set up a copier to handle copying from one to the other.
153 my $copier=Debconf::DbDriver::Backup->new(
154 db => $src, backupdb => $dest, name => 'copier');
155
156 # Now just iterate over all items in src that patch the pattern, and tell
157 # the copier to make a copy of them.
158 my $i=$copier->iterator;
159 while (my $item=$i->iterate) {
160 next unless $item =~ /$pattern/;
161
162 if (defined $owner_pattern) {
163 my $fit_owner = 0;
164 foreach my $owner ($src->owners($item)){
165 $fit_owner = 1 if $owner =~ /$owner_pattern/;
166 }
167 next unless $fit_owner;
168 }
169
170 $copier->copy($item, $src, $dest);
171 }
172
173 $copier->shutdown;
174
175 =head1 AUTHOR
176
177 Joey Hess <[email protected]>
178
179 =cut