Skip to content

Instantly share code, notes, and snippets.

@teckl
Last active March 14, 2023 06:28
Show Gist options
  • Save teckl/6bfb3dce299f52e49038c424f9939d0e to your computer and use it in GitHub Desktop.
Save teckl/6bfb3dce299f52e49038c424f9939d0e to your computer and use it in GitHub Desktop.
Bucket copy script from S3 to R2 using rclone
#!/bin/env perl
use strict;
use warnings;
use v5.10;
use Data::Dumper;
use File::Slurp;
use IO::File;
use Log::Minimal;
use Time::Piece;
my $from_bucket = 's3:xxxx-production';
my $to_bucket = 'r2:xxxx-production';
my $target = 'z';
my $copy_cmd = '';
die 'Never run it after switching from S3 to R2.'; # 本番切り替え後は実行しないこと!!
for my $first_d ("0" .. "9", "a" .. "z") {
next unless $first_d eq $target;
my $second_lsf_cmd = sprintf("rclone lsf %s/%s/", $from_bucket, $first_d);
my $second_ret = qx($second_lsf_cmd);
chomp($second_ret);
for my $second_d (split(/\r\n|\n/, $second_ret)) {
my $copy_cmd = sprintf('rclone copy --transfers 100 --s3-upload-concurrency 100 --s3-chunk-size 50M --no-check-dest --ignore-checksum --s3-disable-checksum -P %s/%s/%s %s/%s/%s', $from_bucket, $first_d, $second_d, $to_bucket, $first_d, $second_d);
infof('start - %s', $copy_cmd);
say $copy_cmd;
_log($copy_cmd, 'start');
system $copy_cmd;
_log($copy_cmd, 'finish');
infof('end - %s', $copy_cmd);
}
}
sub logfile {
return sprintf('/var/log/rclone/s3_to_r2_copy_%s.log', Time::Piece->new->strftime("%Y%m%d"));
}
sub _log {
my @data = @_;
my $fh = IO::File->new(logfile(), O_WRONLY|O_APPEND|O_CREAT) or die $!;
my $log = Time::Piece->new->datetime(T => ' ') . ' : ' . join(' ', @data) . "\n";
$fh->print($log);
$fh->close;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment