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