Checking in changes prior to tagging of version 2.30.
[MogileFS-Utils.git] / moglistfids
blobb11f2d1742dfb8f915b4b6a44be1086e1755826f
1 #!/usr/bin/perl
3 =head1 NAME
5 moglistfids -- Iterate fid/key data from a MogileFS installation
7 =head1 DESCRIPTION
9 Example utility for pulling all file data out of a MogileFS installation.
10 Utilities like this can be built on for creating backup systems,
11 Mogile<->Mogile syncronization systems, or Mogile->S3 syncronization.
13 This method is only a way of pulling new files which have existed since the
14 last time it was checked, as there's no logging of deleted files.
16 =head1 OPTIONS
18 =over
20 =item --trackers=host1:7001,host2:7001
22 Use these MogileFS trackers to negotiate with.
24 =item --fromfid=<fid>
26 The highest numbered fid fetched the last time this utility was run.
28 =item --count=<count>
30 Numer of fids to inspect and return.
32 =back
34 =head1 AUTHOR
36 Dormando E<lt>L<dormando@rydia.net>E<gt>
38 =head1 BUGS
40 None known.
42 =head1 LICENSE
44 Licensed for use and redistribution under the same terms as Perl itself.
46 =cut
48 use strict;
49 use warnings;
51 use MogileFS::Admin;
52 use lib './lib';
53 use MogileFS::Utils;
55 my $util = MogileFS::Utils->new;
56 my $usage = "--trackers=host --fromfid=123 --count=5000";
57 my $c = $util->getopts($usage, qw/fromfid=i count=i/);
59 my $moga = MogileFS::Admin->new(hosts => $c->{trackers});
61 my $fromfid = $c->{fromfid} || 0;
62 my $count = $c->{count} || 100;
64 while ($count) {
65 # Try to fetch the max, but we will likely get less.
66 my $fids_chunk = $moga->list_fids($fromfid, $count);
67 if ($moga->errcode) {
68 die "Error listing fids: ", $moga->errstr, "\n";
70 my @fids = sort { $a <=> $b } keys %$fids_chunk;
71 last unless @fids;
72 $fromfid = $fids[-1];
73 $count -= @fids;
74 for my $fid (@fids) {
75 my $file = $fids_chunk->{$fid};
76 print "fid ", $fid, "\n";
77 for my $key (sort keys %$file) {
78 my $val = $file->{$key};
79 $val = _escape_url_string($val) if $key eq 'dkey';
80 print $key, " ", $val, "\n";
82 print "\n";
86 sub _escape_url_string {
87 my $str = shift;
88 $str =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg;
89 $str =~ tr/ /+/;
90 return $str;