-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestore_collection
executable file
·134 lines (106 loc) · 3.09 KB
/
restore_collection
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
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use lib qw(
lib
);
# NAME: Restore Redis::CappedCollection collection dump.
use bytes;
use Getopt::Long qw(
GetOptions
);
use JSON::XS ();
use Try::Tiny;
use Redis::CappedCollection qw(
$DEFAULT_SERVER
$DEFAULT_PORT
);
my ( $collection, $dump_fh, $json ) = setup();
my @data_fields = qw(
list_id
data_id
data
data_time
);
my $number_data_fields = scalar @data_fields;
while ( my $encoded = <$dump_fh> ) {
chomp $encoded;
my $error;
my $decoded = try {
$json->decode( $encoded );
} catch {
$error = $_;
warn "failed to decode JSON ($error): $encoded";
};
next if $error;
my @data_info;
foreach my $key ( @data_fields ) {
my $value = delete $decoded->{ $key };
push( @data_info, $value ) if defined $value;
}
my $no_complete_information = scalar( @data_info ) != $number_data_fields;
if ( $no_complete_information || %$decoded ) {
warn "Invalid record: $encoded";
next if $no_complete_information;
}
$collection->insert( @data_info );
}
close $dump_fh;
exit 0;
sub setup {
my ( $help, $host, $port, $collection_name, $dump_file );
$host = $DEFAULT_SERVER;
$port = $DEFAULT_PORT;
my $ret = GetOptions(
'host=s' => \$host,
'port=i' => \$port,
'collection=s' => \$collection_name,
'dump-file=s' => \$dump_file,
"help|?" => \$help,
);
if ( !$ret || $help )
{
say <<"HELP";
Usage: $0 [--host="..."] [--port=...] --collection="..." [--dump-file="..."] [--help]
Connect to redis server, creates the specified Redis::CappedCollection collection from specified file or STDIN instead.
Use default settings and 'older_allowed => 1'.
Options:
--help
Display this help and exit.
--host="..."
The server should contain an IP address of Redis server.
If the server is not provided, '${DEFAULT_SERVER}' is used as the default for the Redis server.
--port=N
The server port.
Default ${DEFAULT_PORT} (the default for the Redis server).
--collection="..."
The collection name.
--dump-file="..."
Path to previously created dump file.
If not specified, the STDIN used.
HELP
exit 1;
}
die "Error: Redis::CappedCollection collection name required, use --collection to specify\n"
unless $collection_name;
my $redis_server = "$host:$port";
my $collection = try {
Redis::CappedCollection->create(
redis => $redis_server,
name => $collection_name,
older_allowed => 1,
);
} catch {
my $error = $_;
die "Error: Redis::CappedCollection collection '$collection_name' not created on '$redis_server'.\n$error\n";
};
my $dump_fh;
if ( $dump_file ) {
open( $dump_fh, '<', $dump_file ) or die "cannot open < $dump_file: $!";
} else {
$dump_fh = *STDIN;
}
my $json = JSON::XS->new;
return( $collection, $dump_fh, $json );
}