upload to s3

Mr. Louis 2016-03-14 21:50:49.943581 UTC

1#!/usr/bin/perl -w
2use strict;
3use warnings;
4use feature ':5.10';
5use utf8;
6use File::Basename;
7$ENV{'PATH'} = '/bin:/usr/bin';
8
9my $s3cmd = '/home/data1/protected/Programming/git/s3cmd/s3cmd';
10
11$| = 1;
12binmode(STDOUT, ":encoding(UTF-8)");
13binmode(STDIN, ":encoding(UTF-8)");
14
15my $args = '';
16if($ARGV[0] && $ARGV[0] =~ /delete/i) {
17 $args = ' --delete-removed ';
18}
19
20if($ARGV[0] && $ARGV[0] =~ /cache/i) {
21 $args = ' --add-header "Cache-Control:max-age=0" ';
22}
23
24if($ARGV[0] && $ARGV[0] =~ /invalidate/i) {
25 $args = ' --cf-invalidate ';
26}
27
28my $exclude = qq{--exclude '*~' --exclude '*.swp'};
29my $home = '/home/data1/protected/public_html';
30my $pwd = `/bin/pwd`;
31chomp $pwd;
32exit unless $pwd =~ /$home/;
33my $basename = basename($pwd);
34my $dirname = dirname($pwd);
35exit unless $dirname =~ /$home/;
36say "FOUND DIRECTORY: $basename";
37my $fulldir = $dirname . '/' . $basename;
38my @names = split('/', $fulldir);
39my @bucket = splice(@names, 5, 1);
40my $bucket = $bucket[0];
41my @destination = splice(@names, 5);
42my $destination = 's3://' . $bucket . '/';
43$destination .= join('/', @destination) . '/' if $destination[0];
44say "CHECKING BUCKET...";
45my $s3check = `$s3cmd ws-info s3://$bucket/`;
46chomp $s3check;
47die "S3 BUCKET DOESN'T EXIST: s3//$basename" if $s3check =~ /Unable to receive/;
48my $do = "$s3cmd $exclude --delete-removed --delete-after --rr $args sync . '$destination'";
49say "SYNCHRONIZATION GOING ON...";
50system $do;
51say "DONE";