Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hey Everyone,

I'm a Network Admin and just literally picked up programming within the last month. I'm loving Perl so far. But due to my lack of experience, I'm finding it difficult to write even the simplest if and else statements.

Here is my very first program I wrote last night. Basically it takes an array of directories to back up, checks if the dirs exist then prompts a user for a "yes" or "no" response to back up. Then tarballs the directories and moves them to a different server.

My issue is testing user input. I want the code to shorten up a bit. I'm hoping someone will have some insight into this. Also, I'm going to make this an rsync backup script at one point, but wanted to practice with tar and commands first.

Also, I encourage general formatting, syntax, or anything else to help me along the way! Thanks everyone!

#!/usr/bin/perl use warnings; use POSIX qw(strftime); use vars qw( $now_string @backup_dirs $dir $command_tar $dest $filenam +e); @backup_dirs = ( "/home/justin" , "/usr/src" ); foreach $dir ( @backup_dirs ) { unless ( -d $dir ) { die "The directory $dir does not exist!\n$!"; } } foreach ( @backup_dirs ) { print "$_\n"; } while (1) { print "Are you sure you want to backup these directories?\n"; print "Please select yes/no.\n"; my $input = <>; chomp($input); if ( $input eq 'n' ) { die "Program is shutting down.\n"; } if ( $input eq 'y' ) { print "Backup is beginning......\n"; last; } if ( $input eq 'yes' ) { print "Backup is beginning......\n"; last; } if ( $input eq 'no' ) { die "Program is shutting down.\n"; } } $now_string = strftime("%Y%m%d_%H%M", localtime); $filename = "bkp-".$now_string."tar.gz"; $command_tar = "tar cfvz $filename @backup_dirs"; system($command_tar); unless ( -e $filename ) { die "This file did not archive correctly.\n$!"; } print "Where do you want to back this up to?\n"; $dest = <>; chomp($dest); unless ( -d $dest ) { die "This is not a valid directory.\n$!"; } print "Backing up data to $dest now.........\n"; $command_copy = system( "mv -v $filename $dest" ); system("$command_copy"); print "Backup complete!\n";

In reply to Simple Perl Backup help by laxman526

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2014-12-28 16:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls