Skip to content

Instantly share code, notes, and snippets.

@mertonium
Created April 19, 2011 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mertonium/926721 to your computer and use it in GitHub Desktop.
Save mertonium/926721 to your computer and use it in GitHub Desktop.
Replicates all the dbs from one couch instance to another
<?php
// Config variables
define("FULL_OVERWRITE", true);
$source_couch = "http://yourcouchinstance.com";
$destination_couch = "http://yourothercouchinstance.com";
// Counter
$db_count = 0;
// Time check (we'll use this later)
$start_ts = time();
// Get all the dbs in the couch, clean them up and put them in an array
$all_dbs_str = `curl -X GET {$source_couch}/_all_dbs`;
$all_dbs = explode('","', str_replace(array('["','"]'), '', $all_dbs_str));
// Loop through each DB
foreach($all_dbs as $db) {
// Only copy the ones with the "phl_" prefix
if(strpos($db, 'phl_') !== false) {
$db = trim($db); // Extra cleaning
// Delete the given db
if(FULL_OVERWRITE) {
echo 'Deleting ' . $destination_couch . '/' . $db . "\n";
echo `curl -X DELETE {$destination_couch}/{$db}`;
echo "\n";
}
echo 'Creating ' . $destination_couch . '/' . $db . "\n";
echo `curl -X PUT {$destination_couch}/{$db}`;
echo "\n";
echo 'Replicating from ' . $source_couch .'/'. $db .' to ' . $destination_couch . '/' . $db . "\n";
echo `curl -X POST -d '{"source":"{$source_couch}/{$db}", "target":"{$db}"}' -H "Content-Type: application/json" {$destination_couch}/_replicate`;
echo "\n";
$db_counter++;
}
}
// Final time check
$end_ts = time();
// Figure time spent
$run_time = $end_ts - $start_ts;
// Print out summary
echo "====================\n";
echo 'Run finished in ' . $run_time .' seconds.' . "\n";
echo $db_counter . ' databases replicated.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment