Imported Upstream version 3.2.0
[debian/amanda] / application-src / amzfs-snapshot.pl
1 #!@PERL@
2 # Copyright (c) 2008, 2009, 2010 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 warnings;
30 use Getopt::Long;
31
32 package Amanda::Script::Amzfs_snapshot;
33 use base qw(Amanda::Script Amanda::Application::Zfs);
34 use Symbol;
35 use IPC::Open3;
36 use Amanda::Config qw( :getconf :init );
37 use Amanda::Debug qw( :logging );
38 use Amanda::Util qw( :constants );
39 use Amanda::Paths;
40 use Amanda::Constants;
41
42 sub new {
43     my $class = shift;
44     my ($execute_where, $config, $host, $disk, $device, $level, $index, $message, $collection, $record, $df_path, $zfs_path, $pfexec_path, $pfexec) = @_;
45     my $self = $class->SUPER::new($execute_where, $config);
46
47     $self->{execute_where} = $execute_where;
48     $self->{config}        = $config;
49     $self->{host}          = $host;
50     if (defined $disk) {
51         $self->{disk}      = $disk;
52     } else {
53         $self->{disk}      = $device;
54     }
55     if (defined $device) {
56         $self->{device}    = $device;
57     } else {
58         $self->{device}    = $disk;
59     }
60     $self->{level}         = [ @{$level} ]; # Copy the array
61     $self->{index}         = $index;
62     $self->{message}       = $message;
63     $self->{collection}    = $collection;
64     $self->{record}        = $record;
65     $self->{df_path}       = $df_path;
66     $self->{zfs_path}      = $zfs_path;
67     $self->{pfexec_path}   = $pfexec_path;
68     $self->{pfexec}        = $pfexec;
69     $self->{pfexec_cmd}    = undef;
70
71     return $self;
72 }
73
74 sub zfs_snapshot_set_value() {
75    my $self   = shift;
76
77    $self->zfs_set_value();
78
79    if (!defined $self->{device}) {
80        return;
81    }
82
83    if (!defined $self->{mountpoint}) {
84        $self->print_to_server("$self->{disk} is not a directory", $Amanda::Script_App::ERROR);
85         
86    }
87 }
88
89 sub command_support {
90    my $self = shift;
91
92    print "CONFIG YES\n";
93    print "HOST YES\n";
94    print "DISK YES\n";
95    print "MESSAGE-LINE YES\n";
96    print "MESSAGE-XML NO\n";
97    print "EXECUTE-WHERE YES\n";
98 }
99
100 #define a execute_on_* function for every execute_on you want the script to do
101 #something
102 sub command_pre_dle_amcheck {
103     my $self = shift;
104
105     $self->zfs_snapshot_set_value();
106
107     if (!defined $self->{device}) {
108         return;
109     }
110
111     if ($self->{error_status} == $Amanda::Script_App::GOOD) {
112         if (defined $self->{mountpoint}) {
113             $self->print_to_server("mountpoint $self->{mountpoint}", $Amanda::Script_App::GOOD);
114             $self->print_to_server("directory $self->{directory}", $Amanda::Script_App::GOOD);
115             $self->print_to_server("dir $self->{dir}", $Amanda::Script_App::GOOD);
116         }
117         $self->print_to_server("snapshot $self->{snapshot}", $Amanda::Script_App::GOOD);
118         $self->zfs_create_snapshot("check");
119         print "PROPERTY directory $self->{directory}\n";
120     }
121 }
122
123 sub command_post_dle_amcheck {
124     my $self = shift;
125
126     $self->zfs_snapshot_set_value();
127
128     if (!defined $self->{device}) {
129         return;
130     }
131
132     $self->zfs_destroy_snapshot("check");
133 }
134
135 sub command_pre_dle_estimate {
136     my $self = shift;
137
138     $self->zfs_snapshot_set_value();
139     if ($self->{error_status} == $Amanda::Script_App::GOOD) {
140         $self->zfs_create_snapshot("estimate");
141         print "PROPERTY directory $self->{directory}\n";
142     }
143 }
144
145 sub command_post_dle_estimate {
146     my $self = shift;
147
148     $self->zfs_snapshot_set_value();
149     $self->zfs_destroy_snapshot("estimate");
150 }
151
152 sub command_pre_dle_backup {
153     my $self = shift;
154
155     $self->zfs_snapshot_set_value();
156     if ($self->{error_status} == $Amanda::Script_App::GOOD) {
157         $self->zfs_create_snapshot("backup");
158         print "PROPERTY directory $self->{directory}\n";
159     }
160 }
161
162 sub command_post_dle_backup {
163     my $self = shift;
164
165     $self->zfs_snapshot_set_value("backup");
166     $self->zfs_destroy_snapshot("backup");
167 }
168
169 package main;
170
171 sub usage {
172     print <<EOF;
173 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>.
174 EOF
175     exit(1);
176 }
177
178 my $opt_execute_where;
179 my $opt_config;
180 my $opt_host;
181 my $opt_disk;
182 my $opt_device;
183 my @opt_level;
184 my $opt_index;
185 my $opt_message;
186 my $opt_collection;
187 my $opt_record;
188 my $df_path  = 'df';
189 my $zfs_path = 'zfs';
190 my $pfexec_path = 'pfexec';
191 my $pfexec = "NO";
192
193 Getopt::Long::Configure(qw{bundling});
194 GetOptions(
195     'execute-where=s'  => \$opt_execute_where,
196     'config=s'         => \$opt_config,
197     'host=s'           => \$opt_host,
198     'disk=s'           => \$opt_disk,
199     'device=s'         => \$opt_device,
200     'level=s'          => \@opt_level,
201     'index=s'          => \$opt_index,
202     'message=s'        => \$opt_message,
203     'collection=s'     => \$opt_collection,
204     'record=s'         => \$opt_record,
205     'df-path=s'        => \$df_path,
206     'zfs-path=s'       => \$zfs_path,
207     'pfexec-path=s'    => \$pfexec_path,
208     'pfexec=s'         => \$pfexec
209 ) or usage();
210
211 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);
212 $script->do($ARGV[0]);