Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c4fc92d
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
|
||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
|
||
} |