Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

problem in a small script

by massoo (Initiate)
on Jan 15, 2006 at 04:22 UTC ( [id://523290]=perlquestion: print w/replies, xml ) Need Help??

massoo has asked for the wisdom of the Perl Monks concerning the following question:

hi, i am a network admin and has very minimal/no knowledge of perl programming. i have tried to write a perl script to backup a subversion repository weekly & daily. the weekly script just works fine and writes the youngest version it has backed up in the log file called last_backed_up in the specified backup directory. Now my problem is with the daily backup script, which has to read the value of the last backed up revision from the last_backed_up file and if there is any change then do the incremental backup and again overwrite the file with the youngest (lstatest revision no.) and close it. The problem here is that no error has been thrown out and no desired output either which is the latest version no. in the log file last_backed_up and the compressed output file which includes the date which it was created. The platform i am using are: RHEL 3 / perl-5.8.0-88.4 the code is as follows:
#!/usr/bin/perl -w # # Perform a daily backup of a Subversion repository, # using the previous most-recently-backed-up revision # to create just an incremental dump. my $svn_repos = "/cvs/satrang/software"; my $backups_dir = "/home/svnbkp"; my $next_backup_file = "daily-incremental-backup." . 'date +%Y%m%d'; open(IN, "$backups_dir/last_backed_up"); $previous_youngest = <IN>; chomp $previous_youngest; close IN; my $youngest = "svnlook youngest $svn_repos"; chomp $youngest; if($youngest eq $previous_youngest) { print "No new revisions to back up.\n"; exit 0; } # We need to backup from the last backed up revision # to the latest (youngest) revision in the repository $first_rev = $previous_youngest + 1; $last_rev = $youngest; print "Backing up revisions $first_rev to $last_rev...\n"; #$svnadmin_cmd = "svnadmin dump --incremental " . "--revision $first_rev:$last_rev ". " +$svn_repos > $backups_dir/$next_backup_file"; #'$svnadmin_cmd'; print "Compressing dump file...\n"; print 'gzip -9 $backups_dir/$next_backup_file'; open LOG, ">$backups_dir/last_backed_up"; print LOG $last_rev; close LOG;
and the log file is last_backed_up file contains svnlook youngest /cvs/satrang/software which has to represent a no which is the subversion's youngest no and not the command please help me out on how to solve this problem Thanks & Regards Prashant

Replies are listed 'Best First'.
Re: problem in a small script
by helphand (Pilgrim) on Jan 15, 2006 at 04:55 UTC

    Think you mean to use backticks in several places...

    my $next_backup_file = "daily-incremental-backup." . 'date +%Y%m%d'; should be ... my $next_backup_file = "daily-incremental-backup." . `date +%Y%m%d`;

    and

    my $youngest = "svnlook youngest $svn_repos"; should be.... my $youngest = `svnlook youngest $svn_repos`;

    Not sure what you are trying to accomplish with this line of code, but it probably is mean to be backticked also.

    print 'gzip -9 $backups_dir/$next_backup_file'; should be print `gzip -9 $backups_dir/$next_backup_file`;

    Several other places that are commented also look like they are meant to be backticked.

    Scott

      I'd normally not use backticks myself to run something external to get data that Perl can easily give me, but if you do you may need to chomp the results. If you try the following you will see that the un-chomp()ed newline returned at the end of the date command pushes the second "*" down:
      #!/usr/bin/perl use strict; use warnings; my $next_backup_file = "daily-incremental-backup." . `date +%Y%m%d`; print "*$next_backup_file*\n";
      You could use Perl's build in time command and then wouldn't have to chomp the result. There are lots of ways to use this, this is one way I have done it:
      # # Get time for now. # my %NOW; ($NOW{'sec'}, $NOW{'min'}, $NOW{'hour'}, $NOW{'mday'}, $NOW{'mon'}, $NOW{'year'}, $NOW{'wday'}, $NOW{'yday'}, $NOW{'isdst'}) = localtime time; my $next_backup_file = sprintf("daily-incremental-backup.%04i%02i%2i", $NOW{'year'} + 1900, $NOW{'mon'} + 1, $NOW{'mday'});
Re: problem in a small script
by dorko (Prior) on Jan 15, 2006 at 05:04 UTC
    Welcome massoo.

    I've got a couple of suggestions.

    1. It's good practice to add the following lines to the start of your code:
      use strict; use warnings; # <- this is the same as using the -w in the #! line use diagnostics;
    2. Find out the difference between ' ' (single quotes) and ` ` (backtics).

      Single quotes are related to strings.

      Backtics are related to system() and exec().

    3. There was a couple of times in your post I was not sure if you meant "no" as in "no." short for number; or if you meant "no" like another way of saying "false".

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: problem in a small script
by Fletch (Bishop) on Jan 15, 2006 at 23:26 UTC

    And no one else has mentioned it but you probably want POSIX::strftime instead of shelling out to call date.

    use POSIX qw( strftime ); ## . . . my $ymd = strftime( "%Y%m%d", localtime() ); ## . . .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://523290]
Approved by sunadmn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found