App-sibs
view release on metacpan or search on metacpan
Revision history for perl distribution App-sibs
0.17 2015-05-11T18:25:06+0200
- Add support for running backup locally
0.16 2014-09-30T22:19:43Z
- Made ssh-keygen a lot safer
0.15 2014-04-13T18:07:28Z
- Add "version" action
0.14 2014-04-13T18:04:32Z
- Will touch backup directory afterwards for correct timestamp.
- Will remove destination directory to avoid old junk
- Add support for custom backup directory name. Example: $(sibs --format
%H:%M:%S)
0.13 2013-10-31T20:09:36Z
- Fix missing dep: URI
0.12 2013-10-26T16:39:04Z
- Fix ssh_dir initialization
0.11 2013-10-26T16:34:55Z
- Fix $ENV{HOME} in unittests
0.10 2013-10-24T20:06:03Z
- Can create ssh key
- Can create config file
- Can run backup
{
"abstract" : "Simple incremental backup system",
"author" : [
"Jan Henning Thorsen <jhthorsen@cpan.org>"
],
"dynamic_config" : 1,
"generated_by" : "ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.150001",
"license" : [
"artistic_2"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
---
abstract: 'Simple incremental backup system'
author:
- 'Jan Henning Thorsen <jhthorsen@cpan.org>'
build_requires:
Test::More: '0.88'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.150001'
license: artistic_2
meta-spec:
NAME
App::sibs - Simple incremental backup system
VERSION
0.17
DESCRIPTION
"sibs" create backup from your computer to a destination server, using
"rsync".
* Setup
First you need to "setup" sibs. The setup process will create a SSH
key which is uploaded to the "destination" server. The key is
created using "ssh-keygen" and uploaded using "ssh" and "perl".
In addition, this step will create or update a config file. The
default config file is ".sibs.conf" in $HOME, but you can also
specify your own path.
* Backup
The second step will create the actual backup. This step can be
automated in a cronjob, because the key created in the first step
will not require any password to log into the remote backup server.
Example crontab:
0 */4 * * * /usr/local/bin/sibs backup 2>/dev/null 1>/dev/null
This will create a backup every four hours. The backup will be
"full" each time and kept for a month. Even so, it probably won't
take up too much space, since all the files are hard linked, meaning
an unchanged file will take up the same disk space for each backup.
SYNOPSIS
$ sibs <action> <options> <config file>
$ sibs setup
$ sibs backup
* action: setup
Used to set up the inital config file.
* action: backup
Used to take backup.
* action: man
Used to see this help text.
* options: --verbose, --silent
Adjust log level.
* config file: Path to "CONFIG FILE".
CONFIG FILE
The config file is in perl format, and read by perl. This means that any
perl code that exists in the file will be evaluated by perl. The return
value need to be a hash with the config. Example:
{
format => '%d-%H', # default
exclude => [qw( .cache .gvfs Downloads Dropbox Trash )],
source => [qw( /home/username )],
destination => "rsync://login-username@my.server.com/var/backup/username",
}
Defaults:
* format: %d-%H
The "format" will name the destination folder. See "FORMAT" in
POSIX::strftime::GNU for details.
* exclude: .cache, .gvfs, Downloads, Dropbox, Trash
Which files/directories to exclude.
* source: $HOME
Which directory to backup.
Default "exclude" may change in future release.
COPYRIGHT
This program is free software, you can redistribute it and/or modify it
under the terms of the Artistic License version 2.0.
AUTHOR
Jan Henning Thorsen - "jhthorsen@cpan.org"
package App::sibs;
=head1 NAME
App::sibs - Simple incremental backup system
=head1 VERSION
0.17
=head1 DESCRIPTION
C<sibs> create backup from your computer to a destination server, using
C<rsync>.
=over 4
=item * Setup
First you need to C<setup> sibs. The setup process will create a SSH key
which is uploaded to the C<destination> server. The key is created using
C<ssh-keygen> and uploaded using C<ssh> and C<perl>.
In addition, this step will create or update a config file. The default
config file is L<$HOME/.sibs.conf>, but you can also specify your own path.
=item * Backup
The second step will create the actual backup. This step can be automated
in a cronjob, because the key created in the first step will not require
any password to log into the remote backup server. Example crontab:
0 */4 * * * /usr/local/bin/sibs backup 2>/dev/null 1>/dev/null
This will create a backup every four hours. The backup will be "full" each
time and kept for a month. Even so, it probably won't take up too much space,
since all the files are hard linked, meaning an unchanged file will take up
the same disk space for each backup.
=back
=head1 SYNOPSIS
$ sibs <action> <options> <config file>
$ sibs setup
$ sibs backup
=over 4
=item * action: setup
Used to L<set up|/Setup> the inital config file.
=item * action: backup
Used to take L<backup|/Backup>.
=item * action: man
Used to see this help text.
=item * options: --verbose, --silent
Adjust log level.
=item * config file: Path to L</CONFIG FILE>.
=head1 CONFIG FILE
The config file is in perl format, and read by perl. This means that any perl
code that exists in the file will be evaluated by perl. The return value need
to be a hash with the config. Example:
{
format => '%d-%H', # default
exclude => [qw( .cache .gvfs Downloads Dropbox Trash )],
source => [qw( /home/username )],
destination => "rsync://login-username@my.server.com/var/backup/username",
}
Defaults:
=over 4
=item * format: %d-%H
The "format" will name the destination folder. See L<POSIX::strftime::GNU/FORMAT>
for details.
=item * exclude: .cache, .gvfs, Downloads, Dropbox, Trash
Which files/directories to exclude.
=item * source: C<$HOME>
Which directory to backup.
=back
Default C<exclude> may change in future release.
=cut
our $VERSION = '0.17';
=head1 COPYRIGHT
$ENV{HOME} ||= 'ENVIRONMENT_HOME_IS_NOT_SET';
END {
unlink $LOCK if $LOCK;
}
sub run_rsync {
my $self = shift;
my $uri = $self->{destination};
my $lock = sprintf '%s.backup.lock', $self->{config};
my @options = qw( -az --delete-after --numeric-ids --relative );
push @options, map {qq(--exclude=$_)} @{$self->{exclude} || []};
push @options, '--verbose' if $self->{verbose};
push @options, @{$self->{source}};
if (my $remote_host = $self->remote_host) {
push @options, sprintf '%s@%s:%s/incoming', $uri->userinfo, $remote_host, $uri->path;
}
else {
printf $CONFIG " email => '%s',\n", $_ if $self->_read('email');
printf $CONFIG " exclude => [qw( %s )],\n", $_ if $self->_read('exclude');
printf $CONFIG " source => [qw( %s )],\n", $_ if $self->_read('source');
printf $CONFIG " destination => '%s',\n", $_ if $self->_read('destination');
print $CONFIG "}\n";
close $CONFIG or die "Could not write '$tmp': $!\n";
rename $tmp, $self->{config} or die "Could not write '$self->{config}: $!'\n";
}
sub add_backup_host_to_ssh_config {
my $self = shift;
my $moniker = $self->remote_host;
my $file = $self->ssh_file('config');
if (-r $file) {
open my $CONFIG, '<', $file or die "Could not open $file: $!";
while (<$CONFIG>) {
next unless /Host\s+$moniker/;
$self->_log("Host $moniker exists in $file.");
return 1;
if (!$self->{ssh_dir}) {
mkdir "$ENV{HOME}/.ssh" or die "Could not mkdir $ENV{HOME}/.ssh: $!" unless -d "$ENV{HOME}/.ssh";
chmod 0700, "$ENV{HOME}/.ssh";
$self->{ssh_dir} = "$ENV{HOME}/.ssh";
}
return $self->{ssh_dir} unless $file;
return join '/', $self->{ssh_dir}, $file;
}
sub _backup_name {
POSIX::strftime($_[0]->{format} || '%d-%H', localtime);
}
sub _log {
my $self = shift;
my $min = (localtime)[1];
my $hour = (localtime)[2];
return if $self->{silent};
warn sprintf "[%02s:%02s] %s\n", $hour, $min, join ' ', @_;
$self->{silent} = splice @args, $i, 1, () and next if $args[$i] =~ /^--?s/;
$i++;
}
$action = shift @args if @args;
$self->{config} ||= "$ENV{HOME}/.sibs.conf";
if ($action eq 'setup') {
$self->create_sibs_config until eval { $self->load_config };
$self->_log("Created $self->{config}");
$self->add_backup_host_to_ssh_config;
$self->create_identity_file;
}
elsif ($action eq 'backup') {
$self->load_config;
$self->run_rsync;
$self->run_sibs_remote('remote-archive', $self->{destination}->path, $self->_backup_name);
}
elsif ($action eq 'man') {
exec perldoc => 'App::sibs';
}
elsif ($action eq 'remote-init') {
$self->remote_add_pub_key(eval 'do { local $/; <DATA> }');
}
elsif ($action eq 'remote-archive') {
my ($dir, $name) = @args;
chdir $dir or die "Cannot chdir $dir: $!\n";
$self->_system(touch => $name);
}
elsif ($action eq 'version') {
require App::sibs;
print App::sibs->VERSION, "\n";
}
elsif (!$ENV{HARNESS_IS_VERBOSE}) {
print <<' HELP';
sibs man
sibs setup
sibs backup
sibs version
HELP
}
return 0;
}
exit +(bless {})->run(@ARGV) unless defined wantarray;
bless {};
lib/App/sibs.pm view on Meta::CPAN
package App::sibs;
=head1 NAME
App::sibs - Simple incremental backup system
=head1 VERSION
0.17
=head1 DESCRIPTION
C<sibs> create backup from your computer to a destination server, using
C<rsync>.
=over 4
=item * Setup
First you need to C<setup> sibs. The setup process will create a SSH key
which is uploaded to the C<destination> server. The key is created using
C<ssh-keygen> and uploaded using C<ssh> and C<perl>.
In addition, this step will create or update a config file. The default
config file is L<$HOME/.sibs.conf>, but you can also specify your own path.
=item * Backup
The second step will create the actual backup. This step can be automated
in a cronjob, because the key created in the first step will not require
any password to log into the remote backup server. Example crontab:
0 */4 * * * /usr/local/bin/sibs backup 2>/dev/null 1>/dev/null
This will create a backup every four hours. The backup will be "full" each
time and kept for a month. Even so, it probably won't take up too much space,
since all the files are hard linked, meaning an unchanged file will take up
the same disk space for each backup.
=back
=head1 SYNOPSIS
$ sibs <action> <options> <config file>
$ sibs setup
$ sibs backup
=over 4
=item * action: setup
Used to L<set up|/Setup> the inital config file.
=item * action: backup
Used to take L<backup|/Backup>.
=item * action: man
Used to see this help text.
=item * options: --verbose, --silent
Adjust log level.
=item * config file: Path to L</CONFIG FILE>.
lib/App/sibs.pm view on Meta::CPAN
=head1 CONFIG FILE
The config file is in perl format, and read by perl. This means that any perl
code that exists in the file will be evaluated by perl. The return value need
to be a hash with the config. Example:
{
format => '%d-%H', # default
exclude => [qw( .cache .gvfs Downloads Dropbox Trash )],
source => [qw( /home/username )],
destination => "rsync://login-username@my.server.com/var/backup/username",
}
Defaults:
=over 4
=item * format: %d-%H
The "format" will name the destination folder. See L<POSIX::strftime::GNU/FORMAT>
for details.
=item * exclude: .cache, .gvfs, Downloads, Dropbox, Trash
Which files/directories to exclude.
=item * source: C<$HOME>
Which directory to backup.
=back
Default C<exclude> may change in future release.
=cut
our $VERSION = '0.17';
=head1 COPYRIGHT
}
{
ok $script, 'enable to load bin/sibs' or diag $@;
is $script->ssh_file('config'), 't/home/.ssh/config', 'got ssh_file(config)';
}
{
my @read = (
'test@dummy.com',
'.swp backup Downloads',
'./t',
'rsync://bruce@localhost/tmp',
);
no warnings 'redefine';
local *main::_read = sub { $_ = shift @read };
$script->create_sibs_config;
$script->load_config;
is_deeply(
$script,
{
config => 't/config.sibs',
silent => !$ENV{HARNESS_IS_VERBOSE},
ssh_dir => 't/home/.ssh',
email => 'test@dummy.com',
exclude => [qw( .swp backup Downloads )],
source => [qw( ./t )],
destination => 'rsync://bruce@localhost/tmp',
},
'config was created',
);
}
{
$script->add_backup_host_to_ssh_config;
open my $CONFIG, '<', 't/home/.ssh/config';
my @config = (
"",
"Host sibs-localhost",
" Hostname localhost",
" IdentityFile t/home/.ssh/sibs_dsa",
);
while(<$CONFIG>) {
my $line = shift @config;
is $_, "$line\n", "config has $line";
}
$script->add_backup_host_to_ssh_config;
is -s 't/home/.ssh/config', 78, 'config is the same size';
}
done_testing;
( run in 1.470 second using v1.01-cache-2.11-cpan-49f99fa48dc )