root/zetaback_agent.in

Revision 2f7771043693427156e4040c47f807cd956cb1a0, 5.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

reference the license from each source file

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 # Copyright (c) 2007 OmniTI Computer Consulting, Inc. All rights reserved.
4 # For information on licensing see:
5 #   https://labs.omniti.com/zetaback/trunk/LICENSE
6
7 use strict;
8 use Getopt::Long;
9 use Data::Dumper;
10
11 use vars qw/%conf $version_string
12             $PREFIX $CONF $LIST $FULL $SNAP $ZFS $BASE $RESTORE $VERSION
13             $BUG_6343779/;
14 $version_string = '0.1';
15 $PREFIX = q^__PREFIX__^;
16 $CONF = qq^$PREFIX/etc/zetaback_agent.conf^;
17
18 =pod
19
20 =head1 NAME
21
22 zetaback_agent - client-side component of zetaback.
23
24 =head1 SYNOPSIS
25
26   zetaback_agent -v
27
28   zetaback_agent -l [-c conf]
29
30   zetaback_agent -r [-b <timestamp>] [-c conf] [-z zfs]
31
32   zetaback -f <timestamp> [-c conf] [-z zfs]
33
34   zetaback -i <timestamp> [-c conf] [-z zfs]
35
36   zetaback -d <snap> -z <zfs> [-c conf]
37
38 =cut
39
40 GetOptions(
41   "c=s" => \$CONF,
42   "l"   => \$LIST,
43   "r"   => \$RESTORE,
44   "z=s" => \$ZFS,
45   "d=s" => \$SNAP,
46   "f=s" => \$FULL,
47   "i=s" => \$BASE,
48   "b=s" => \$BUG_6343779,
49   "v"   => \$VERSION,
50 );
51
52 =pod
53
54 =head1 DESCRIPTION
55
56 B<zetaback_agent> handles requests from zetaback and performs the requested
57 operations on a host.  Normally B<zetaback_agent> is only called by
58 zetaback and should never need to be invoked directly.
59
60 =head1 OPTIONS
61
62 The following options are available:
63
64 =over
65
66 =item -c <conf>
67
68 Use the specified file as the configuration file.  The default file, if
69 none is specified is /etc/zetaback_agent.conf.  The prefix of this file
70 may also be specified in the Makefile.
71
72 =item -d <snap>
73
74 Delete the specified snapshot.  Requires the use of -z to specify the
75 ZFS filesystem.
76
77 =item -f <timestamp>
78
79 Perform a full backup.  The name of the backup will include <timestamp>,
80 which is provided by the backup server.
81
82 =item -i <timestamp>
83
84 Perform an incremental backup.  The name of the backup will include
85 <timestamp>, which is provided by the backup server.
86
87 =item -l
88
89 List ZFS filesystems.
90
91 =item -r
92
93 Perform a restore.
94
95 =item -b
96
97 When performing a restore, if -b is specified, it informs the agent that
98 the receive command is an incremental based of the full snapshot with the
99 timestamp specified.  The agent will unmount and rollback the filesystem
100 prior to applying the incremental in order to work around bug 6343779.
101
102 =item -v
103
104 Print the version number and exit.
105
106 =item -z
107
108 Specify a ZFS filesystem to backup, restore, or delete.
109
110 =cut
111
112 if($VERSION) {
113   print "zetaback_agent: $version_string\n";
114   exit 0;
115 }
116
117 =pod
118
119 =head1 CONFIGURATION
120
121 The zetaback_agent configuration file contains a pattern list of ZFS
122 filesystems to be backed up.  The pattern list is a Perl-compatible
123 regular expression (PCRE).  Only one 'pattern=' line is permitted.
124
125 =head1 CONFIGURATION EXAMPLES
126
127 =head2 All ZFS filesystems
128
129 This pattern matches all ZFS filesystems.
130
131   pattern=.
132
133 =head2 Substring match
134
135 This will match anywhere in the name of the ZFS filesystem.  This is
136 helpful for catching all ZFS filesystems in a particular zpool, while
137 excluding any others.
138
139   pattern=zones
140
141 =head2 Left-anchored names
142
143 This pattern matches all ZFS filesystems whose names begin with 'www'.
144
145   pattern=^www
146
147 =head2 Specific ZFS filesystems
148
149 This pattern matches specific ZFS filesystems.
150
151   pattern=(?:data|mirrors|www)
152
153 =cut
154
155 # Read our config in
156 open(CONF, "<$CONF");
157 while(<CONF>) { /^\s*([^#](?:\S*)?)\s*=\s*(\S+)/ && ($conf{lc($1)} = $2); }
158 close(CONF);
159
160 sub zfs_agent_remove_snap {
161   my $target = $ZFS . '@';
162   die "zfs_agent_remove_snap: insufficient args\n" unless($ZFS && $SNAP);
163   if($SNAP eq '__zb_incr' or
164      $SNAP =~ /__zb_full_\d+/) {
165     $target .= $SNAP;
166   }
167   else {
168     die "zfs_agent_remove_snap: illegal snap: $SNAP\n";
169   }
170   `/usr/sbin/zfs destroy $target`;
171 }
172
173 sub zfs_agent_perform_full {
174   my $target = $ZFS . '@__zb_full_' . $FULL;
175   unless($ZFS && $FULL =~ /^\d+$/) {
176     die "zfs_agent_perform_full: bad fs or snap name\n"
177   }
178   `/usr/sbin/zfs snapshot $target`;
179   my @cmd = ("/usr/sbin/zfs", "send", $target);
180   exec { $cmd[0] } @cmd;
181   exit;
182 }
183
184 sub zfs_agent_perform_incremental {
185   my $target = $ZFS . '@__zb_incr';
186   my $base = $ZFS . '@__zb_full_' . $BASE;
187   unless($ZFS && $BASE) {
188     die "zfs_agent_perform_incremental: bad args\n"
189   }
190   `/usr/sbin/zfs snapshot $target`;
191   my @cmd = ("/usr/sbin/zfs", "send", "-i", $base, $target);
192   exec { $cmd[0] } @cmd;
193   exit;
194 }
195
196 sub zfs_agent_list {
197   my %zfs;
198   open(ZFSLIST, "/usr/sbin/zfs list -H |");
199   while(<ZFSLIST>) {
200     chomp;
201     my @line = split /\t/;
202     if($line[0] =~ /$conf{pattern}/) {
203       if($line[0] =~ /(\S+)\@([^\@]+)$/) {
204         $zfs{$1} ||= [];
205         push @{$zfs{$1}}, $2;
206       }
207       else {
208         $zfs{$line[0]} ||= [];
209       }
210     }
211   }
212   close(ZFSLIST);
213
214   foreach my $fs (sort keys %zfs) {
215     print "$fs [".join(',',@{$zfs{$fs}})."]\n";
216   }
217 }
218
219 sub zfs_agent_perform_restore {
220   unless($ZFS && $RESTORE) {
221     die "zfs_agent_perform_restore: bad state\n";
222   }
223   if($BUG_6343779) {
224     # Optionally work around Solaris bug: 6343779
225     my $base = $ZFS . '@__zb_full_' . $BUG_6343779;
226     `/usr/sbin/zfs unmount $ZFS`;
227     `/usr/sbin/zfs rollback $base`;
228   }
229   my @cmd = ("/usr/sbin/zfs", "recv", $ZFS);
230   exec { $cmd[0] } @cmd;
231   exit;
232 }
233
234 if($LIST) { zfs_agent_list(); exit; }
235 if($ZFS && $SNAP) { zfs_agent_remove_snap(); exit; }
236 if($ZFS && $RESTORE) { zfs_agent_perform_restore(); exit; }
237 if($ZFS && $FULL) { zfs_agent_perform_full(); exit; }
238 if($ZFS && $BASE) { zfs_agent_perform_incremental(); exit; }
239
240 =pod
241
242 =head1 FILES
243
244 =over
245
246 =item /etc/zetaback_agent.conf
247
248 The zetaback_agent configuration file.  The location of the file can be
249 specified on the command line with the -c flag.
250
251 =back
252
253 =head1 SEE ALSO
254
255 zetaback(1)
256
257 =cut
Note: See TracBrowser for help on using the browser.