Skip to content

Instantly share code, notes, and snippets.

@martinusadyh
Created December 12, 2014 12:59
Show Gist options
  • Save martinusadyh/6c4fe50c84be906bd61d to your computer and use it in GitHub Desktop.
Save martinusadyh/6c4fe50c84be906bd61d to your computer and use it in GitHub Desktop.
Script Backup dengan History
# http://kb.masterweb.com/beta/index.cgi/read/Artikel:Backup_filesystem_dengan_histori_menggunakan_rsync
#!/usr/bin/perl -w
use strict;
use Cwd qw(abs_path);
use POSIX;
use Time::Local;
# --- config
my $HISTORIES = [ -7, 4, 3 ];
# --- subs
sub esc {
local $_ = shift;
s/'/'"'"'/g;
"'$_'";
}
# --- main
@ARGV == 2 or die "Usage: $0 <src> <dest>\n";
my ( $src, $dst ) = @ARGV;
for ( $src, $dst ) {s!/$!!}
( -d $src ) or die "Src haruslah direktori yang sudah ada: $src\n";
system "mkdir -p " . esc("$dst/current");
( -d "$dst/current" ) or die "Gagal membuat creating $dst/current\n";
print "Membuat backup terbaru $src ke $dst/ ...\n";
system "nice -n19 rsync -av --del --force "
. "--link-dest "
. esc( abs_path("$dst") . "/current" ) . " "
. esc("$src/") . " "
. esc("$dst/.work/") . "\n";
warn "Perintah rsync tidak sukses, sebaiknya Anda periksa\n" if $?;
chdir($dst) or die "Tidak bisa chdir ke $dst: $!\n";
system "touch .current.timestamp";
my $now = time;
my @st = stat(".current.timestamp");
my $tstamp = POSIX::strftime( "%Y-%m-%d\@%H:%M:%S+00", gmtime( $st[9] || $now ) );
rmdir "current" or rename "current", "hist.$tstamp";
rename ".work", "current";
print "Menghapus histori backup yang sudah terlalu lama ...\n";
for my $level ( 1 .. @$HISTORIES ) {
my $is_highest_level = $level == @$HISTORIES;
my $prefix = "hist" . ($level == 1 ? "" : $level);
my $prefix_next_level = "hist" . ($level+1);
my $n = $HISTORIES->[$level-1];
my $moved = 0;
if ( $n > 0 ) {
print "Hanya menyimpan $n buah histori backup L$level ...\n";
my @f = reverse sort grep { !/\.work$/ } glob "$prefix.*";
my $any_tagged = ( grep {/t$/} @f ) ? 1 : 0;
for my $f ( @f[ $n .. @f - 1 ] ) {
my ( $st, $tagged ) = $f =~ /[^.]+\.(.+?)(t)?$/;
my $f2 = "$prefix_next_level.$st";
if (!$is_highest_level && !$moved && ( $tagged || !$any_tagged ) ) {
print "Memindahkan level histori backup: $f -> $f2\n";
system "mv " . esc($f) . " " . esc($f2);
$moved++;
if ( $f ne $f[0] ) {
my $e3 = esc( $f[0] );
system "mv $e3 ${e3}t";
}
} else {
print "Menghapus histori backup: $f ...\n";
system "nice -n19 rm -rf " . esc($f);
}
}
} else {
$n = -$n;
print "Hanya menyimpan histori backup L$level sd $n hari ...\n";
my @f = reverse sort grep { !/\.work$/ } glob "$prefix.*";
my $any_tagged = ( grep {/t$/} @f ) ? 1 : 0;
for my $f (@f) {
my ( $st, $tagged ) = $f =~ /[^.]+\.(.+?)(t)?$/;
my $f2 = "$prefix_next_level.$st";
my $t;
$st =~ /(\d\d\d\d)-(\d\d)-(\d\d)\@(\d\d):(\d\d):(\d\d)\+00/;
$t = timegm( $6, $5, $4, $3, $2 - 1, $1 ) if $1;
$st && $t or do {
print "Histori backup salah format: $f, diabaikan\n";
next;
};
if ( $t > $now ) {
print "Histori backup di masa depan? $f, diabaikan\n";
next;
}
my $delta = ( $now - $t ) / 86400;
if ( $delta > $n ) {
if (!$is_highest_level && !$moved && ( $tagged || !$any_tagged ) ) {
print "Memindahkan level histori backup: $f -> $f2\n";
system "mv " . esc($f) . " " . esc($f2);
$moved++;
if ( $f ne $f[0] ) {
my $e3 = esc( $f[0] );
system "mv $e3 ${e3}t";
}
} else {
print "Menghapus history backup: $f ...\n";
system "nice -n19 rm -rf " . esc($f);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment