App-sibs

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

META.json  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
{
   "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" : {

META.yml  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
---
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:

README  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 )],
      }
 
    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"

README.pod  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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>.

README.pod  view on Meta::CPAN

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
=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 )],
  }
 
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

bin/sibs  view on Meta::CPAN

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$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 {

bin/sibs  view on Meta::CPAN

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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;

bin/sibs  view on Meta::CPAN

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  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 ' ', @_;

bin/sibs  view on Meta::CPAN

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  $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";

bin/sibs  view on Meta::CPAN

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    $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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
=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 )],
  }
 
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

t/setup.t  view on Meta::CPAN

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
}
 
{
  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',
  );
 
  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 0.321 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )