Backup-EZ

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
==================================================
 
-----------------------------------------
version 0.46 at 2021-01-14 20:48:26 +0000
-----------------------------------------
 
  Change: e6839c576ddfa36f128d51878ec8ced66be41627
  Author: John Gravatt <john@gravatt.org>
  Date : 2021-01-14 14:48:14 +0000
 
    removed previous sudo addition in EZ.pm, because it breaks backups
 
-----------------------------------------
version 0.45 at 2020-10-29 19:39:30 +0000
-----------------------------------------
 
  Change: 3fab9d8b94fa23427a9133eeed584d637db73939
  Author: John Gravatt <john@gravatt.org>
  Date : 2020-10-29 14:39:30 +0000
 
    Merge pull request #5 from sandidgm/remote_sudo

MANIFEST  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
# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012.
Changes
INSTALL
LICENSE
MANIFEST
META.json
META.yml
Makefile.PL
README
bin/ezbackup
dist.ini
lib/Backup/EZ.pm
lib/Backup/EZ/Dir.pm
share/ezbackup.conf
share/ezbackup_exclude.rsync
t/00-load.t
t/01-module-prereq.t
t/10-new.t
t/11-backup.t
t/12-reldir.t
t/13-with-user.t
t/14-whoami.t
t/15-expire-without-sudo.t
t/16-expire-with-sudo.t
t/17-missing-dir.t
t/18-exclude.t
t/19-chunked.t
t/author-pod-syntax.t
t/boilerplate.t
t/common.pl
t/ezbackup.conf
t/ezbackup2.conf
t/ezbackup_chunked.conf
t/ezbackup_exclude.conf
t/ezbackup_min.conf
t/ezbackup_missing_dir.conf
t/ezbackup_reldir.conf
t/ezbackup_user.conf

META.json  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
{
   "abstract" : "Simple backups based on rsync.",
   "author" : [
      "John Gravatt <gravattj@cpan.org>"
   ],
   "dynamic_config" : 0,
   "generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {

META.yml  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
---
abstract: 'Simple backups based on rsync.'
author:
  - 'John Gravatt <gravattj@cpan.org>'
build_requires:
  File::Path: '0'
  File::RandomGenerator: '0'
  File::Touch: '0'
  Test::More: '0'
  perl: '5.006'
configure_requires:
  ExtUtils::MakeMaker: '0'

Makefile.PL  view on Meta::CPAN

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
 
$File::ShareDir::Install::INCLUDE_DOTFILES = 1;
$File::ShareDir::Install::INCLUDE_DOTDIRS = 1;
install_share dist => "share";
 
 
my %WriteMakefileArgs = (
  "ABSTRACT" => "Simple backups based on rsync.",
  "AUTHOR" => "John Gravatt <gravattj\@cpan.org>",
  "CONFIGURE_REQUIRES" => {
    "ExtUtils::MakeMaker" => 0,
    "File::ShareDir::Install" => "0.06"
  },
  "DISTNAME" => "Backup-EZ",
  "EXE_FILES" => [
    "bin/ezbackup"
  ],
  "LICENSE" => "perl",
  "MIN_PERL_VERSION" => "5.006",
  "NAME" => "Backup::EZ",
  "PREREQ_PM" => {
    "Carp" => 0,
    "Config::General" => 0,
    "Data::Dumper" => 0,
    "Data::Printer" => 0,
    "Data::UUID" => 0,

README  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
This archive contains the distribution Backup-EZ,
version 0.46:
 
  Simple backups based on rsync.
 
This software is copyright (c) 2014 by John Gravatt.
 
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
 
 
This README file was generated by Dist::Zilla::Plugin::Readme v6.012.

bin/ezbackup  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env perl
 
# vim: tabstop=4 expandtab
 
=head1 NAME
 
ezbackup - a cli utility for Backup::EZ
 
=head1 VERSION
 
version 0.46
 
=cut
 
###### PACKAGES ######
 
use strict;

bin/ezbackup  view on Meta::CPAN

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
}
else {
    main();
}
 
###### END MAIN #######
 
sub install_cron {
    my $cron_dir = "/etc/cron.$InstallCron";
 
    my $ez_path = `which ezbackup`;
    chomp $ez_path;
 
    if ( !File::Spec->file_name_is_absolute($ez_path) ) {
        $ez_path = File::Spec->rel2abs($ez_path);
    }
 
    my $cron_file = "$cron_dir/ezbackup";
    print "installing $cron_file\n";
 
    open my $fh, ">$cron_file" or die "failed to open $cron_file: $!";
    print $fh "#!/bin/sh\n\n";
    print $fh "nice $ez_path\n";
    close $fh;
 
    my_system("chmod 755 $cron_file");
}
 
sub install_cfg {
    my $etc_dir  = "/etc/ezbackup";
    my $dist_dir = File::ShareDir::dist_dir("Backup-EZ");
 
    my_system("mkdir -p $etc_dir");
    my_system("cp $dist_dir/ezbackup.conf $etc_dir");
    my_system("cp $dist_dir/ezbackup_exclude.rsync $etc_dir");
    my_system("chmod 644 $etc_dir/*");
}
 
sub my_system {
    my $cmd = shift;
    print "$cmd\n";
    system($cmd);
    die if $?;
}

bin/ezbackup  view on Meta::CPAN

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        conf         => $Conf,
        exclude_file => $Exclude,
        dryrun       => $DryRun
    );
    die if !$ez;
 
    if ($DumpConf) {
        $ez->dump_conf;
    }
    else {
        $ez->backup;
    }
}
 
