sub backup (Str $filepath, Int :$limit = 3, Str :$ext = 'bk', Bool :$follow-symlinks = False) {
my $abs = $follow-symlinks ?? $filepath.IO.resolve.absolute !! $filepath.IO.absolute;
for (1 ..^ $limit).reverse -> $bnum {
if "{$abs}.{$ext}{$bnum}".IO.e {
"{$abs}.{$ext}{$bnum}".IO.rename: "{$abs}.{$ext}{$bnum + 1}";
}
}
if $abs.IO.e {
if $limit > 0 {
$abs.IO.rename: "{$abs}.{$ext}1";
my $in = "{$abs}.{$ext}1".IO.open :r :bin or note $! and return False;
my $out = $abs.IO.open :w :bin or note $! and return False;
my $buffer-size = 32768;
while my $buf = $in.read($buffer-size) { $out.write($buf) };
close $in;
close $out;
}
} else {
note "File not found: $abs" and return False;
}
$abs
}
backup $*PROGRAM-NAME;
backup 'myfile', :follow-symlinks, :limit(2), :ext('bak');