Config-MyConfig2

 view release on metacpan or  search on metacpan

bin/myconfig-demo.cfg  view on Meta::CPAN


 # Debuglevel, range (0..2)
 debuglevel      1
 
 
 #
 # ---- Backup Directives ----
 #
 
 
 <backup server-system>

        hostname        localhost
        backupschedule  Mon, Wed, Fri
        archiveschedule Sun
        archivemaxdays  60

        add  /
        excl /home, /proc, /sys, /dev
        excl /mnt, /media

 </backup>
 
 <backup server-home>

        hostname		= localhost
        backupschedule 		= Mon, Wed, Fri
        archiveschedule 	= Sun
        archivemaxdays  	= 30

        add /home

 </backup>

bin/myconfig-demo.pl  view on Meta::CPAN

);

my $conftemplate;
$conftemplate->{global}->{rsync} = { type => 'single', match => '.+' };
$conftemplate->{global}->{sendmail} = { type => 'single', match => '.+' };
$conftemplate->{global}->{tar} = { type => 'single', match => '.+' };
$conftemplate->{global}->{ssh} = { type => 'single', match => '.+' };
$conftemplate->{global}->{rsync} = { type => 'single', match => '.+' };
$conftemplate->{global}->{debuglevel} = { type => 'single', match => '^\d$' };

$conftemplate->{backup}->{hostname} = { type => 'single', match => '^[a-zA-Z0-9\.]+$' };
$conftemplate->{backup}->{backupschedule} = { type => 'list', match => '^[Mon]|[Tue]|[Wed]|[Thu]|[Fri]|[Sat]|[Sun]$' };
$conftemplate->{backup}->{archiveschedule} = { type => 'list', match => '^[Mon]|[Tue]|[Wed]|[Thu]|[Fri]|[Sat]|[Sun]$' };
$conftemplate->{backup}->{archivemaxdays} = { type => 'list', match => '^\d+$' };
$conftemplate->{backup}->{add} = { type => 'list', match => '.+' };
$conftemplate->{backup}->{excl} = { type => 'list', match => '.+' };

$myconfig->SetupDirectives($conftemplate);

my $config = $myconfig->ReadConfig();

print "\n* Dump original configuration\n";

print Dumper (\$config);

print "\n* Get a global value\n";

my $global_value = $myconfig->GetGlobalValue('rsync');
print "Global value rsync: $global_value\n";

print "\n* Get some directive values\n";
my @backup_identifiers = $myconfig->GetDirectiveIdentifiers('backup');
foreach my $identifier (@backup_identifiers)
{
	my $backup_value_array_ref = $myconfig->GetDirectiveVaue('backup',$identifier,'backupschedule');
	foreach my $val (@$backup_value_array_ref)
	{
		print "Backup directive with identifier $identifier, parameter archiveschedule has value $val\n";	
	}	
}

print "\n* Change some values\n";

my $error;

print "  - Changing tar to /has/been/changed: ";
$error = $myconfig->SetGlobalValue('tar','/has/been/changed');
$error ? print "$error\n" : print "ok\n";

print "  - Changing debuglevel to /has/been/changed: ";
$error = $myconfig->SetGlobalValue('debuglevel','ultra-maximum');
$error ? print "$error\n" : print "ok\n";

print "  - Inserting new backup directive with the name new-directive: ";
$error = $myconfig->SetDirectiveValue('backup','new-directive','hostname','somenewhostname.com');
$error ? print "$error\n" : print "ok\n";

print "  - Adding new archiveschedule day for directive server-system: ";
$error = $myconfig->SetDirectiveValue('backup','server-system','archiveschedule','Thu');
$error ? print "$error\n" : print "ok\n";

my $file = 'new_demo.cfg';
print "\n* Write the modified configuration file to $file\n";
$myconfig->WriteConfig('Server Backup Configuration File',$file);

lib/Config/MyConfig2.pm  view on Meta::CPAN


=head2 error

Internal method, that is used to throw an error. The default behavior is to
croack().

=head1 EXAMPLE

=over

=item * Configuration file for a backup script: backup.cfg
 
 --- snip ---
 #
 # Config file
 #
 
 #
 # ---- Global Section ----
 #
 

