Debian Codegraph @

application-src/amzfs-snapshot.pl

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