handle exists for first_time_indexing
[BackupPC.git] / lib / BackupPC / Xfer.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Xfer package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a Factory for invoking transfer protocols in
8 #   a polymorphic manner.  This libary allows for easier expansion of
9 #   supported protocols.
10 #
11 # AUTHOR
12 #   Paul Mantz  <pcmantz@zmanda.com>
13 #
14 # COPYRIGHT
15 #   Copyright (C) 2001-2009  Craig Barratt
16 #
17 #   This program is free software; you can redistribute it and/or modify
18 #   it under the terms of the GNU General Public License as published by
19 #   the Free Software Foundation; either version 2 of the License, or
20 #   (at your option) any later version.
21 #
22 #   This program is distributed in the hope that it will be useful,
23 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
24 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 #   GNU General Public License for more details.
26 #
27 #   You should have received a copy of the GNU General Public License
28 #   along with this program; if not, write to the Free Software
29 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30 #
31 #========================================================================
32 #
33 # Version 3.2.0, released 31 Jul 2010.
34 #
35 # See http://backuppc.sourceforge.net.
36 #
37 #========================================================================
38
39
40 package BackupPC::Xfer;
41
42 use strict;
43 use Encode qw/from_to encode/;
44
45 use BackupPC::Xfer::Archive;
46 use BackupPC::Xfer::Ftp;
47 use BackupPC::Xfer::Protocol;
48 use BackupPC::Xfer::Rsync;
49 use BackupPC::Xfer::Smb;
50 use BackupPC::Xfer::Tar;
51
52 use vars qw( $errStr );
53
54 sub create
55 {
56     my($protocol, $bpc, $args) = @_;
57     my $xfer;
58
59     $errStr = undef;
60
61     if ( $protocol eq 'archive' ) {
62
63         $xfer = BackupPC::Xfer::Archive->new( $bpc, $args );
64         $errStr = BackupPC::Xfer::Archive::errStr() if ( !defined($xfer) );
65         return $xfer;
66
67     } elsif ( $protocol eq 'ftp' ) {
68
69         $xfer = BackupPC::Xfer::Ftp->new( $bpc, $args );
70         $errStr = BackupPC::Xfer::Ftp::errStr() if ( !defined($xfer) );
71         return $xfer;
72
73     } elsif ( $protocol eq 'rsync' || $protocol eq 'rsyncd' ) {
74
75         $xfer = BackupPC::Xfer::Rsync->new( $bpc, $args );
76         $errStr = BackupPC::Xfer::Rsync::errStr() if ( !defined($xfer) );
77         return $xfer;
78
79     } elsif ( $protocol eq 'smb' ) {
80
81         $xfer = BackupPC::Xfer::Smb->new( $bpc, $args );
82         $errStr = BackupPC::Xfer::Smb::errStr() if ( !defined($xfer) );
83         return $xfer;
84
85     } elsif ( $protocol eq 'tar' ) {
86
87         $xfer = BackupPC::Xfer::Tar->new( $bpc, $args );
88         $errStr = BackupPC::Xfer::Tar::errStr() if ( !defined($xfer) );
89         return $xfer;
90
91     } elsif ( $protocol eq 'protocol') {
92
93         $xfer = BackupPC::Xfer::Protocol->new( $bpc, $args );
94         $errStr = BackupPC::Xfer::Protocol::errStr() if ( !defined($xfer) );
95         return $xfer;
96
97     } else {
98
99         $xfer = undef;
100         $errStr = "$protocol is not a supported protocol.";
101         return $xfer;
102     }
103 }
104
105 #
106 # getShareNames() loads the correct shares dependent on the
107 # transfer type.
108 #
109 sub getShareNames
110 {
111     my($conf) = @_;
112     my $ShareNames;
113
114     if ( $conf->{XferMethod} eq "tar" ) {
115         $ShareNames = $conf->{TarShareName};
116
117     } elsif ( $conf->{XferMethod} eq "ftp" ) {
118         $ShareNames = $conf->{FtpShareName};
119
120     } elsif ( $conf->{XferMethod} eq "rsync" || $conf->{XferMethod} eq "rsyncd" ) {
121         $ShareNames = $conf->{RsyncShareName};
122
123     } elsif ( $conf->{XferMethod} eq "smb" ) {
124         $ShareNames = $conf->{SmbShareName};
125
126     } else {
127         #
128         # default to smb shares
129         #
130         $ShareNames = $conf->{SmbShareName};
131     }
132
133     $ShareNames = [$ShareNames] unless ref($ShareNames) eq "ARRAY";
134     return $ShareNames;
135 }
136
137
138 sub getRestoreCmd
139 {
140     my($conf) = @_;
141     my $restoreCmd;
142
143     if ( $conf->{XferMethod} eq "archive" ) {
144         $restoreCmd = undef;
145
146     } elsif ( $conf->{XferMethod} eq "ftp" ) {
147         $restoreCmd = undef;
148
149     } elsif ( $conf->{XferMethod} eq "rsync"
150            || $conf->{XferMethod} eq "rsyncd" ) {
151         $restoreCmd = $conf->{RsyncRestoreArgs};
152
153     } elsif ( $conf->{XferMethod} eq "tar" ) {
154         $restoreCmd = $conf->{TarClientRestoreCmd};
155
156     } elsif ( $conf->{XferMethod} eq "smb" ) {
157         $restoreCmd = $conf->{SmbClientRestoreCmd};
158
159     } else {
160
161         #
162         # protocol unrecognized
163         #
164         $restoreCmd = undef;
165     }
166     return $restoreCmd;
167 }
168
169
170 sub restoreEnabled
171 {
172     my($conf) = @_;
173     my $restoreCmd;
174
175     if ( $conf->{XferMethod} eq "archive" ) {
176         return;
177
178     } elsif ( $conf->{XferMethod} eq "ftp" ) {
179         return;
180
181     } elsif ( $conf->{XferMethod} eq "rsync"
182            || $conf->{XferMethod} eq "rsyncd"
183            || $conf->{XferMethod} eq "tar"
184            || $conf->{XferMethod} eq "smb" ) {
185         $restoreCmd = getRestoreCmd( $conf );
186         return !!(
187             ref $restoreCmd eq "ARRAY"
188             ? @$restoreCmd
189             : $restoreCmd ne ""
190         );
191
192     } else {
193         return;
194     }
195 }
196
197
198 sub errStr
199 {
200     return $errStr;
201 }
202
203 1;