lib/Config/MyConfig2.pm  view on Meta::CPAN

 # Path to ssh command
 # If not specified, rsh will be used 
 ssh             /usr/bin/ssh
 # Debuglevel, range (0..2)
 debuglevel      1
 
 #
 # ---- Backup Directives ----
 #
  
 <backup server-system>
        hostname        localhost
        backupschedule  Mon, Wed, Fri
        archiveschedule Sun
        archivemaxdays  60
        add  /
        excl /home, /proc, /sys, /dev, /mnt, /media
 </backup>
 
 <backup server-home>
        hostname        localhost
        backupschedule  Mon, Wed, Fri
        archiveschedule Sun
        archivemaxdays  30
        add  /home
 </backup>
 
 --- snap ---

=item * Setup procedure in perl context

 #!/usr/bin/perl
 
 use Config::MyConfig2;
 use strict;
 use Data::Dumper;
 
 my $myconfig = Config::MyConfig2->new(
 	conffile => "backup.cfg"
 );
 
 my $conftemplate;
 $conftemplate->{global}->{rsync} = { required => 'true', type => 'single', match => '.+' };
 $conftemplate->{global}->{sendmail} = { required => 'true', type => 'single', match => '.+' };
 $conftemplate->{global}->{tar} = { required => 'true', type => 'single', match => '.+' };
 $conftemplate->{global}->{ssh} = { required => 'true', type => 'single', match => '.+' };
 $conftemplate->{global}->{rsync} = { required => 'true', type => 'single', match => '.+' };
 $conftemplate->{global}->{debuglevel} = { required => 'true', type => 'single', match => '^\d$' };
 
 $conftemplate->{backup}->{hostname} = { required => 'true', type => 'single', match => '^[a-zA-Z0-9\.]+$' };
 $conftemplate->{backup}->{backupschedule} = { required => 'true', type => 'list', match => '^[Mon]|[Tue]|[Wed]|[Thu]|[Fri]|[Sat]|[Sun]$' };
 $conftemplate->{backup}->{archiveschedule} = { required => 'true', type => 'list', match => '^[Mon]|[Tue]|[Wed]|[Thu]|[Fri]|[Sat]|[Sun]$' };
 $conftemplate->{backup}->{archivemaxdays} = { required => 'true', type => 'list', match => '^\d+$' };
 $conftemplate->{backup}->{add} = { required => 'true', type => 'list', match => '.+' };
 $conftemplate->{backup}->{excl} = { required => 'false', type => 'list', match => '.+' };
 
 $myconfig->SetupDirectives($conftemplate);
 
 my $config = $myconfig->ReadConfig();
 
 print Dumper (\$config);

=item * Results in the following hash structure

 $VAR1 = \{
            'global' => {
                          'tar' => '/bin/tar',
                          'sendmail' => '/usr/sbin/sendmail',
                          'rsync' => '/usr/bin/rsync',
                          'ssh' => '/usr/bin/ssh',
                          'debuglevel' => '1'
                        },
            'backup' => {
                          'server-home' => {
                                             'archivemaxdays' => [
                                                                   '30'
                                                                 ],
                                             'add' => [
                                                        '/home'
                                                      ],
                                             'archiveschedule' => [
                                                                    'Sun'
                                                                  ],
                                             'hostname' => 'localhost',
                                             'backupschedule' => [
                                                                   'Mon',
                                                                   'Wed',
                                                                   'Fri'
                                                                 ]
                                           },
                          'server-system' => {
                                               'excl' => [
                                                           '/home',
                                                           '/proc',
                                                           '/sys',

lib/Config/MyConfig2.pm  view on Meta::CPAN

                                               'archivemaxdays' => [
                                                                     '60'
                                                                   ],
                                               'add' => [
                                                          '/'
                                                        ],
                                               'archiveschedule' => [
                                                                      'Sun'
                                                                    ],
                                               'hostname' => 'localhost',
                                               'backupschedule' => [
                                                                     'Mon',
                                                                     'Wed',
                                                                     'Fri'
                                                                   ]
                                             }
                        }
          };

=back



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