sub check_required {
    my $opt = shift;
    my $arg = shift;
 
    print_usage("missing arg $opt") if !$arg;
}

bin/ezbackup  view on Meta::CPAN

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    if ( !($rc) || ( @ARGV != 0 ) ) {
        ## if rc is false or args are left on line
        print_usage("parse_cmd_line failed");
    }
}
 
sub print_usage {
    print STDERR "@_\n";
 
    print "\n* Install default config files to /etc/ezbackup:\n\n"
      . "\t$0 -installcfg\n" . "\n\n";
 
    print "* Install cron.X\n\n"
      . "\t$0 -installcron (hourly|daily|weekly|monthly)\n" . "\n\n";
 
    print "* Run a backup:\n\n"
      . "\t$0\n"
      . "\t\t[-c <conf file>]\n"
      . "\t\t[-e <rsync exclude file>]\n"
      . "\t\t[-dry-run]\n"
      . "\t\t[-?] (usage)\n" . "\n";
 
    exit 1;
}
 
=head1 Backup-EZ
 
Backup::EZ is backup software that is designed to be as easy to use
as possible, yet provide a robust backup solution. I haven't been
able to find an open-source backup solution that worked how I wanted
it to. There are no shortage of options, but the ones I found store
the data in a format that isn't straight-forward. With this, if you
can use ls and rsync/scp, you can do a restore. No muss, no fuss,
just works.
 
POST INSTALLATION
 
        1. configure ssh keys for your remote username
                a. su - root
                b. ssh-keygen -t rsa (skip if already in place)
                c. ssh-copy-id USERNAME@YOURBACKUPHOST
                d. verify you can auto-login
                        ssh root@YOURBACKUPHOST
                e. exit
                 
        2. identify (create if necessary) remote backup directory
                example: /backups
                 
        3. run "ezbackup -installcfg"
                 
        4. edit /etc/ezbackup/ezbackup.conf and modify the TODO's with the
                appropriate values
 
        OPTIONAL
         
        5.  do a manual backup to get things started
                 run: "ezbackup"
 
        AUTOMATE
                 
        6.  Setup cron to run ezbackup on a schedule.  You can do this manually
                if you prefer or let ezbackup do it for you.  Run:
                        "ezbackup -installcron daily"
                                (or whatever freq you prefer in /etc/cron.*)   
                 
RESTORE
 
        There are a plethora of ways to restore files.  You could use scp,
        rsync, cp via nfs, etc.  How to use these utilities is beyond the scope
        of this document.  Here is one example with scp.
         
        1.  login to remote backup server
         
        2.  cd YOURDESTDIR
         
        3.  ls
         
        4.  cd <the subdir of your choice>       
 
        5.  pwd
         
        6.  open another terminal on the source host and login as the user that
                performs backups
         
        7.  scp \
                USERNAME@YOURBACKUPHOST:<DIR FROM STEP 5>/some/dir/you/want/to/restore \
                <YOUR LOCAL DIR>
 
=cut

dist.ini  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
name = Backup-EZ
author = John Gravatt <gravattj@cpan.org>
license = Perl_5
copyright_holder = John Gravatt
copyright_year = 2014
abstract = Simple backups based on rsync.
main_module = lib/Backup/EZ.pm
 
version = 0.46
 
;[NextRelease]
;[CheckChangesHasContent]
;[GatherDir]
 
[Git::GatherDir]
; for integration with travis http://www.guido-flohr.net/integrating-travis-ci-with-dist-zilla/

lib/Backup/EZ.pm  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
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
use File::Slurp qw(slurp read_dir);
use Data::Printer alias => 'pdump';
 
