-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3backup.pl
executable file
·134 lines (110 loc) · 3.13 KB
/
s3backup.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/local/cpanel/3rdparty/bin/perl
use strict;
use warnings;
# These are the commands that a custom destination script must process
my %commands = (
put => \&my_put,
get => \&my_get,
ls => \&my_ls,
mkdir => \&my_mkdir,
chdir => \&my_chdir,
rmdir => \&my_rmdir,
delete => \&my_delete,
);
# There must be at least the command and the local directory
usage() if ( @ARGV < 2 );
#
# The command line arguments passed to the script will be in the following order:
# command, local_directory, command arguments, and optionally, host, user password
# The local directory is passed in so we know from which directory to run the command
# we need to pass this in each time since we start the script fresh for each command
#
my ( $cmd, $local_dir, @args ) = @ARGV;
# complain if the command does not exist
usage() unless exists $commands{$cmd};
# Run our command
$commands{$cmd}->(@args);
#
# This script should only really be executed by the custom backup destination type
# If someone executes it directly out of curiosity, give them usage info
#
sub usage {
my @cmds = sort keys %commands;
print STDERR "This script is for implementing a custom backup destination\n";
print STDERR "It requires the following arguments: cmd, local_dir, cmd_args\n";
print STDERR "These are the valid commands: @cmds\n";
exit 1;
}
#
# Build the remote url
#
sub my_remote {
my ( $bucket) = @_;
return "s3://${bucket}";
}
#
# Copy a local file to a remote destination
#
sub my_put {
my ( $local, $remote, $host, $user, $password ) = @_;
my $full_remote = my_remote($host);
`s3cmd put $local ${full_remote}${remote}`;
return;
}
#
# Copy a remote file to a local destination
#
sub my_get {
my ( $remote, $local, $host, $user, $password ) = @_;
my $full_remote = my_remote($host);
`s3cmd get ${full_remote}${remote} ${local} --force`;
return;
}
#
# Print out the results of doing an ls operation
# The calling program will expect the data to be
# in the format supplied by 'ls -l' and have it
# printed to STDOUT
#
sub my_ls {
my ( $path, $host, $user, $password ) = @_;
my $full_remote = my_remote($host);
`s3cmd ls $full_remote`;
return;
}
#
# Create a directory on the remote destination
#
sub my_mkdir {
my ( $path, $recurse, $host, $user, $password ) = @_;
return;
}
#
# Change into a directory on the remote destination
# This does not have the same meaning as it normally would since the script
# is run anew for each command call.
# This needs to do the operation to ensure it doesn't fail
# then print the new resulting directory that the calling program
# will pass in as the local directory for subsequent calls
#
sub my_chdir {
my ( $path, $host, $user, $password ) = @_;
print $path;
return;
}
#
# Recursively delete a directory on the remote destination
#
sub my_rmdir {
my ( $path, $host, $user, $password ) = @_;
return;
}
#
# Delete an individual file on the remote destination
#
sub my_delete {
my ( $path, $host, $user, $password ) = @_;
my $full_remote = my_remote($host);
`s3cmd del ${full_remote}$path`;
return;
}