CLI-Startup

 view release on metacpan or  search on metacpan

examples/rs  view on Meta::CPAN

use Hash::Merge qw{ merge };

our $VERSION = 0.1;

# Command-line options
my $optspec = {
    'archive!'     => 'Use archive mode--see manpage for rsync',
    'checksum'     => 'Use checksums instead of file times',
    'compress!'    => 'Enable compression',
    'delete'       => 'Delete extraneous files from the destination',
    'dest=s'       => 'Specify the destination of the copy or backup',
    'devices!'     => 'Copy device files. Implied by --archive',
    'dry-run'      => 'Just print what files would be copied; do not copy',
    'exclude=s@'   => 'Do not copy files matching this (these) pattern(s)',
    'group!'       => 'Preserve group ownership. Implied by --archive',
    'include=s@'   => 'Do not exclude files matching this (these) pattern(s)',
    'links!'       => 'Copy symbolic links as symbolic links. Implied by --archive',
    'owner!'       => 'Preserve file owners (super-user only). Implied by --archive',
    'perms!'       => 'Preserve file permissions. Implied by --archive',
    'recursive!'   => 'Descend into sub-directories. Implied by --archive',
    'restore'      => 'Use the destination as the source, and vice versa',

examples/rs  view on Meta::CPAN

        dest   => $dest,
        outfun => sub { select STDOUT; $| = 1; print $_[0] },
        errfun => sub { select STDERR; $| = 1; print $_[0] },
    }) or $app->warn("Rsync failed for $source -> $dest: $!");

} while @ARGV;

__END__
=head1 NAME

rs - Wrapper for backups using rsync

=head1 SYNOPSIS

  rs                        ;# Use default settings from $HOME/.rsrc
  rs [options] [module ...] ;# Backup the named modules

=head1 DESCRIPTION

The C<rs> command is a simple wrapper around C<rsync> that provides three basic services:

examples/rs  view on Meta::CPAN

"modules."

=over

=item

A combination of options lets you invent custom "modes."

=item

A combination including C<--src> and C<--dest> helps you make routine backups.

=back

=back

=head2 Default Options

The default options are convenient for doing ad hoc copies using rsync with default
options. It lets you say this:

  rs --src $HOME --dest backup:homedir/

instead of this:

  rsync --archive --compress --delete --rsh ssh \
        --src $HOME/ --dest backup:homedir/

If you usually use the latter combination of options, as I do, then
C<rs> can at least save you some typing.

=head2 Named Modules

=head3 As Command Names

Named "modules" are groupings of options in the config file, C<$HOME/.rsrc> by
default. The C<rsync> command supports the C<--archive> option, which represents
the grouping:

  --recursive --links --perms --times --group --owner --devices --specials

Using C<rs>, you can define a new grouping called "backup" which adds additional
options, by putting the following in your config file:

  [backup]
  archive=1
  compress=1
  delete=1
  rsh=ssh -2 -c arcfour -l backup_user -p 2222 -y -o BatchMode=yes

Now you can make a backup of your home directory simply by issuing the command:

  rs backup --src $HOME/ --dest backup:homedir/

=head3 As Backup Tasks

If a grouping in your config file includes C<--src> and C<--dest> options, then
you can make a backup of a specific folder to a specific destination without
remembering combinations of options. For example, you can put this in your config
file:

  [documents]
  delete=1
  exclude=*.tmp, *.bak, tmp/
  src=/home/USERNAME/Documents/
  dest=backup:Documents/

And now you can backup your C<Documents> folder by issuing the command:

  rs documents

You can define multiple backup targets in this way, and then do all your
nightly backups in a single command, like this:

  rs documents music pictures

=head3 Restoring from Backup

C<rs> also adds the C<--restore> option, which reverses the role of C<--src>
and C<--dest>. If you're using those options directly, you probably don't
want to use C<--restore> option to swap them, since that will only confuse
you. If you're using modules defined in the config file, however, and you
think of those modules as backups, then C<--restore> probably does exactly
what you think it does.

=head1 OPTIONS

=head2 --archive

Use C<rsync> in "archive" mode; see the manpage for L<rsync>. Defaults
to C<true>. Can be negated by setting the C<--no-archive> option.

=head2 --checksum

examples/rs  view on Meta::CPAN

looking at the modification time and file size. Turned off by default.

=head2 --compress

Use compression; defaults to C<true>.  Disable compression entirely
by using the C<--no-compress> option.

=head2 --delete

Delete extraneous files in the destination folders. I<Not> enabled
by default, but for backup purposes you probably want to enable it.

=head2 --dest [ HOST:DIRECTORY | HOST: | DIRECTORY ]

Local or remote directory to copy I<to>. Mandatory unless one or more
modules are specified on the command line.

=head2 --devices

Preserve device files (super-user only). Implied by C<--archive>,
which is enabled by default, so this option is unset by default.

examples/rs  view on Meta::CPAN


=head2 --recursive

Recurse into sub-directories. Implied by C<--archive>, which is
enabled by default, so this option is unset by default. It can be
specifically disabled using the C<--no-recursive> option.

=head2 --restore

Reverse the roles of C<--src> and C<--dest>. If you think of C<rs>
as making a backup, then C<--restore> does just what you think it
does: it pulls from the backup destination and overwrites the
file or directory that you're normally backing up.

=head2 --rsh [command]

Command to use for remote access. Defaults to C<ssh>. A good
choice for performance reasons is C<ssh -c arcfour>.

=head2 --rsync-path [path]

Path to run C<rsync> on the remote host.

t/write_rcfile.t  view on Meta::CPAN

        'hash=s%'       => 'A hash option',
    },
};

# Initialize an app object for which the defaults match the
# current settings.
my $app = CLI::Startup->new($options);
lives_ok { $app->init } "Loaded perl-format rc file.";
is_deeply $app->get_config, $config, "Contents of rc file are correct.";

# Make a backup of the rc file.
ok move($rcfile, "$rcfile.orig"), "Wrote backup of original perl-format RC file";

# Now create a new config file for each file type, if available, and test
# that it matches expectations.
for my $format ( sort keys %$libs )
{
    SKIP: {
        eval "use $libs->{$format}";
        skip( "Skipping $format format: $libs->{$format} is not installed", 1 ) if $@;

        # Restore from backup
        ok copy("$rcfile.orig", $rcfile), "Copied original perl-format RC file.";

        # Load the file
        eval {
            local @ARGV = ( '--rcfile', $rcfile, '--rcfile-format', $format, '--write-rcfile' );
            my $app1 = CLI::Startup->new($options);

            exits_zero { $app1->init } "Wrote ${format}-format rc file.";
            is_deeply $app1->get_config, $config, "Settings are correct.";
        };



( run in 1.382 second using v1.01-cache-2.11-cpan-49f99fa48dc )