#
# CONSTANTS
#
use constant EXCLUDE_FILE            => '/etc/ezbackup/ezbackup_exclude.rsync';
use constant CONF                    => '/etc/ezbackup/ezbackup.conf';
use constant COPIES                  => 30;
use constant DEST_HOSTNAME           => 'localhost';
use constant DEST_APPEND_MACH_ID     => 0;
use constant USE_SUDO                => 0;
use constant IGNORE_VANISHED         => 0;
use constant DEFAULT_ARCHIVE_OPTS    => '-az';
use constant ARCHIVE_NO_RECURSE_OPTS => '-dlptgoDz';
 
=head1 NAME
 
Backup::EZ - Simple backups based on rsync
 
=head1 VERSION
 
version 0.46
 
=cut
 
=head1 SYNOPSIS
 
  use Backup::EZ;
 
  my $ez = Backup::EZ->new;
  $ez->backup;
 
=head1 DESCRIPTION
 
Backup::EZ is backup software that is designed to be as easy to use
as possible, yet provide a robust solution
 
If you only want to run backups, see the included command line utility
"ezbackup".  See the README for configuration instructions.
 
=head1 SUBROUTINES/METHODS
 
=head2 new
 
optional args:
    conf         => $config_file
    dryrun       => $bool,
    exclude_file => $rsync_excl_file

lib/Backup/EZ.pm  view on Meta::CPAN

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    bless $self, $class;
    return $self;
}
 
sub _debug {
    my $self = shift;
    my $msg  = shift;
 
    my $line = (caller)[2];
 
    openlog "ezbackup", $self->{syslog_option}, LOG_SYSLOG;
    syslog LOG_DEBUG, "($line) $msg";
    closelog;
}
 
#sub _error {
#       my $self = shift;
#       my $msg  = shift;
#
#       openlog "ezbackup", $self->{syslog_option}, LOG_LOCAL7;
#       syslog LOG_ERR, $msg;
#       closelog;
#}
 
sub _info {
    my $self = shift;
    my $msg  = shift;
 
    openlog "ezbackup", $self->{syslog_option}, LOG_SYSLOG;
    syslog LOG_INFO, $msg;
    closelog;
}
 
sub _read_conf {
    my $self = shift;
    my %a    = @_;
 
    # uncoverable branch false
    my $conf = $a{conf} ? $a{conf} : CONF;

lib/Backup/EZ.pm  view on Meta::CPAN

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    -LowerCaseNames => 1,
    -AutoTrue       => 1,
 
);
 
my %conf = $config->getall;
_debug( $self, Dumper \%conf );
 
foreach my $key ( keys %conf ) {
 
    if ( !defined $conf{backup_host} ) {
        $conf{backup_host} = DEST_HOSTNAME;
    }
 
    if ( !defined $conf{copies} ) {
        $conf{copies} = COPIES;
    }
 
    if ( !defined $conf{append_machine_id} ) {
        $conf{append_machine_id} = DEST_APPEND_MACH_ID;
    }

lib/Backup/EZ.pm  view on Meta::CPAN

235
236
237
238
239
240
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    # uncoverable branch true
    confess if $?;
 
    return @out;
}
 
sub _get_dest_username {
    my $self = shift;
 
    if ( $self->{conf}->{backup_user} ) {
        return $self->{conf}->{backup_user};
    }
 
    if ( $ENV{USER} ) {
        return $ENV{USER};
    }
 
    my $whoami = `whoami`;
    chomp $whoami;
 
    return $whoami;
}
 
sub _get_dest_hostname {
    my $self = shift;
 
    return $self->{conf}->{backup_host};
}
 
sub _get_dest_tmp_dir {
    my $self = shift;
 
    return sprintf( "%s/%s", $self->get_dest_dir, ".tmp" );
}
 
sub _get_dest_backup_dir {
    my $self = shift;
 
    return sprintf( "%s/%s", $self->get_dest_dir, $self->{datestamp} );
}
 
