Skip to content

Instantly share code, notes, and snippets.

@lord-otori
Last active August 23, 2017 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lord-otori/4670011 to your computer and use it in GitHub Desktop.
Save lord-otori/4670011 to your computer and use it in GitHub Desktop.
A script to export all views in a Couchbase server to files.
<?php
/*
* @author: Juan S. Simon
* @copyright: none, just please don't try to sell it, they'll laugh at you...
*
*/
if (php_sapi_name() != 'cli') {
exit("No web access allowed!" . PHP_EOL);
}
function color($str) {
return "\033[0;31m$str\033[0m";
}
if ($argc < 3) {//Require first 2 arguments
echo color("+-------------------------------------------------------------+") . PHP_EOL;
echo color("|") . " USAGE " . color("|") . PHP_EOL;
echo color("+-------------------------------------------------------------+") . PHP_EOL;
echo color("|") . " script.php user password " . color("|") . PHP_EOL;
echo color("|") . " By default the script will try to connect to localhost:8091 " . color("|") . PHP_EOL;
echo color("|") . " you can specify the address like this: " . color("|") . PHP_EOL;
echo color("|") . " script.php user password address " . color("|") . PHP_EOL;
echo color("+-------------------------------------------------------------+") . PHP_EOL;
exit();
}
$user = $argv[1];
$pass = $argv[2];
$addr = (isset($argv[3])) ? $argv : 'localhost';
$port = '8091';
$start_time = microtime(true);
$buv = 0;
$bud = 0;
$strlen = 0;
$errors = array();
$messages = array('There is no place like "Couchbase"!', 'Happy Coding!', 'Code smart, Code less...',);
function string_to_size($string, $size) {
return $string . str_repeat(' ', $size - strlen($string));
}
function fetch_object($url) {
try {
global $pass, $user, $errors;
if (!function_exists('curl_init')) {
exit('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$res = curl_exec($ch);
if ($res != FALSE) {
if (!$buckets_response = json_decode($res)) {
array_push($errors, $url . " replied: " . $res);
}
} else {
array_push($errors, "Failed to fetch the information, auth might have failed.");
}
curl_close($ch); //Close link to free memory
return $buckets_response;
} catch (Exception $exc) {
echo $exc->getTraceAsString() . PHP_EOL;
}
}
$url = 'http://' . $addr . ':' . $port . '/pools/default/buckets';
$buckets = array();
$buckets_response = fetch_object($url);
if (count($buckets_response) != 0)
foreach ($buckets_response as $key => $val) {
array_push($buckets, $val->name);
}
unset($buckets_response); //Clear buckets query response from memory
foreach ($buckets as $bucket) {
$ddoc = fetch_object('http://' . $addr . ':' . $port . '/pools/default/buckets/' . $bucket . '/ddocs');
foreach ($ddoc->rows as $row) {
$bud++;
$ddoc = 'views/' . $bucket . '/' . $row->doc->meta->id;
if (!file_exists($ddoc)) {
@mkdir($ddoc, 0777, true);
}
foreach ($row->doc->json->views as $view_name => $view) {
//[MAP]
if (isset($view->map)) {
$strlen += strlen($view->map);
file_put_contents($ddoc . '/' . $view_name . '.map.js', $view->map);
$buv++;
}
//[REDUCE]
if (isset($view->reduce)) {
$strlen += strlen($view->reduce);
file_put_contents($ddoc . '/' . $view_name . '.reduce.js', $view->reduce);
$buv++;
}
}
}
unset($ddoc);
}
$finish_time = microtime(true);
$file_directory = './views';
$output = exec('du -sk ' . $file_directory);
$filesize = trim(str_replace($file_directory, '', $output)) * 1024;
//[OUTPUT]
$result = (count($errors) == 0) ? 'Finished Correctly' : 'Failed Abruptly';
echo '+-------------------------------------------------------------------------------------------------------------+' . PHP_EOL;
echo '| Views Backup Script V2 |' . PHP_EOL;
echo '+-------------------------------------------------------------------------------------------------------------+' . PHP_EOL;
echo '| Result | ' . string_to_size($result, 88) . '|' . PHP_EOL;
echo '| Address | ' . string_to_size("$addr:$port", 88) . '|' . PHP_EOL;
echo '| Total Time | ' . string_to_size(substr($finish_time - $start_time, 0, 6) . " seconds", 88) . '|' . PHP_EOL;
echo '| Total Views | ' . string_to_size($buv . " Views", 88) . '|' . PHP_EOL;
echo '| Total Design Docs | ' . string_to_size($bud . " Design Docs", 88) . '|' . PHP_EOL;
echo '| Total Size | ' . string_to_size($filesize . " bytes (on disk)", 88) . '|' . PHP_EOL;
echo '| Random Message | ' . string_to_size($messages[array_rand($messages)], 88) . '|' . PHP_EOL;
if (count($errors) != 0) {
foreach ($errors as $key => $error) {
echo '| Error #' . ($key + 1) . ' | ' . string_to_size($error, 88) . '|' . PHP_EOL;
}
}
echo '+-------------------------------------------------------------------------------------------------------------+' . PHP_EOL;
?>
@eliyahud
Copy link

Line 28 should be the following, right?

$addr = (isset($argv[3])) ? $argv[3] : 'localhost';

@doodyparizada
Copy link

I published a repo that handles both exporting and importing couchbase views similar to the above code.
It handles both couchbase buckets and memcached buckets.

https://github.com/rounds/couchbase-dump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment