Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 138 lines (101 sloc) 3.13 KB
#!/usr/bin/env perl
use common::sense;
use Store::CouchDB;
use Config::Tiny;
use Getopt::Long;
use Pod::Usage;
use File::Path qw(mkpath);
my $help;
my $man;
my $conf = '.couchdb';
my $dir;
my $from = pop(@ARGV);
GetOptions(
"config|c=s" => \$conf,
"export|e=s" => \$dir,
'help|?' => \$help,
"man" => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage(1) unless $from;
my @from = split(/\//, $from);
my $c = Config::Tiny->new();
$c = Config::Tiny->read( $conf );
die "Could not find configuration for " . $from[0]
unless $c->{$from[0]};
$dir = $c->{_}->{export_dir} unless $dir;
if(! -d $dir){
mkdir($dir);
if($@){
die "Could not create missing export directory: $@";
}
warn "Created export directory $dir\n";
}
my $couch = Store::CouchDB->new();
my $host = $from[0];
my $db = $from[1];
$couch->config( $c->{$host} );
$couch->db($db);
my $docs = $couch->get_doc({id => '_all_docs?startkey="_design"&endkey="_design0"'});
foreach my $row (@{$docs->{rows}}){
my $doc = $couch->get_doc({id => $row->{id}});
my ($_n, $name) = split(/\//, $doc->{_id}, 2);
my $views = delete $doc->{views};
print "saving $name\n";
foreach my $view (keys %{$views}){
my $vdir = $dir.'/'.$host.'/'.$db.'/'.$name;
if(! -d $vdir){
mkpath($vdir);
if($@){
die "Could not create missing export directory: $@";
}
warn "Created export directory $vdir\n";
}
open(FH, '>', $vdir.'/'.$view.'.view') || die "Could not open file: $@";
print FH "[map]\n";
print FH $views->{$view}->{map};
if($views->{$view}->{reduce}){
print FH "\n[reduce]\n";
print FH $views->{$view}->{reduce};
}
print FH "\n[params]\n";
print FH "name = $view\n";
foreach my $ky (keys %{$doc}) {
print FH $ky .' = '. $doc->{$ky} ."\n";
}
close FH;
}
}
=head1 NAME
couch_dump_views - dump all views of a couchDB database
=head1 VERSION
Version 0.1
=head1 SYNOPSIS
This is a simple copy script that copies one document to a new one
couch_dump_views [options] [host/db]
-c configuration file
-e export directory
-? brief help
-man long help
the host part in the two copy arguments have to be config sections
in the config file. In the config file the definition looks like
this:
[pumpkin]
host = 127.0.0.1
port = 5985
so the command line would look like this:
couch_dump_views pumpkin/log
=head1 AUTHOR
Lenz Gschwendtner, C<< <lenz@springtimesoft.com> >>
=head1 BUGS
Please report any bugs or feature requests to C<< <lenz@springtimesoft.com> >>
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc couch_dump_views
=head1 ACKNOWLEDGEMENTS
=head1 COPYRIGHT & LICENSE
Copyright 2009 Lenz Gschwendtner, springtimesoft LTD, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut