Imported Upstream version 3.1.0
[debian/amanda] / application-src / amzfs-snapshot.pl
1 #!@PERL@
2 # Copyright (c) 2008,2009 Zmanda, Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 # PROPERTY:
21 #
22 #    DF-PATH     (Default from PATH): Path to the 'df' binary
23 #    ZFS-PATH    (Default from PATH): Path to the 'zfs' binary
24 #    PFEXEC-PATH (Default from PATH): Path to the 'pfexec' binary
25 #    PFEXEC      (Default NO): Set to "YES" if you want to use pfexec
26 #
27 use lib '@amperldir@';
28 use strict;
29 use Getopt::Long;
30
31 package Amanda::Script::Amzfs_snapshot;
32 use base qw(Amanda::Script Amanda::Application::Zfs);
33 use Symbol;
34 use IPC::Open3;
35 use Amanda::Config qw( :getconf :init );
36 use Amanda::Debug qw( :logging );
37 use Amanda::Util qw( :constants );
38 use Amanda::Paths;
39 use Amanda::Constants;
40
41 sub new {
42     my $class = shift;
43     my ($execute_where, $config, $host, $disk, $device, $level, $index, $message, $collection, $record, $df_path, $zfs_path, $pfexec_path, $pfexec) = @_;
44     my $self = $class->SUPER::new($execute_where, $config);
45
46     $self->{execute_where} = $execute_where;
47     $self->{config}        = $config;
48     $self->{host}          = $host;
49     if (defined $disk) {
50         $self->{disk}      = $disk;
51     } else {
52         $self->{disk}      = $device;
53     }
54     if (defined $device) {
55         $self->{device}    = $device;
56     } else {
57         $self->{device}    = $disk;
58     }
59     $self->{level}         = [ @{$level} ]; # Copy the array
60     $self->{index}         = $index;
61     $self->{message}       = $message;
62     $self->{collection}    = $collection;
63     $self->{record}        = $record;
64     $self->{df_path}       = $df_path;
65     $self->{zfs_path}      = $zfs_path;
66     $self->{pfexec_path}   = $pfexec_path;
67     $self->{pfexec}        = $pfexec;
68     $self->{pfexec_cmd}    = undef;
69
70     return $self;
71 }
72
73 sub zfs_snapshot_set_value() {
74    my $self   = shift;
75
76    $self->zfs_set_value();
77
78    if (!defined $self->{device}) {
79        return;
80    }
81
82    if (!defined $self->{mountpoint}) {
83        $self->print_to_server("$self->{disk} is not a directory", $Amanda::Script_App::ERROR);
84         
85    }
86 }
87
88 sub command_support {
89    my $self = shift;
90
91    print "CONFIG YES\n";
92    print "HOST YES\n";
93    print "DISK YES\n";
94    print "MESSAGE-LINE YES\n";
95    print "MESSAGE-XML NO\n";
96    print "EXECUTE-WHERE YES\n";
97 }
98
99 #define a execute_on_* function for every execute_on you want the script to do
100 #something
101 sub command_pre_dle_amcheck {
102     my $self = shift;
103
104     $self->zfs_snapshot_set_value();
105
106     if (!defined $self->{device}) {
107         return;
108     }
109
110     if ($self->{error_status} == $Amanda::Script_App::GOOD) {
111         if (defined $self->{mountpoint}) {
112             $self->print_to_server("mountpoint $self->{mountpoint}", $Amanda::Script_App::GOOD);
113             $self->print_to_server("directory $self->{directory}", $Amanda::Script_App::GOOD);
114             $self->print_to_server("dir $self->{dir}", $Amanda::Script_App::GOOD);
115         }
116         $self->print_to_server("snapshot $self->{snapshot}", $Amanda::Script_App::GOOD);
117         $self->zfs_create_snapshot("check");
118         print "PROPERTY directory $self->{directory}\n";
119     }
120 }
121
122 sub command_post_dle_amcheck {
123     my $self = shift;
124
125     $self->zfs_snapshot_set_value();
126
127     if (!defined $self->{device}) {
128         return;
129     }
130
131     $self->zfs_destroy_snapshot("check");
132 }
133
134 sub command_pre_dle_estimate {
135     my $self = shift;
136
137     $self->zfs_snapshot_set_value();
138     if ($self->{error_status} == $Amanda::Script_App::GOOD) {
139         $self->zfs_create_snapshot("estimate");
140         print "PROPERTY directory $self->{directory}\n";
141     }
142 }
143
144 sub command_post_dle_estimate {
145     my $self = shift;
146
147     $self->zfs_snapshot_set_value();
148     $self->zfs_destroy_snapshot("estimate");
149 }
150
151 sub command_pre_dle_backup {
152     my $self = shift;
153
154     $self->zfs_snapshot_set_value();
155     if ($self->{error_status} == $Amanda::Script_App::GOOD) {
156         $self->zfs_create_snapshot("backup");
157         print "PROPERTY directory $self->{directory}\n";
158     }
159 }
160
161 sub command_post_dle_backup {
162     my $self = shift;
163
164     $self->zfs_snapshot_set_value("backup");
165     $self->zfs_destroy_snapshot("backup");
166 }
167
168 package main;
169
170 sub usage {
171     print <<EOF;
172 Usage: amzfs-snapshot <command> --execute-where=client --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --df-path=<path/to/df> --zfs-path=<path/to/zfs> --pfexec-path=<path/to/pfexec> --pfexec=<yes|no>.
173 EOF
174     exit(1);
175 }
176
177 my $opt_execute_where;
178 my $opt_config;
179 my $opt_host;
180 my $opt_disk;
181 my $opt_device;
182 my @opt_level;
183 my $opt_index;
184 my $opt_message;
185 my $opt_collection;
186 my $opt_record;
187 my $df_path  = 'df';
188 my $zfs_path = 'zfs';
189 my $pfexec_path = 'pfexec';
190 my $pfexec = "NO";
191
192 Getopt::Long::Configure(qw{bundling});
193 GetOptions(
194     'execute-where=s'  => \$opt_execute_where,
195     'config=s'         => \$opt_config,
196     'host=s'           => \$opt_host,
197     'disk=s'           => \$opt_disk,
198     'device=s'         => \$opt_device,
199     'level=s'          => \@opt_level,
200     'index=s'          => \$opt_index,
201     'message=s'        => \$opt_message,
202     'collection=s'     => \$opt_collection,
203     'record=s'         => \$opt_record,
204     'df-path=s'        => \$df_path,
205     'zfs-path=s'       => \$zfs_path,
206     'pfexec-path=s'    => \$pfexec_path,
207     'pfexec=s'         => \$pfexec
208 ) or usage();
209
210 my $script = Amanda::Script::Amzfs_snapshot->new($opt_execute_where, $opt_config, $opt_host, $opt_disk, $opt_device, \@opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, $df_path, $zfs_path, $pfexec_path, $pfexec);
211 $script->do($ARGV[0]);