sub _is_unit_test {
    my $self = shift;
 
    # uncoverable branch false
    if ( $0 =~ /\.t$/ ) {

lib/Backup/EZ.pm  view on Meta::CPAN

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#
#    $cmd .= " --exclude-from " . $self->{exclude_file};
#
#    $self->_debug($cmd);
#    system($cmd);
#
#    # uncoverable branch true
#    confess if $?;
#}
 
sub _full_backup_chunked {
    my $self = shift;
    my $dir  = shift;
 
    $self->_rsync2(
        dir          => $dir->dirname,
        archive_opts => ARCHIVE_NO_RECURSE_OPTS,
        extra_opts   => $dir->excludes(),
    );
 
    my @entries = read_dir( $dir->dirname, prefix => 1 );

lib/Backup/EZ.pm  view on Meta::CPAN

491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
        if ( -d $entry ) {
            $self->_rsync2(
                dir          => $entry,
                archive_opts => DEFAULT_ARCHIVE_OPTS,
                extra_opts   => $dir->excludes(),
            );
        }
    }
}
 
sub _full_backup {
    my $self = shift;
    my $dir  = shift;
 
    if ( $dir->chunked ) {
        $self->_full_backup_chunked($dir);
    }
    else {
        $self->_rsync2(
            dir          => $dir->dirname,
            archive_opts => DEFAULT_ARCHIVE_OPTS,
            extra_opts   => $dir->excludes(),
        );
    }
}
 
sub _inc_backup_chunked {
    my $self            = shift;
    my $dir             = shift;
    my $last_backup_dir = shift;
    my $link_dest       = shift;
 
    $self->_rsync2(
        dir          => $dir->dirname,
        archive_opts => ARCHIVE_NO_RECURSE_OPTS,
        extra_opts   => $dir->excludes(),
        link_dest    => $link_dest,
    );
 
    my @entries = read_dir( $dir->dirname, prefix => 0 );

lib/Backup/EZ.pm  view on Meta::CPAN

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
            $self->_rsync2(
                dir          => $abs_entry,
                archive_opts => DEFAULT_ARCHIVE_OPTS,
                extra_opts   => $dir->excludes(),
                link_dest    => sprintf( '%s/%s', $link_dest, $entry ),
            );
        }
    }
}
 
sub _inc_backup {
    my $self            = shift;
    my $dir             = shift;
    my $last_backup_dir = shift;
 
    my $link_dest = sprintf(
        "%s/%s/%s",
        $self->get_dest_dir,    #
        $last_backup_dir,       #
        $dir->dirname,          #
    );
 
    if ( $dir->chunked ) {
        $self->_inc_backup_chunked( $dir, $last_backup_dir, $link_dest );
    }
    else {
        $self->_rsync2(
            dir          => $dir->dirname,
            archive_opts => DEFAULT_ARCHIVE_OPTS,
            extra_opts   => $dir->excludes(),
            link_dest    => $link_dest,
        );
    }
}

lib/Backup/EZ.pm  view on Meta::CPAN

595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
Does what it says.
 
=cut
 
sub dump_conf {
    my $self = shift;
 
    pdump $self->{conf};
}
 
=head2 backup
 
Invokes the backup process.  Takes no args.
 
=cut
 
sub backup {
    my $self = shift;
 
    $self->_mk_dest_dir( $self->get_dest_dir );
    my @backups = $self->get_list_of_backups;
    $self->_set_datestamp;
    $self->_mk_dest_dir( $self->_get_dest_tmp_dir, $self->{dryrun} );
 
    foreach my $dir ( $self->_get_dirs ) {
 
        my $dirname = $dir->dirname();
        if ( -d $dirname ) {
 
            $self->_info("backing up $dirname");
 
            if ( !@backups ) {
 
                # full
                $self->_full_backup($dir);
            }
            else {
                # incremental
                $self->_inc_backup( $dir, $backups[$#backups] );
            }
        }
        else {
            $self->_info("skipping $dirname because it does not exist");
        }
 
    }
 
    $self->_ssh(
        sprintf( "mv %s %s",
            $self->_get_dest_tmp_dir, $self->_get_dest_backup_dir ),
        $self->{dryrun}
    );
 
    $self->expire();
 
    return 1;
}
 
=head2 expire
 
Expire backups.  Gets a list of current backups and removes old ones that are
beyond the cutoff (see "copies" in the conf file).
 
=cut
 
sub expire {
    my $self = shift;
 
    my @list = $self->get_list_of_backups;
 
    while ( scalar(@list) > $self->{conf}->{copies} ) {
 
        my $subdir = shift @list;
        my $del_dir = sprintf( "%s/%s", $self->get_dest_dir, $subdir );
 
        my $cmd = sprintf( "%s rm -rf $del_dir",
            $self->{conf}->{use_sudo} ? 'sudo' : '' );
 
        $self->_ssh($cmd);
    }
}
 
=head2 get_backup_host
 
Returns the backup_host name.
 
=cut
 
sub get_backup_host {
    my $self = shift;
    return $self->{conf}->{backup_host};
}
 
=head2 get_dest_dir
 
Returns the dest_dir.
 
=cut
 
sub get_dest_dir {
    my $self = shift;

lib/Backup/EZ.pm  view on Meta::CPAN

715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
        my $uuid = slurp("/etc/machine-id");
        chomp $uuid;
 
        $hostname = "$hostname-$uuid";
    }
 
    return sprintf( "%s/%s", $self->{conf}->{dest_dir}, $hostname );
}
 
=head2 get_list_of_backups
 
Returns an array of backups.  They are in the format of "YYYY-MM-DD_HH:MM:SS".
=cut
 
sub get_list_of_backups {
    my $self = shift;
 
    my @backups;
 
    my @list = $self->_ssh( sprintf( "ls %s", $self->get_dest_dir ) );
 
    foreach my $e (@list) {
        chomp $e;
 
        if ( $e =~ /^\d\d\d\d-\d\d-\d\d_\d\d:\d\d:\d\d$/ ) {
            push( @backups, $e );
        }
    }
 
    return @backups;
}
 
1;

lib/Backup/EZ/Dir.pm  view on Meta::CPAN

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    return $str;
}
 
