root/trunk/brackup-target

Revision 161, 3.9 kB (checked in by bradfitz, 2 years ago)

'prune' and 'gc' commands commands for both Amazon
and Filesystem targets. from Alessandro Ranellucci <aar@…>.

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3=head1 NAME
4
5brackup-target - Manage your backup targets
6
7=head1 SYNOPSIS
8
9 $ brackup-target [opts] <target_name> list_backups
10 $ brackup-target [opts] <target_name> get_backup <backup_file>
11 $ brackup-target [opts] <target_name> get_backups
12 $ brackup-target [opts] <target_name> delete_backup <backup_file>
13 $ brackup-target [opts] <target_name> prune   # remove old backups
14 $ brackup-target [opts] <target_name> gc      # run garbage collector
15
16=head2 OPTIONS
17
18=over 4
19
20=item --dest=DIR
21
22Destination to write files to.  Defaults to current working directory.
23
24=item --verbose|-v
25
26Be verbose with status.
27
28=item --dry-run
29
30Do not actually execute write operations.
31
32=item --keep-backups
33
34To be used in combination with the I<prune> command. This overrides the
35I<keep_backups> option specified in the configuration file.
36
37=back
38
39=head1 WARRANTY
40
41Brackup is distributed as-is and comes without warranty of any kind,
42expressed or implied.  We aren't responsible for your data loss.
43
44=head1 SEE ALSO
45
46brackup-restore
47
48=head1 AUTHOR
49
50Brad Fitzpatrick E<lt>brad@danga.comE<gt>
51
52Copyright (c) 2006-2007 Six Apart, Ltd. All rights reserved.
53
54This module is free software. You may use, modify, and/or redistribute this
55software under the terms of same terms as perl itself.
56
57=cut
58
59use strict;
60use warnings;
61use Getopt::Long;
62
63use Cwd;
64use FindBin qw($Bin);
65use lib "$Bin/lib";
66
67use Brackup;
68
69my $config_file;
70my $destdir;
71my $opt_help;
72my $opt_verbose;
73my $opt_keep_backups;
74my $opt_dryrun;
75usage() unless
76    GetOptions(
77               'verbose'  => \$opt_verbose,
78               'dest=s'   => \$destdir,
79               'config=s' => \$config_file,
80               'keep-backups=i' => \$opt_keep_backups,
81               'dry-run'   => \$opt_dryrun,
82               'help'     => \$opt_help,
83               );
84
85if ($destdir) {
86    chdir $destdir or die "Failed to chdir to $destdir: $!\n";
87}
88
89if ($opt_help) {
90    eval "use Pod::Usage;";
91    Pod::Usage::pod2usage( -verbose => 1, -exitval => 0 );
92    exit 0;
93}
94
95my $config = eval { Brackup::Config->load($config_file) } or
96    usage($@);
97
98my $target_name = shift or usage();
99my $cmd_name    = shift or usage();
100
101my $target = eval { $config->load_target($target_name); } or
102    usage($@);
103
104my $code = __PACKAGE__->can("CMD_$cmd_name") or
105    usage("Unknown/unimplemented command.");
106
107exit($code->() ? 0 : 1);
108
109
110sub CMD_list_backups {
111    foreach my $si ($target->backups) {
112        printf("%-35s %-20s %10d\n",
113               $si->filename,
114               $si->time,
115               $si->size);
116    }
117    return 1;
118}
119
120sub CMD_get_backup {
121    my $name = shift @ARGV or
122        die "get_backup requires a filename to download";
123    $target->get_backup($name)
124                or die "Failed to retrieve backup $name\n";
125}
126
127sub CMD_get_backups {
128    foreach my $si ($target->backups) {
129        my $size = $si->size;
130        my $name = $si->filename;
131        no warnings 'uninitialized';
132        if (-s "$name.brackup" == $size || -s "$name.brackup.orig" == $size) {
133            debug("Skipping $name; already have it");
134            next;
135        }
136        debug("Fetching $name");
137        $target->get_backup($si->filename);
138    }
139}
140
141sub CMD_delete_backup {
142    my $name = shift @ARGV or
143        die "delete_backup requires a filename to download";
144    $target->delete_backup($name)
145                or die "Failed to delete backup $name\n";
146}
147
148sub CMD_prune {
149    my $removed_count = $target->prune( keep_backups => $opt_keep_backups,
150                                        dryrun => $opt_dryrun);
151    debug("$removed_count backups removed from target");
152}
153
154sub CMD_gc {
155    my $removed_chunks = $target->gc(dryrun => $opt_dryrun);
156    debug("$removed_chunks chunks removed from target");
157}
158
159sub debug {
160    my $msg = shift;
161    return unless $opt_verbose;
162    warn "$msg\n";
163}
164
165
166sub usage {
167    my $why = shift || "";
168    if ($why) {
169        $why =~ s/\s+$//;
170        $why = "Error: $why\n\n";
171    }
172    die "${why}brackup-target <target> <cmd> [...]\nbrackup-target --help\n";
173}
Note: See TracBrowser for help on using the browser.