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