Skip to content

Commit

Permalink
Birthday!
Browse files Browse the repository at this point in the history
  • Loading branch information
rudionrails committed Jun 26, 2012
0 parents commit c4fc92d
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
79 changes: 79 additions & 0 deletions backup.js
@@ -0,0 +1,79 @@
/**
*
* MongoDB Backup Script
*
* Copyright (c) 2012, Rudolf Schmidt
* Released under the MIT license.
*
*/


/**
* Switch to the admin db and get the dbpath (usually
* passed as argument to he mongod process)
*/
var adminDb = db.getSisterDB( "admin" ),
dbPath = adminDb.runCommand( "getCmdLineOpts" ).parsed.dbpath || "/data/db";

/**
* This file is executed when copying files for the snapshot.
*
* If you are using another script or want to run it from a different
* locatin, please change it.
*
* @example Tar the data
* var date = new Date(),
* timestamp = [date.getFullYear(), ('0'+(date.getMonth()+1)).slice(-2), ('0'+date.getDate()).slice(-2)].join(),
* snapshot = ["tar", "-czvf", "mongodb-backup-"+ timestamp +".tar.gz", dbPath];
*
* @example Rsync the data
* var snapshot = ["rsync", "-avz", "--delete", dbPath, "/mnt/backups/mongodb"];
*/

var snapshot = ["rsync", "-avz", "--delete", dbPath, "/home/rudolf/backup"];


try {
/**
* Some pre-checks
*/
if ( rs.isMaster().ismaster ) {
throw "Connected to PRIMARY. Not going to perform a backup."

}

if ( db.currentOp().fsyncLock == 1 ) {
throw "Database is already locked. Not going to perform backup."

}

/**
* Actual Backup
*/
try {
// TODO: get profiling level and disable if necessary --R

print( "[INFO] Flushing data and locking for snapshot" );
var lock = adminDb.runCommand({ fsync:1, lock:1 });

if ( lock.ok == 0 ) {
throw "Could not obtain lock: "+ lock.toSource();
}

// execute the snapshot script
print( "[INFO] Performing snapshot" );
runProgram.apply(null, snapshot);

} catch( e ) {
print( "[ERROR] "+ e );

} finally {
print( "[INFO ] Releasing the lock" );
adminDb.$cmd.sys.unlock.findOne();

}
} catch( e ) {
print( "[ERROR] "+ e );

}

81 changes: 81 additions & 0 deletions compact.js
@@ -0,0 +1,81 @@
/**
*
* MongoDB maintenance script to compact all collections in all databases.
*
* Copyright (c) 2012, Rudolf Schmidt
* Released under the MIT license.
*
*/

try {

/**
* check if we are the master and step down if necessary
*/
if ( rs.isMaster().ismaster ) {
print( "[INFO] Connected to PRIMARY, going to step down..." );
rs.stepDown();

// After stepdown, connections are droped. Reconnect.
rs.isMaster();

// Let's wait for someone else to become the new PRIMARY
var count = 1;
while( rs.isMaster().ismaster || !rs.isMaster().primary ) {
if ( count < 20 ) {
print( "* No-one else is promoted to PRIMARY yet, going to sleep and try again..." );
sleep( 1000 ); // second

count++;
} else {
throw "No-one was promoted to master within "+ count +" tries"
}
}
}

// enable to do stuff on the slave
// rs.slaveOk();

/**
* Compact from here onwards
*/
print( "[INFO] Performing compact:" );
// db.getSisterDB( "databse-name" )[ "collection-name" ].runCommand( "compact" )

var dRegex = /^(admin|local|test)/,
cRegex = /^(system)/,
dNames = db.getMongo().getDBNames();

for( i in dNames ) {
var dName = dNames[i],
d = db.getSisterDB( dName );

if( dRegex.test(dName) ) continue; // skip

print( " "+ dName );

// iterate all collections on the current db and compact
try {
var cNames = d.getCollectionNames();

for( j in cNames ) {
var cName = cNames[j];

if( cRegex.test(cName) ) continue; // skip

print( " - "+ cName );

// d.runCommand({ compact : cName, slaveOk : true });
d.runCommand({ compact : cName, slaveOk : true });
// d.runCommand({ compact : cName, slaveOk : true });
}
} catch( e ) {
print( "[ERROR]"+ e )
}
}


} catch( e ) {
print( "[ERROR] " + e );

}

0 comments on commit c4fc92d

Please sign in to comment.