=head1 AUTHOR
 
John Gravatt, C<< <john at gravatt.org> >>
 
=head1 BUGS
 
Please report any bugs or feature requests to C<bug-backup-ez at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Backup-EZ>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
 
 
 
 
=head1 SUPPORT
 
You can find documentation for this module with the perldoc command.

share/ezbackup.conf  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
#
# copies - Indicate how many backups to maintain
#
 
copies 30
 
#
# backup_host - Host that will keep backups
#
 
backup_host TODO
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user OPTIONAL
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir TODO
 
#
# exclude_file - File to pass to rsync with --exclude-from
#
 
exclude_file ezbackup_exclude.rsync
 
#
# use_sudo - Use sudo for remote commands
#
 
use_sudo 0
 
#
# ignore_vanished - Don't fail when files/directories vanish, rsync return code 24
#

t/10-new.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env perl
 
use strict;
use warnings FATAL => 'all';
 
my $ez = Backup::EZ->new( conf      => 'share/ezbackup.conf',
                                                  exclude_file => 'share/ezbackup_exclude.rsync'
);
ok($ez);
 
done_testing();

t/11-backup.t  view on Meta::CPAN

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
###### NUKE AND PAVE ######
 
nuke();
pave();
 
###### RUN TESTS ######
 
# verify dryrun actually works
my $ez = Backup::EZ->new(
        conf         => 't/ezbackup.conf',
        exclude_file => 'share/ezbackup_exclude.rsync',
        dryrun       => 1
);
die if !$ez;
remove_tree($ez->get_dest_dir);
 
ok( $ez->backup );
ok( !$ez->get_list_of_backups() );
 
# now run for real
$ez = Backup::EZ->new(
        conf         => 't/ezbackup.conf',
        exclude_file => 'share/ezbackup_exclude.rsync',
        dryrun       => 0
);
die if !$ez;
 
ok( $ez->backup );
my @list = $ez->get_list_of_backups();
ok( @list == 1 );
 
ok( sleep 1 && $ez->backup );
@list = $ez->get_list_of_backups();
ok( @list == 2 ) or print Dumper \@list;
 
my $host = $ez->get_backup_host;
ok($host);
 
$ez = Backup::EZ->new(
        conf         => 't/ezbackup_min.conf',
        exclude_file => 'share/ezbackup_exclude.rsync',
        dryrun       => 0
);
die if !$ez;
 
ok( sleep 1 && $ez->backup );
@list = $ez->get_list_of_backups();
ok( @list == 3 ) or print STDERR Dumper \@list;
 
my $dir = sprintf "%s/junk", $ez->get_dest_dir;
touch($dir);
 
@list = $ez->get_list_of_backups();
ok( @list == 3 );
 
done_testing();
nuke();

t/12-reldir.t  view on Meta::CPAN

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
require "t/common.pl";
 
nuke();
pave();
 
my $ez;
eval {
        $ez = Backup::EZ->new(
                conf         => 't/ezbackup_reldir.conf',
                exclude_file => 'share/ezbackup_exclude.rsync',
                dryrun       => 0
        );
};
ok($ez);
 
eval { $ez->backup };
ok($@);
 
nuke();
done_testing();

t/13-with-user.t  view on Meta::CPAN

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
require "t/common.pl";
 
nuke();
pave();
 
my $ez;
eval {
        $ez = Backup::EZ->new(
                conf         => 't/ezbackup_user.conf',
                exclude_file => 'share/ezbackup_exclude.rsync',
                dryrun       => 0,
        );
};
ok($ez);
 
ok( $ez->backup );
 
nuke();
done_testing();

t/14-whoami.t  view on Meta::CPAN

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require "t/common.pl";
 
$ENV{USER} = undef;
 
nuke();
pave();
 
my $ez;
eval {
        $ez = Backup::EZ->new(
                conf         => 't/ezbackup.conf',
                exclude_file => 'share/ezbackup_exclude.rsync',
                dryrun       => 0,
        );
};
ok($ez);
ok( $ez->backup );
 
nuke();
done_testing();

t/15-expire-without-sudo.t  view on Meta::CPAN

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
use File::Path qw(make_path remove_tree);
 
require "t/common.pl";
 
###### NUKE AND PAVE ######
 
# delete previous backup dir if exists
my $ez = Backup::EZ->new(
    conf         => 't/ezbackup.conf',
    exclude_file => 'share/ezbackup_exclude.rsync',
    dryrun       => 1
);
die if !$ez;
 
nuke();
pave();
 
# TODO: allow get_list_of_backups to not fail if remote dir does not exist
#ok( !$ez->get_list_of_backups() );
 
###### RUN BACKUPS ######
 
$ez = Backup::EZ->new(
    conf         => 't/ezbackup.conf',
    exclude_file => 'share/ezbackup_exclude.rsync',
    dryrun       => 0
);
die if !$ez;
 
for ( my $i = 0 ; $i < 3 ; $i++ ) {
    ok( $ez->backup );
    sleep 1;    # sleep 1 to make sure we get a new timestamp
}
 
my @list = $ez->get_list_of_backups();
ok( @list == 3 );
 
touch(sprintf("%s/junk", $ez->get_dest_dir() ));
 
# run another backup to test "sudo rm ...." works
ok( $ez->backup );
 
@list = $ez->get_list_of_backups();
ok( @list == 3 );
 
###### CLEANUP ######
 
nuke();
 
done_testing();

t/16-expire-with-sudo.t  view on Meta::CPAN

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
use File::Path qw(make_path remove_tree);
 
require "t/common.pl";
 
###### NUKE AND PAVE ######
 
# delete previous backup dir if exists
my $ez = Backup::EZ->new(
    conf         => 't/ezbackup2.conf',
    exclude_file => 'share/ezbackup_exclude.rsync',
    dryrun       => 1
);
die if !$ez;
 
nuke();
pave();
 
# TODO: allow get_list_of_backups to not fail if remote dir does not exist
#ok( !$ez->get_list_of_backups() );
 
###### RUN BACKUPS ######
 
$ez = Backup::EZ->new(
    conf         => 't/ezbackup2.conf',
    exclude_file => 'share/ezbackup_exclude.rsync',
    dryrun       => 0
);
die if !$ez;
 
for ( my $i = 0 ; $i < 3 ; $i++ ) {
    ok( $ez->backup );
    sleep 1;    # sleep 1 to make sure we get a new timestamp
}
 
my @list = $ez->get_list_of_backups();
ok( @list == 3, "expected 3 dirs, got " . scalar(@list) );
 
touch(sprintf("%s/junk", $ez->get_dest_dir() ));
 
# run another backup to test "sudo rm ...." works
ok( $ez->backup );
 
@list = $ez->get_list_of_backups();
ok( @list == 3 );
 
###### CLEANUP ######
 
nuke();
done_testing();

t/17-missing-dir.t  view on Meta::CPAN

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
###### NUKE AND PAVE ######
 
nuke();
pave();
 
###### RUN TESTS ######
 
# verify dryrun actually works
my $ez = Backup::EZ->new(
        conf         => 't/ezbackup_missing_dir.conf',
        exclude_file => 'share/ezbackup_exclude.rsync',
        dryrun       => 1
);
die if !$ez;
remove_tree($ez->get_dest_dir);
 
ok( $ez->backup );
ok( !$ez->get_list_of_backups() );
 
# now run for real
$ez = Backup::EZ->new(
        conf         => 't/ezbackup_missing_dir.conf',
        exclude_file => 'share/ezbackup_exclude.rsync',
        dryrun       => 0
);
die if !$ez;
 
ok( $ez->backup );
my @list = $ez->get_list_of_backups();
ok( @list == 1 );
 
ok( sleep 1 && $ez->backup );
@list = $ez->get_list_of_backups();
ok( @list == 2 ) or print Dumper \@list;
 
my $host = $ez->get_backup_host;
ok($host);
 
nuke();
done_testing();

t/18-exclude.t  view on Meta::CPAN

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
use Data::Printer alias => 'pdump';
 
require "t/common.pl";
 
use constant SRC_DIR     => '/tmp/backup_ez_testdata';
use constant FOO_SUBDIR  => 'dir1/foo';
use constant SRC_FOO_DIR => sprintf( '%s/%s', SRC_DIR(), FOO_SUBDIR() );
 
###### NUKE AND PAVE ######
 
nuke();
pave();
 
###### RUN TESTS ######
 
my $ez = Backup::EZ->new(
    conf         => 't/ezbackup_exclude.conf',
    exclude_file => 'share/ezbackup_exclude.rsync',
    dryrun       => 0
);
die if !$ez;
remove_tree($ez->get_dest_dir);
 
validate_conf($ez);
finish_paving($ez);
ok( $ez->backup );
 
my @list = $ez->get_list_of_backups();
ok( @list == 1 );
 
my $foo_subdir = get_dest_foo_dir($ez);
ok( !-d $foo_subdir, "checking that $foo_subdir does not exist" );
 
# check counts
my $src_count = get_dir_entry_count(SRC_DIR);
my $foo_count = get_dir_entry_count(SRC_FOO_DIR);
my $expect_count = $src_count - $foo_count;
 
my $dest_count = get_dir_entry_count(get_dest_backup_dir($ez));
ok($dest_count == $expect_count, "checking file counts");
 
done_testing();
nuke();
 
#######################
 
sub get_dir_entry_count{
     
    my $dir = shift;   

t/18-exclude.t  view on Meta::CPAN

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
109
110
111
112
113
114
115
    my @out = `find $src_foo`;
    if ( @out < 2 ) {
        die "not enough files in $src_foo";
    }
}
 
sub get_dest_foo_dir {
    my $ez = shift;
 
    my ($backup_dir) = $ez->get_list_of_backups();
    my $foo_dir = sprintf( '%s/%s%s',
        $ez->get_dest_dir, $backup_dir, get_root_backup_dir($ez));
     
    return $foo_dir;
}
 
sub get_dest_backup_dir {
    my $ez = shift;
     
    return sprintf('%s/%s', get_root_backup_dir($ez), SRC_DIR());
}
 
sub get_root_backup_dir {
    my $ez = shift;
 
    my ($backup_dir) = $ez->get_list_of_backups();
 
    return sprintf( '%s/%s', $ez->get_dest_dir, $backup_dir );
}
 
sub validate_conf {
    my $ez = shift;
 
    # should only have one source dir
    my @dirs = $ez->get_conf_dirs;
    if ( scalar @dirs == 1 ) {
        return 1;
    }

t/19-chunked.t  view on Meta::CPAN

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
use Data::Printer alias => 'pdump';
 
require "t/common.pl";
 
use constant SRC_DIR     => '/tmp/backup_ez_testdata';
use constant FOO_SUBDIR  => 'dir1/foo';
use constant SRC_FOO_DIR => sprintf( '%s/%s', SRC_DIR(), FOO_SUBDIR() );
 
###### NUKE AND PAVE ######
 
nuke();
pave();
 
###### RUN TESTS ######
 
my $ez = Backup::EZ->new(
    conf         => 't/ezbackup_chunked.conf',
    exclude_file => 'share/ezbackup_exclude.rsync',
    dryrun       => 0
);
die if !$ez;
remove_tree($ez->get_dest_dir);
 
validate_conf($ez);
finish_paving($ez);
ok( $ez->backup );
 
my @list = $ez->get_list_of_backups();
ok( @list == 1 );
 
my $foo_subdir = get_dest_foo_dir( $ez, $list[0] );
ok( -d $foo_subdir, "checking that $foo_subdir does exist" );
 
# check counts
my $src_count  = get_dir_entry_count(SRC_DIR);
my $dest_count = get_dir_entry_count( get_dest_backup_dir($ez, $list[0]) );
ok( $src_count == $dest_count, "checking file counts" );
 
#
# now do an inc backup
#
sleep 1;
ok($ez->backup());
@list = $ez->get_list_of_backups();
ok( @list == 2 );
 
$foo_subdir = get_dest_foo_dir( $ez, $list[1] );
ok( -d $foo_subdir, "checking that $foo_subdir does exist" );
 
# check counts
$src_count  = get_dir_entry_count(SRC_DIR);
$dest_count = get_dir_entry_count( get_dest_backup_dir($ez, $list[1]) );
ok( $src_count == $dest_count, "checking file counts" );
 
done_testing();
nuke();
 
#######################
 
sub get_dir_entry_count {
 
    my $dir = shift;

t/19-chunked.t  view on Meta::CPAN

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    $frg->generate;
 
    my @out = `find $src_foo`;
    if ( @out < 2 ) {
        die "not enough files in $src_foo";
    }
}
 
sub get_dest_foo_dir {
    my $ez         = shift;
    my $backup = shift;
 
    my $foo_dir = sprintf(
        '%s/%s%s',
        $ez->get_dest_dir, #
        $backup,             #
        SRC_FOO_DIR(), #
    );
 
    return $foo_dir;
}
 
sub get_dest_backup_dir {
    my $ez = shift;
    my $backup = shift;
     
    my $dir sprintf( '%s/%s', get_root_backup_dir($ez, $backup), SRC_DIR() );
     
    return $dir;
}
 
sub get_root_backup_dir {
    my $ez = shift;
    my $backup = shift;
     
    return sprintf( '%s/%s', $ez->get_dest_dir, $backup );
}
 
sub validate_conf {
    my $ez = shift;
 
    # should only have one source dir
    my @dirs = $ez->get_conf_dirs;
    if ( scalar @dirs == 2 ) {
        return 1;
    }

t/common.pl  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
use File::Path qw(make_path remove_tree);
 
###
 
use constant DATA_DIR => '/tmp/backup_ez_testdata';
 
###
 
sub nuke {
 
        my $data_dir = shift @ARGV;
        if (!$data_dir) {
        $data_dir = DATA_DIR;
        }
 
        # delete previous backup dir if exists
        my $ez = Backup::EZ->new(
        conf         => 't/ezbackup.conf',
        exclude_file => 'share/ezbackup_exclude.rsync',
                dryrun       => 1
        );
        die if !$ez;
         
        remove_tree( $ez->{conf}->{dest_dir} );
 
        # delete previous test data dir if exists
        remove_tree($data_dir);
}

t/ezbackup.conf  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
#
# copies - Indicate how many backups to maintain
#
 
copies                  3
 
#
# backup_host - Host that will keep backups
#
 
backup_host             localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
append_machine_id       off
 
#
# local directories you want backed up
#
 
dir     /tmp/backup_ez_testdata/dir1
dir /tmp/backup_ez_testdata/dir2

t/ezbackup2.conf  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
#
# copies - Indicate how many backups to maintain
#
 
copies                  3
 
#
# backup_host - Host that will keep backups
#
 
backup_host             localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
use_sudo        1
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
append_machine_id       off
 
#
# local directories you want backed up
#
 
dir /tmp/backup_ez_testdata/dir1
dir /tmp/backup_ez_testdata/dir2

t/ezbackup_chunked.conf  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
#
# copies - Indicate how many backups to maintain
#
 
copies                  3
 
#
# backup_host - Host that will keep backups
#
 
backup_host             localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
append_machine_id       off
 
#
# local directories you want backed up
#
# additional options separated by comma:
# * exclude=foo
# * chunked
#
 
dir     /tmp/backup_ez_testdata, exclude=foo/
dir /tmp/backup_ez_testdata/dir1/foo, chunked

t/ezbackup_exclude.conf  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
#
# copies - Indicate how many backups to maintain
#
 
copies                  3
 
#
# backup_host - Host that will keep backups
#
 
backup_host             localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
append_machine_id       off
 
#
# local directories you want backed up
#
# additional options separated by comma:
# * exclude=foo
# * chunked
#
 
dir     /tmp/backup_ez_testdata, exclude=foo/

t/ezbackup_min.conf  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
#
# copies - Indicate how many backups to maintain
#
 
#copies                         30
 
#
# backup_host - Host that will keep backups
#
 
#backup_host            localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
#append_machine_id      off
 
#
# local directories you want backed up
#
 
dir     /tmp/backup_ez_testdata/dir1
dir /tmp/backup_ez_testdata/dir2

t/ezbackup_missing_dir.conf  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
#
# copies - Indicate how many backups to maintain
#
 
copies                  3
 
#
# backup_host - Host that will keep backups
#
 
backup_host             localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
append_machine_id       off
 
#
# local directories you want backed up
#
 
dir     /tmp/backup_ez_testdata/dir1
dir /tmp/backup_ez_testdata/dir2
dir /tmp/backup_ez_testdata/bogus

t/ezbackup_reldir.conf  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
#
# copies - Indicate how many backups to maintain
#
 
#copies                         30
 
#
# backup_host - Host that will keep backups
#
 
#backup_host            localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
#backup_user    root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
append_machine_id       on

t/ezbackup_user.conf  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
#
# copies - Indicate how many backups to maintain
#
 
#copies                         30
 
#
# backup_host - Host that will keep backups
#
 
#backup_host            localhost
 
#
# backup_user - Overrides the default remote user.  Default is $USER.
#
 
backup_user     root
 
 
#
# dest_dir - Location to place backups on the remote FS.
#
 
dest_dir                /tmp/backups
 
#
# append_machine_id - Flag to indicate whether or not to append /etc/machine-id
# to the hostname.  If on, the uuid from /etc/machine-id will be appended to the
# hostname subdir.  If /etc/machine-id does not exist, it will be created.
#
# Example:  localhost-8d18a07b-f327-4fe4-85a0-168f95f17cf9/
#
 
#append_machine_id      off
 
#
# local directories you want backed up
#
dir     /tmp/backup_ez_testdata/dir1
dir /tmp/backup_ez_testdata/dir2



( run in 0.536 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )