Backup-Hanoi

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.010.
Changes
LICENSE
MANIFEST
META.json
META.yml
Makefile.PL
README
README.md
bin/backup-hanoi
dist.ini
lib/Backup/Hanoi.pm
t/5_lib/0_init.t
t/5_lib/get_device_for_cycle.t
t/8_bin/backup-hanoi.t
t/8_bin/example_devices.txt

META.json  view on Meta::CPAN

{
   "abstract" : "select backup according to algo",
   "author" : [
      "Boris D\u00e4ppen <bdaeppen.perl@gmail.com>"
   ],
   "dynamic_config" : 0,
   "generated_by" : "Dist::Zilla version 6.010, CPAN::Meta::Converter version 2.150001",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.yml  view on Meta::CPAN

---
abstract: 'select backup according to algo'
author:
  - 'Boris Däppen <bdaeppen.perl@gmail.com>'
build_requires:
  Test::Exception: '0'
  Test::More: '0'
  Test::Script: '1.10'
configure_requires:
  ExtUtils::MakeMaker: '0'
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.010, CPAN::Meta::Converter version 2.150001'

Makefile.PL  view on Meta::CPAN

# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.010.
use strict;
use warnings;

use 5.006000;

use ExtUtils::MakeMaker;

my %WriteMakefileArgs = (
  "ABSTRACT" => "select backup according to algo",
  "AUTHOR" => "Boris D\x{e4}ppen <bdaeppen.perl\@gmail.com>",
  "CONFIGURE_REQUIRES" => {
    "ExtUtils::MakeMaker" => 0
  },
  "DISTNAME" => "Backup-Hanoi",
  "EXE_FILES" => [
    "bin/backup-hanoi"
  ],
  "LICENSE" => "perl",
  "MIN_PERL_VERSION" => "5.006000",
  "NAME" => "Backup::Hanoi",
  "PREREQ_PM" => {
    "strict" => 0,
    "warnings" => 0
  },
  "TEST_REQUIRES" => {
    "Test::Exception" => 0,

README  view on Meta::CPAN



This archive contains the distribution Backup-Hanoi,
version 0.005:

  select backup according to algo

This software is copyright (c) 2018 by Boris Däppen.

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.010.

README.md  view on Meta::CPAN

# Backup-Hanoi

This program is written in the Perl Programming Language.

## Purpose

Select a backup device accoring to the algo.

## Example Usage

List 100 cycles:

```bash
backup-hanoi device.list 0 99
```

bin/backup-hanoi  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
# Autor: Boris Däppen, 2018
# No guarantee given, use at own risk and will

# PODNAME: backup-hanoi
# ABSTRACT: predict on which device to put next backup

use v5.6.0;
use Backup::Hanoi;

# we fake say, to support older perl
sub say {
    print "$_[0]\n";
}

my $help = <<'END_MESSAGE';

  backup-hanoi tells you where to write your next backup

  You need to list your devices in a text file, separated by newline

  example usages:

    # list repeatable pattern
    backup-hanoi device-list.txt

    # list which device should be used for backup number 40
    backup-hanoi device-list.txt 40

    # create a device-list for the backups 40 up to 60
    backup-hanoi device-list.txt 40 60

    # first initialise all backups (fifo), then replace 40 times (hanoi)
    backup-hanoi device-list.txt init 40

END_MESSAGE
unless (@ARGV) {
    say $help;
    exit 0;
}


my $devices_file = shift @ARGV;
my $cycle        = shift @ARGV;
my $cycle_top    = shift @ARGV;

open my $file_handle, '<', $devices_file;
chomp(my @devices = <$file_handle>);
close $file_handle;

my $backup = Backup::Hanoi->new(\@devices);

if (defined $cycle_top and ($cycle_top or $cycle_top == 0)) {

    # start with negative number for FIFO
    $cycle = 1 - (scalar @devices) if ($cycle eq 'init');

	for ($cycle .. $cycle_top) {
		say $backup->get_device_for_cycle($_);
	}
}
elsif (defined $cycle and ($cycle or $cycle == 0)) {
	say $backup->get_device_for_cycle($cycle);
}
else {
	for (0 .. ((2**(scalar @devices))/2)-1)  {
		say $backup->get_device_for_cycle($_);
	}
}

__END__

=pod

=encoding UTF-8

=head1 NAME

backup-hanoi - predict on which device to put next backup

=head1 VERSION

version 0.005

=head1 SYNOPSIS

This is an early release.
This code is currently not used in production by the author.
Use it with care!

L<backup-hanoi> tells you where to write your next backup.
You need to list your devices in a text file, separated by newline.

 # list repeatable pattern
 backup-hanoi device-list.txt

 # list which device should be used for backup number 40
 backup-hanoi device-list.txt 40

 # create a device-list for the backups 40 up to 60
 backup-hanoi device-list.txt 40 60

 # if you have 5 devices in the list
 # create a plan for initilisation and overwriting
 # for one year of daily backups
 backup-hanoi device-list.txt -4 360

 # same, but easier
 backup-hanoi device-list.txt init 360

=head1 AUTHOR

Boris Däppen <bdaeppen.perl@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Boris Däppen.

This is free software; you can redistribute it and/or modify it under

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

package Backup::Hanoi;
# ABSTRACT: select backup according to algo
$Backup::Hanoi::VERSION = '0.005';
use strict;
use warnings;

use v5.6.0;

sub new {
    my $class   = shift;
    my $devices = shift;
    $devices = [] unless ($devices); # // operator needs v5.10

    # the number of devices predicts the size of the cycles
    my $device_count = scalar @{$devices};

    die "three devices needed.\n" if ($device_count < 3);

    # half a hanoi cycle is just what we need for backup
    my $hanoi_cycles_half = (2**$device_count) / 2;

    my $self = {    devices           => $devices,
                    hanoi_cycles_half => $hanoi_cycles_half,
               };

    bless $self, $class;

    return $self;
}

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

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Backup::Hanoi - select backup according to algo

=head1 VERSION

version 0.005

=head1 SYNOPSIS

This is an early release.
This code is currently not used in production by the author.
Use it with care!

 my @devices = ('A', 'B', 'C', 'D');
 my $backup  = Backup::Hanoi->new(\@devices);

 # calculate the next 100 backup cycles
 for (0 .. 99) {
     print $backup->get_device_for_cycle($_);
     print "\n";
 }
 
 # enhanced compination of FIFO for initialisation
 # and Hanoi algorithm for overwriting
 for (-3 .. 99) {
     print $backup->get_device_for_cycle($_);
     print "\n";
 }

See also the script L<backup-hanoi>.

=head1 FUNCTIONS

=head2 new

Takes a reference to a list with at least three items.

=head2 get_device_for_cycle

Give any integer, receive a string which represents the selected item.

t/5_lib/get_device_for_cycle.t  view on Meta::CPAN

######################
# test three devices #
######################

my $three_devices =   [
                    'A',
                    'B',
                    'C',
                ];

my $backup3 = Backup::Hanoi->new($three_devices);

is $backup3->get_device_for_cycle(-3), 'C', 'three devices cycle -3';
is $backup3->get_device_for_cycle(-2), 'A', 'three devices cycle -2';
is $backup3->get_device_for_cycle(-1), 'B', 'three devices cycle -1';
is $backup3->get_device_for_cycle( 0), 'C', 'three devices cycle  0';

is $backup3->get_device_for_cycle( 1), 'A', 'three devices cycle  1';
is $backup3->get_device_for_cycle( 2), 'B', 'three devices cycle  2';
is $backup3->get_device_for_cycle( 3), 'A', 'three devices cycle  3';
is $backup3->get_device_for_cycle( 4), 'C', 'three devices cycle  4';
is $backup3->get_device_for_cycle( 5), 'A', 'three devices cycle  5';
is $backup3->get_device_for_cycle( 6), 'B', 'three devices cycle  6';
is $backup3->get_device_for_cycle( 7), 'A', 'three devices cycle  7';

is $backup3->get_device_for_cycle( 8), 'C', 'three devices cycle  8';

#####################
# test four devices #
#####################

my $four_devices =   [
                    'A',
                    'B',
                    'C',
                    'D',
                ];

my $backup4 = Backup::Hanoi->new($four_devices);

is $backup4->get_device_for_cycle( 0), 'D', 'four devices cycle  0';

is $backup4->get_device_for_cycle( 1), 'A', 'four devices cycle  1';
is $backup4->get_device_for_cycle( 2), 'B', 'four devices cycle  2';
is $backup4->get_device_for_cycle( 3), 'A', 'four devices cycle  3';
is $backup4->get_device_for_cycle( 4), 'C', 'four devices cycle  4';
is $backup4->get_device_for_cycle( 5), 'A', 'four devices cycle  5';
is $backup4->get_device_for_cycle( 6), 'B', 'four devices cycle  6';
is $backup4->get_device_for_cycle( 7), 'A', 'four devices cycle  7';
is $backup4->get_device_for_cycle( 8), 'D', 'four devices cycle  8';
is $backup4->get_device_for_cycle( 9), 'A', 'four devices cycle  9';
is $backup4->get_device_for_cycle(10), 'B', 'four devices cycle 10';
is $backup4->get_device_for_cycle(11), 'A', 'four devices cycle 11';
is $backup4->get_device_for_cycle(12), 'C', 'four devices cycle 12';
is $backup4->get_device_for_cycle(13), 'A', 'four devices cycle 13';
is $backup4->get_device_for_cycle(14), 'B', 'four devices cycle 14';
is $backup4->get_device_for_cycle(15), 'A', 'four devices cycle 15';

is $backup4->get_device_for_cycle(16), 'D', 'four devices cycle 16';
is $backup4->get_device_for_cycle(17), 'A', 'four devices cycle 17';
is $backup4->get_device_for_cycle(18), 'B', 'four devices cycle 18';
is $backup4->get_device_for_cycle(19), 'A', 'four devices cycle 19';
is $backup4->get_device_for_cycle(20), 'C', 'four devices cycle 20';
is $backup4->get_device_for_cycle(21), 'A', 'four devices cycle 21';
is $backup4->get_device_for_cycle(22), 'B', 'four devices cycle 22';
is $backup4->get_device_for_cycle(23), 'A', 'four devices cycle 23';
is $backup4->get_device_for_cycle(24), 'D', 'four devices cycle 24';
is $backup4->get_device_for_cycle(25), 'A', 'four devices cycle 25';
is $backup4->get_device_for_cycle(26), 'B', 'four devices cycle 26';
is $backup4->get_device_for_cycle(27), 'A', 'four devices cycle 27';
is $backup4->get_device_for_cycle(28), 'C', 'four devices cycle 28';
is $backup4->get_device_for_cycle(29), 'A', 'four devices cycle 29';
is $backup4->get_device_for_cycle(30), 'B', 'four devices cycle 30';
is $backup4->get_device_for_cycle(31), 'A', 'four devices cycle 31';

is $backup4->get_device_for_cycle(32), 'D', 'four devices cycle 32';

#####################
# test five devices #
#####################

my $five_devices =   [
                    'A',
                    'B',
                    'C',
                    'D',
                    'E',
                ];

my $backup5 = Backup::Hanoi->new($five_devices);

is $backup5->get_device_for_cycle( 0), 'E', 'five devices cycle  0';

is $backup5->get_device_for_cycle( 1), 'A', 'five devices cycle  1';
is $backup5->get_device_for_cycle( 2), 'B', 'five devices cycle  2';
is $backup5->get_device_for_cycle( 3), 'A', 'five devices cycle  3';
is $backup5->get_device_for_cycle( 4), 'C', 'five devices cycle  4';
is $backup5->get_device_for_cycle( 5), 'A', 'five devices cycle  5';
is $backup5->get_device_for_cycle( 6), 'B', 'five devices cycle  6';
is $backup5->get_device_for_cycle( 7), 'A', 'five devices cycle  7';
is $backup5->get_device_for_cycle( 8), 'D', 'five devices cycle  8';
is $backup5->get_device_for_cycle( 9), 'A', 'five devices cycle  9';
is $backup5->get_device_for_cycle(10), 'B', 'five devices cycle 10';
is $backup5->get_device_for_cycle(11), 'A', 'five devices cycle 11';
is $backup5->get_device_for_cycle(12), 'C', 'five devices cycle 12';
is $backup5->get_device_for_cycle(13), 'A', 'five devices cycle 13';
is $backup5->get_device_for_cycle(14), 'B', 'five devices cycle 14';
is $backup5->get_device_for_cycle(15), 'A', 'five devices cycle 15';
is $backup5->get_device_for_cycle(16), 'E', 'five devices cycle 16';

is $backup5->get_device_for_cycle(31), 'A', 'five devices cycle 31';
is $backup5->get_device_for_cycle(32), 'E', 'five devices cycle 32';
is $backup5->get_device_for_cycle(128),'E', 'five devices cycle 128';
is $backup5->get_device_for_cycle(254),'B', 'five devices cycle 254';
is $backup5->get_device_for_cycle(255),'A', 'five devices cycle 255';
is $backup5->get_device_for_cycle(256),'E', 'five devices cycle 256';

####################
# test six devices #
####################

my $six_devices =   [
                    'A',
                    'B',
                    'C',
                    'D',
                    'E',
                    'F',
                ];

my $backup6 = Backup::Hanoi->new($six_devices);

is $backup6->get_device_for_cycle( 0), 'F', 'six devices cycle  0';

is $backup6->get_device_for_cycle( 1), 'A', 'six devices cycle  1';
is $backup6->get_device_for_cycle( 2), 'B', 'six devices cycle  2';
is $backup6->get_device_for_cycle( 3), 'A', 'six devices cycle  3';
is $backup6->get_device_for_cycle( 4), 'C', 'six devices cycle  4';
is $backup6->get_device_for_cycle( 5), 'A', 'six devices cycle  5';
is $backup6->get_device_for_cycle( 6), 'B', 'six devices cycle  6';
is $backup6->get_device_for_cycle( 7), 'A', 'six devices cycle  7';
is $backup6->get_device_for_cycle( 8), 'D', 'six devices cycle  8';
is $backup6->get_device_for_cycle( 9), 'A', 'six devices cycle  9';
is $backup6->get_device_for_cycle(10), 'B', 'six devices cycle 10';
is $backup6->get_device_for_cycle(11), 'A', 'six devices cycle 11';
is $backup6->get_device_for_cycle(12), 'C', 'six devices cycle 12';
is $backup6->get_device_for_cycle(13), 'A', 'six devices cycle 13';
is $backup6->get_device_for_cycle(14), 'B', 'six devices cycle 14';
is $backup6->get_device_for_cycle(15), 'A', 'six devices cycle 15';
is $backup6->get_device_for_cycle(16), 'E', 'six devices cycle 16';

is $backup6->get_device_for_cycle(31), 'A', 'six devices cycle 31';
is $backup6->get_device_for_cycle(32), 'F', 'six devices cycle 32';
is $backup6->get_device_for_cycle(128),'F', 'six devices cycle 128';
is $backup6->get_device_for_cycle(254),'B', 'six devices cycle 254';
is $backup6->get_device_for_cycle(255),'A', 'six devices cycle 255';
is $backup6->get_device_for_cycle(256),'F', 'six devices cycle 256';

t/8_bin/backup-hanoi.t  view on Meta::CPAN

use Test::Script 1.10 tests => 11;

script_compiles('bin/backup-hanoi');

script_runs(['bin/backup-hanoi', 't/8_bin/example_devices.txt', 8]);
script_stdout_is("D\n", 't/8_bin/example_devices 8 -> D');

script_runs(['bin/backup-hanoi', 't/8_bin/example_devices.txt', -1]);
script_stdout_is("D\n", 't/8_bin/example_devices -1 -> D');

script_runs(['bin/backup-hanoi', 't/8_bin/example_devices.txt', -1, 1]);
script_stdout_is("D\nE\nA\n", 't/8_bin/example_devices -1 1 -> DEA');

script_runs(['bin/backup-hanoi', 't/8_bin/example_devices.txt', "init", 0]);
script_stdout_is("A\nB\nC\nD\nE\n", 't/8_bin/example_devices init 0 -> ABCDE');

script_runs(['bin/backup-hanoi', 't/8_bin/example_devices.txt', "-4", 0]);
script_stdout_is("A\nB\nC\nD\nE\n", 't/8_bin/example_devices -4 0 -> ABCDE');



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