Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 138 lines (101 sloc) 3.204 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#!/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
Something went wrong with that request. Please try again.