Created
November 1, 2012 07:31
-
-
Save pyh/3992325 to your computer and use it in GitHub Desktop.
Removes a defined domain from MogileFS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Efficient script to remove all files from MogileFS for defined domain | |
# Use with extreme caution... | |
use warnings; | |
use strict; | |
use MogileFS::Server; | |
MogileFS::Config->load_config; | |
my $dbh = Mgd::get_store()->dbh; | |
my $domain = "somedomain.com"; | |
my ($dmid) = $dbh->selectrow_array("SELECT dmid FROM domain WHERE namespace = ?", undef, $domain); | |
if(!$dmid){ | |
print "Couldn't get dmid for: $domain\n"; | |
exit 1; | |
} | |
# logging | |
open(TXT, ">>/root/delete_log.txt"); | |
select(TXT); | |
$|++; | |
print TXT localtime(). " starting\n"; | |
select(STDOUT); | |
my $is_ok = 0; | |
while( my $files = $dbh->selectall_arrayref( "SELECT fid, dkey, length FROM file WHERE dmid = ? LIMIT 500", undef, $dmid ) ) { | |
if (! $is_ok ) { | |
print "Going to remove files like this: (not including everything, this is just first 20)\n"; | |
print map{ "$_->[1]\n" } (@{$files}[0..20]); | |
print "Please write YES if you want to proceed: "; | |
while( <STDIN> ){ | |
if ( $_ eq "YES\n" ) { | |
$is_ok = 1; | |
last; | |
} else { | |
chomp; | |
print "$_ ne YES. try again or ctrl+c to abort: "; | |
} | |
} | |
} | |
while( ($dbh->selectrow_array("SELECT COUNT(*) FROM file_to_delete2"))[0] > 10000 ) { | |
print "sleeping\n"; | |
sleep(2); | |
} | |
foreach my $file (@{$files}) { | |
my $fid = $file->[0]; | |
printf(TXT "%s %s %s\n", @{$file} ); | |
my $retval; | |
eval { $retval = $dbh->do("INSERT INTO file_to_delete2 (fid, nexttry) VALUES(?,?)", undef, $fid, time() ) }; | |
if($@) { | |
if ( $@ =~ /Deadlock found when trying to get lock; try restarting transaction/ ){ | |
print "Deadlock at $fid skipping for now\n"; | |
next; | |
}else{ | |
die($@); | |
} | |
} | |
if ( $retval ){ | |
if ( $dbh->do("DELETE FROM file WHERE fid=? LIMIT 1", undef, $fid ) ) { | |
# success... file is gone | |
print "."; | |
} else { | |
die("Couldn't remove from file ($fid): " . $dbh->errstr . "\n"); | |
} | |
} | |
} | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment