| 1 | package Brackup::Target::CloudFiles; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'Brackup::Target'; | ||
| 6 | use Date::Parse; | ||
| 7 | use Carp qw(croak); | ||
| 8 | |||
| 9 | |||
| 10 | eval { require Net::Mosso::CloudFiles } or die "You need the Net::Mosso::CloudFiles module installed to use the CloudFiles target in brackup. Please install this module first.\n\n"; | ||
| 11 | |||
| 12 | # fields in object: | ||
| 13 | # cf -- Net::Mosso::CloudFiles | ||
| 14 | # username | ||
| 15 | # apiKey | ||
| 16 | # chunkContainer : $self->{username} . "-chunks"; | ||
| 17 | # backupContainer : $self->{username} . "-backups"; | ||
| 18 | # | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ($class, $confsec) = @_; | ||
| 22 | my $self = $class->SUPER::new($confsec); | ||
| 23 | |||
| 24 | $self->{username} = $confsec->value("cf_username") | ||
| 25 | or die "No 'cf_username'"; | ||
| 26 | $self->{apiKey} = $confsec->value("cf_api_key") | ||
| 27 | or die "No 'cf_api_key'"; | ||
| 28 | |||
| 29 | $self->_common_cf_init; | ||
| 30 | |||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _common_cf_init { | ||
| 35 | my $self = shift; | ||
| 36 | $self->{chunkContainerName} = $self->{username} . "-chunks"; | ||
| 37 | $self->{backupContainerName} = $self->{username} . "-backups"; | ||
| 38 | |||
| 39 | $self->{cf} = Net::Mosso::CloudFiles->new( | ||
| 40 | user => $self->{username}, | ||
| 41 | key => $self->{apiKey} | ||
| 42 | ); | ||
| 43 | |||
| 44 | #createContainer makes the object and returns it, or returns it | ||
| 45 | #if it already exists | ||
| 46 | $self->{chunkContainer} = | ||
| 47 | $self->{cf}->create_container(name => $self->{chunkContainerName}) | ||
| 48 | or die "Failed to get chunk container"; | ||
| 49 | $self->{backupContainer} = | ||
| 50 | $self->{cf}->create_container(name => $self->{backupContainerName}) | ||
| 51 | or die "Failed to get backup container"; | ||
| 52 | |||
| 53 | } | ||
| 54 | |||
| 55 | sub _prompt { | ||
| 56 | my ($q) = @_; | ||
| 57 | my $ans = <STDIN>; | ||
| 58 | $ans =~ s/^\s+//; | ||
| 59 | $ans =~ s/\s+$//; | ||
| 60 | return $ans; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub new_from_backup_header { | ||
| 64 | my ($class, $header, $confsec) = @_; | ||
| 65 | |||
| 66 | my $username = ($ENV{'CF_USERNAME'} || | ||
| 67 | $confsec->value('cf_username') || | ||
| 68 | _prompt("Your CloudFiles username: ")) | ||
| 69 | or die "Need your Cloud Files username.\n"; | ||
| 70 | |||
| 71 | my $apiKey = ($ENV{'CF_API_KEY'} || | ||
| 72 | $confsec->value('cf_api_key') || | ||
| 73 | _prompt("Your CloudFiles api key: ")) | ||
| 74 | or die "Need your CloudFiles api key.\n"; | ||
| 75 | |||
| 76 | my $self = bless {}, $class; | ||
| 77 | $self->{username} = $username; | ||
| 78 | $self->{apiKey} = $apiKey; | ||
| 79 | $self->_common_cf_init; | ||
| 80 | return $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub has_chunk { | ||
| 84 | my ($self, $chunk) = @_; | ||
| 85 | my $dig = $chunk->backup_digest; # "sha1:sdfsdf" format scalar | ||
| 86 | |||
| 87 | my $res = $self->{chunkContainer}->object(name => $dig); | ||
| 88 | |||
| 89 | return 0 unless $res; | ||
| 90 | |||
| 91 | #return 0 if $@ && $@ =~ /key not found/; | ||
| 92 | |||
| 93 | #TODO: check for content type? | ||
| 94 | #return 0 unless $res->{content_type} eq "x-danga/brackup-chunk"; | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub load_chunk { | ||
| 99 | my ($self, $dig) = @_; | ||
| 100 | |||
| 101 | my $val = $self->{chunkContainer}->object(name => $dig)->get | ||
| 102 | or return 0; | ||
| 103 | return \ $val; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub store_chunk { | ||
| 107 | my ($self, $chunk) = @_; | ||
| 108 | my $dig = $chunk->backup_digest; | ||
| 109 | my $chunkref = $chunk->chunkref; | ||
| 110 | |||
| 111 | my $content = do { local $/; <$chunkref> }; | ||
| 112 | |||
| 113 | $self->{chunkContainer}->object( | ||
| 114 | name => $dig, | ||
| 115 | content_type => 'x-danga/brackup-chunk' | ||
| 116 | )->put($content); | ||
| 117 | |||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub delete_chunk { | ||
| 122 | my ($self, $dig) = @_; | ||
| 123 | |||
| 124 | return $self->{chunkContainer}->object(name => $dig)->delete; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub chunks { | ||
| 128 | my $self = shift; | ||
| 129 | my @objectNames; | ||
| 130 | |||
| 131 | my @objects = $self->{chunkContainer}->objects->all; | ||
| 132 | foreach (@objects){ push @objectNames, $_->name;} | ||
| 133 | return @objectNames; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub store_backup_meta { | ||
| 137 | my ($self, $name, $fh) = @_; | ||
| 138 | |||
| 139 | my $content = do { local $/; <$fh> }; | ||
| 140 | |||
| 141 | $self->{backupContainer}->object(name => $name)->put($content); | ||
| 142 | |||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub backups { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my @ret; | ||
| 150 | |||
| 151 | my @backups = $self->{backupContainer}->objects->all; | ||
| 152 | |||
| 153 | foreach my $backup (@backups) { | ||
| 154 | push @ret, Brackup::TargetBackupStatInfo->new( | ||
| 155 | $self, $backup->name, | ||
| 156 | time => str2time($backup->last_modified), | ||
| 157 | size => $backup->size); | ||
| 158 | } | ||
| 159 | return @ret; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub get_backup { | ||
| 163 | my $self = shift; | ||
| 164 | my ($name, $output_file) = @_; | ||
| 165 | |||
| 166 | my $val = $self->{backupContainer}->object(name => $name)->get | ||
| 167 | or return 0; | ||
| 168 | |||
| 169 | $output_file ||= "$name.brackup"; | ||
| 170 | open(my $out, ">$output_file") or die "Failed to open $output_file: $!\n"; | ||
| 171 | |||
| 172 | my $outv = syswrite($out, $val); | ||
| 173 | |||
| 174 | die "download/write error" unless | ||
| 175 | $outv == do { use bytes; length $val }; | ||
| 176 | close $out; | ||
| 177 | |||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub delete_backup { | ||
| 182 | my $self = shift; | ||
| 183 | my $name = shift; | ||
| 184 | return $self->{backupContainer}->object(name => $name)->delete; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | ############################################################# | ||
| 189 | # These functions are for the brackup-verify-inventory script | ||
| 190 | ############################################################# | ||
| 191 | |||
| 192 | sub chunkpath { | ||
| 193 | my $self = shift; | ||
| 194 | my $dig = shift; | ||
| 195 | |||
| 196 | return $dig; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub size { | ||
| 200 | my $self = shift; | ||
| 201 | my $dig = shift; | ||
| 202 | |||
| 203 | my $obj = $self->{chunkContainer}->object(name => $dig); | ||
| 204 | $obj->head(); | ||
| 205 | my $size = $obj->size; | ||
| 206 | |||
| 207 | return $size; | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | =head1 NAME | ||
| 214 | |||
| 215 | Brackup::Target::CloudFiles - backup to Rackspace's CloudFiles Service | ||
| 216 | |||
| 217 | =head1 EXAMPLE | ||
| 218 | |||
| 219 | In your ~/.brackup.conf file: | ||
| 220 | |||
| 221 | [TARGET:cloudfiles] | ||
| 222 | type = CloudFiles | ||
| 223 | cf_username = ... | ||
| 224 | cf_api_key = .... | ||
| 225 | |||
| 226 | =head1 CONFIG OPTIONS | ||
| 227 | |||
| 228 | =over | ||
| 229 | |||
| 230 | =item B<type> | ||
| 231 | |||
| 232 | Must be "B<CloudFiles>". | ||
| 233 | |||
| 234 | =item B<cf_username> | ||
| 235 | |||
| 236 | Your Rackspace/Mosso CloudFiles user name. | ||
| 237 | |||
| 238 | =item B<cf_api_key> | ||
| 239 | |||
| 240 | Your Rackspace/Mosso CloudFiles api key. | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =head1 SEE ALSO | ||
| 245 | |||
| 246 | L<Brackup::Target> | ||
| 247 | |||
| 248 | L<Net::Mosso::CloudFiles> -- required module to use Brackup::Target::CloudFiles | ||
| 249 | |||
| 250 | =head1 AUTHOR | ||
| 251 | |||
| 252 | William Wolf E<lt>throughnothing@gmail.comE<gt> | ||
| 253 | |||
| 254 |