line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Redis::Dump; |
3
|
|
|
|
|
|
|
|
4
|
8
|
|
|
8
|
|
1210399
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
with 'MooseX::Getopt'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Redis 1.904; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ABSTRACT: It's a simple way to dump and backup data from redis-server |
10
|
|
|
|
|
|
|
our $VERSION = '0.016'; # VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has _conn => ( |
13
|
|
|
|
|
|
|
is => 'ro', |
14
|
|
|
|
|
|
|
isa => 'Redis', |
15
|
|
|
|
|
|
|
init_arg => undef, |
16
|
|
|
|
|
|
|
lazy => 1, |
17
|
|
|
|
|
|
|
default => sub { |
18
|
|
|
|
|
|
|
my $tconf = shift; |
19
|
|
|
|
|
|
|
my $tconn = Redis->new( server => $tconf->server ); |
20
|
|
|
|
|
|
|
$tconn->select( $tconf->database); |
21
|
|
|
|
|
|
|
$tconn |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _get_keys { |
26
|
|
|
|
|
|
|
my $self = shift; |
27
|
|
|
|
|
|
|
my $filter = $self->filter; |
28
|
|
|
|
|
|
|
return $self->_conn->keys("*$filter*") if $filter; |
29
|
|
|
|
|
|
|
return $self->_conn->keys("*"); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _get_type_and_filter { |
33
|
|
|
|
|
|
|
my ( $self, $key ) = @_; |
34
|
|
|
|
|
|
|
return if $self->has_filter and not $key =~ $self->filter; |
35
|
|
|
|
|
|
|
my $type = $self->_conn->type($key); |
36
|
|
|
|
|
|
|
return if @{ $self->type } and not grep {/^$type/} @{ $self->type }; |
37
|
|
|
|
|
|
|
return $type; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _get_value { |
41
|
|
|
|
|
|
|
my ( $self, $key, $type ) = @_; |
42
|
|
|
|
|
|
|
return $self->_conn->get($key) if $type eq 'string'; |
43
|
|
|
|
|
|
|
return $self->_conn->lrange( $key, 0, -1 ) if $type eq 'list'; |
44
|
|
|
|
|
|
|
return $self->_conn->smembers($key) if $type eq 'set'; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
if ( $type eq 'zset' ) { |
47
|
|
|
|
|
|
|
my %hash; |
48
|
|
|
|
|
|
|
my @zsets = $self->_conn->zrange( $key, 0, -1, 'withscores' ); |
49
|
|
|
|
|
|
|
for ( my $loop = 0; $loop < scalar(@zsets) / 2; $loop++ ) { |
50
|
|
|
|
|
|
|
my $value = $zsets[ $loop * 2 ]; |
51
|
|
|
|
|
|
|
my $score = $zsets[ ( $loop * 2 ) + 1 ]; |
52
|
|
|
|
|
|
|
$hash{$score} = $value; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
return [ {%hash} ]; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
if ( $type eq 'hash' ) { |
58
|
|
|
|
|
|
|
my %hash; |
59
|
|
|
|
|
|
|
foreach my $item ( $self->_conn->hkeys($key) ) { |
60
|
|
|
|
|
|
|
$hash{$item} = $self->_conn->hget( $key, $item ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
return {%hash}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _get_values_by_keys { |
67
|
|
|
|
|
|
|
my $self = shift; |
68
|
|
|
|
|
|
|
my %keys; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
foreach my $key ( $self->_get_keys ) { |
71
|
|
|
|
|
|
|
my $type = $self->_get_type_and_filter($key) or next; |
72
|
|
|
|
|
|
|
my $show_name = $key; |
73
|
|
|
|
|
|
|
$show_name .= " ($type)" if $self->showtype; |
74
|
|
|
|
|
|
|
$keys{$show_name} = $self->hidevalues ? '' : $self->_get_value( $key, $type ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
return %keys; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub run { |
82
|
|
|
|
|
|
|
my $self = shift; |
83
|
|
|
|
|
|
|
return $self->_get_values_by_keys; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
has server => ( |
88
|
|
|
|
|
|
|
is => 'ro', |
89
|
|
|
|
|
|
|
isa => 'Str', |
90
|
|
|
|
|
|
|
default => '127.0.0.1:6379', |
91
|
|
|
|
|
|
|
documentation => 'Host:Port of redis server (ex. 127.0.0.1:6379)' |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
has database => ( |
96
|
|
|
|
|
|
|
is => 'ro', |
97
|
|
|
|
|
|
|
isa => 'Int', |
98
|
|
|
|
|
|
|
default => 0, |
99
|
|
|
|
|
|
|
documentation => 'Database used in a multi-database setup' |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
has filter => ( |
104
|
|
|
|
|
|
|
is => 'ro', |
105
|
|
|
|
|
|
|
isa => 'Str', |
106
|
|
|
|
|
|
|
default => '', |
107
|
|
|
|
|
|
|
predicate => 'has_filter', |
108
|
|
|
|
|
|
|
documentation => 'String to filter keys stored in redis server' |
109
|
|
|
|
|
|
|
); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
has type => ( |
113
|
|
|
|
|
|
|
is => 'ro', |
114
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
115
|
|
|
|
|
|
|
default => sub { [] }, |
116
|
|
|
|
|
|
|
predicate => 'has_type', |
117
|
|
|
|
|
|
|
documentation => 'Show just this type of key', |
118
|
|
|
|
|
|
|
); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
has showtype => ( |
122
|
|
|
|
|
|
|
is => 'ro', |
123
|
|
|
|
|
|
|
isa => 'Bool', |
124
|
|
|
|
|
|
|
default => 0, |
125
|
|
|
|
|
|
|
documentation => 'If you want to show type with key name.' |
126
|
|
|
|
|
|
|
); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
has hidevalues => ( |
130
|
|
|
|
|
|
|
is => 'ro', |
131
|
|
|
|
|
|
|
isa => 'Bool', |
132
|
|
|
|
|
|
|
default => 0, |
133
|
|
|
|
|
|
|
documentation => 'Hide values of keys' |
134
|
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
1; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=pod |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 NAME |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Redis::Dump - It's a simple way to dump and backup data from redis-server |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 VERSION |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
version 0.016 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SYNOPSIS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
use Redis::Dump; |
153
|
|
|
|
|
|
|
use Data::Dumper; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
my $dump = Redis::Dump->new({ server => '127.0.0.6379', filter => 'foo' }); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
print Dumper( \$dump->run ); |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 DESCRIPTION |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
It's a simple way to dump data from redis-server in JSON format or any format |
162
|
|
|
|
|
|
|
you want. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 COMMAND LINE API |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This class uses L<MooseX::Getopt> to provide a command line api. The command line options map to the class attributes. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 METHODS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 new_with_options |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Provided by L<MooseX::Getopt>. Parses attributes init args from @ARGV. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 run |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Perfomas the actual dump. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 server |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Host:Port of redis server, example: 127.0.0.1:6379. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 database |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
If you want to select another database than default which is 0. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 filter |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
String to filter keys stored in redis server. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 type |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
If you want to get just some types of keys. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
It can be: lists, sets, hashs, strings, zsets |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 showtype |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
If you want to show type with key name. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 hidevalues |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Hide value of keys. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 DEVELOPMENT |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Redis::Dump is a open source project for everyone to participate. The code repository |
209
|
|
|
|
|
|
|
is located on github. Feel free to send a bug report, a pull request, or a |
210
|
|
|
|
|
|
|
beer. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
L<http://www.github.com/maluco/Redis-Dump> |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 SEE ALSO |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
L<Redis::Dump::Restore>, L<App::Redis::Dump>, L<App::Redis::Dump::Restore> |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 CREDITS |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=over 4 |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item * Rémi Paulmier |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=back |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 SUPPORT |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
perldoc Redis::Dump |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
You can also look for information at: |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=over 4 |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Redis-Dump> |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Redis-Dump> |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=item * CPAN Ratings |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Redis-Dump> |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item * Search CPAN |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Redis-Dump> |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=back |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 AUTHOR |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Thiago Rondon <thiago@nsms.com.br> |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Thiago Rondon. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
263
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=cut |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
__END__ |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
|