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