@@ -8,7 +8,7 @@ use Cpanel::JSON::XS qw( decode_json encode_json );
88use DateTime ();
99use IO::Zlib ();
1010use Log::Contextual qw( :log :dlog ) ;
11- use MetaCPAN::Types::TypeTiny qw( Bool Int Path Str ) ;
11+ use MetaCPAN::Types::TypeTiny qw( Bool Int Path Str CommaSepOption ) ;
1212use Moose;
1313use Try::Tiny qw( catch try ) ;
1414
@@ -22,6 +22,15 @@ has batch_size => (
2222 ' Number of documents to restore in one batch, defaults to 100' ,
2323);
2424
25+ has index => (
26+ reader => ' _index' ,
27+ is => ' ro' ,
28+ isa => CommaSepOption,
29+ coerce => 1,
30+ default => ' cpan' ,
31+ documentation => ' ES indexes to backup, defaults to "cpan"' ,
32+ );
33+
2534has type => (
2635 is => ' ro' ,
2736 isa => Str,
@@ -61,34 +70,38 @@ sub run {
6170 return $self -> run_restore if $self -> restore;
6271
6372 my $es = $self -> es;
64- $self -> index -> refresh;
65-
66- my $filename = join ( ' -' ,
67- DateTime-> now-> strftime(' %F' ),
68- grep {defined } $self -> index -> name,
69- $self -> type );
70-
71- my $file = $self -> home-> child( qw( var backup) , " $filename .json.gz" );
72- $file -> parent-> mkpath unless ( -e $file -> parent );
73- my $fh = IO::Zlib-> new( " $file " , ' wb4' );
74-
75- my $scroll = $es -> scroll_helper(
76- index => $self -> index -> name,
77- $self -> type ? ( type => $self -> type ) : (),
78- size => $self -> size,
79- fields => [qw( _parent _source) ],
80- scroll => ' 1m' ,
81- body => {
82- sort => ' _doc' ,
83- },
84- );
8573
86- log_info { ' Backing up ' , $scroll -> total, ' documents' };
74+ for my $index ( @{ $self -> _index } ) {
75+
76+ $self -> es-> indices-> refresh( index => $index );
8777
88- while ( my $result = $scroll -> next ) {
89- print $fh encode_json($result ), $/ ;
78+ my $filename = join ( ' -' ,
79+ DateTime-> now-> strftime(' %F' ),
80+ grep {defined } $index ,
81+ $self -> type );
82+
83+ my $file = $self -> home-> child( qw( var backup) , " $filename .json.gz" );
84+ $file -> parent-> mkpath unless ( -e $file -> parent );
85+ my $fh = IO::Zlib-> new( " $file " , ' wb4' );
86+
87+ my $scroll = $es -> scroll_helper(
88+ index => $index ,
89+ $self -> type ? ( type => $self -> type ) : (),
90+ size => $self -> size,
91+ fields => [qw( _parent _source) ],
92+ scroll => ' 1m' ,
93+ body => {
94+ sort => ' _doc' ,
95+ },
96+ );
97+
98+ log_info { ' Backing up ' , $scroll -> total, ' documents' };
99+
100+ while ( my $result = $scroll -> next ) {
101+ print $fh encode_json($result ), $/ ;
102+ }
103+ close $fh ;
90104 }
91- close $fh ;
92105 log_info {' done' };
93106}
94107
0 commit comments