It is currently Sun May 19, 2013 11:47 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Review my backup script
PostPosted: Mon Jan 28, 2008 4:29 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
Hi everyone.

This is my perl backup script. It's not long and overly complex (40 lines with coments).
It should just read a backup.cfg, which contains a SOURCE and a DESTINATION.
The script parses it and then passes the SOURCE and DESTINATION to rsync.
The rsync options are also defined in the script. For some weird reason it doesnt work though...
Just make a backup.cfg somewhere, edit the path to in the script, try and please report back.

Repair and enjoy

Code:
#!/usr/bin/perl -w
use strict;

my $rsync="/usr/bin/rsync";
my $rsync_opts="-nauz";
my $path_to_cfg="/home/felipe/backup.cfg";      #This is the path to the config gile

#my $path_to_script ="/home/felipe/scripts/package-backup.sh";   #The path to my backup script
#This part of the script used an external bash-script as a helper... I gues sits better to have it all in perl

#Open the config file
my $config_file=open(FILE,$path_to_cfg);

#Damn it if the file can't be opend
if(not defined($config_file))
{
   die "Error: could not open $path_to_cfg: $!\n";
}


#Read each line of the file and then parse/work with it.
while(defined(my $line = <FILE>))
{
   chomp($line);      #Remove the '\n'
   $line =~ s/\t/ /;   #Let's also remove any tabs...they suck in configs (I can only remove one, ideas?)
   
   
   if( $line =~ m/#/ or length($line)==0)    #Let's kick out all the coments or empty lines
   {
      next
   }
   
   
   my @line_array = split(/ /,$line);      #We split the line into the two arguments and pump them into an array
   
   system("$rsync $rsync_opts $line_array[0] $line_array[1]");

};
close(FILE);   

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
 Post subject: Re: Review my backup script
PostPosted: Mon Jan 28, 2008 5:16 pm 
Global Moderator
Global Moderator
User avatar

Joined: Fri Mar 10, 2006 4:46 am
Posts: 3631
Location: Still on IPv4
Question: do you need additional options besides what can be done by rsync itself?

_________________
Leenucks - the greatest thing since evolution theory || Questions about forum etiquette? Feel free to PM me.


 Profile Send private message  
 
 Post subject: Re: Review my backup script
PostPosted: Mon Jan 28, 2008 5:46 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
Well, I want to have an easy to maintain system, which in this case is just adding files to a list.  :-[

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
 Post subject: Re: Review my backup script
PostPosted: Tue Jan 29, 2008 5:55 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Mon Aug 28, 2006 11:57 am
Posts: 1040
Location: Karlsruhe, Germany
BUMP.
anyone have a ideas or improvements?

_________________
...and just when you thought, the shadows were safe...


 Profile Send private message  
 
 Post subject: Re: Review my backup script
PostPosted: Tue Jan 29, 2008 7:44 pm 
Master of the known universe
Master of the known universe
User avatar

Joined: Sat Mar 18, 2006 1:13 pm
Posts: 1182
before:
Code:
   $line =~ s/\t/ /;   #Let's also remove any tabs...they suck in configs (I can only remove one, ideas?)

Code:
   $line =~ s/\s+/ /;   #Let's replace any space(s) with 1 space

;)

_________________
Guth
Zenwalk: Don't Panic


 Profile Send private message  
 
 Post subject: Re: Review my backup script
PostPosted: Wed Jan 30, 2008 3:07 am 
Master of the known universe
Master of the known universe
User avatar

Joined: Thu Mar 29, 2007 2:34 am
Posts: 1253
Location: Taiwan
Code:
#Open the config file
my $config_file=open(FILE,$path_to_cfg);

#Damn it if the file can't be opend
if(not defined($config_file))
{
   die "Error: could not open $path_to_cfg: $!\n";
}


I'd recommend you to write it this way:
Code:
open FILE, "<$path_to_cfg" or die "Error: could not open $path_to_cfg: $!\n";


Also,

Code:
   if( $line =~ m/#/ or length($line)==0)    #Let's kick out all the coments or empty lines
   {
      next
   }


Although perl allows you to skip the semicolon at the last line of a block, I'd still recommend you to add it.

BTW, it's really nice to see more perl programmers here!

\!D/

_________________
Wisdom begins in wonder. -- Socrates


 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: