laxman526 has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple Perl Backup help
by davido (Cardinal) on Aug 03, 2012 at 19:46 UTC | |
by laxman526 (Initiate) on Aug 03, 2012 at 20:51 UTC | |
|
Re: Simple Perl Backup help
by moritz (Cardinal) on Aug 03, 2012 at 19:26 UTC | |
by laxman526 (Initiate) on Aug 03, 2012 at 19:32 UTC | |
|
Re: Simple Perl Backup help
by toolic (Bishop) on Aug 03, 2012 at 19:30 UTC | |
|
Re: Simple Perl Backup help
by ww (Archbishop) on Aug 03, 2012 at 20:22 UTC | |
by laxman526 (Initiate) on Aug 03, 2012 at 20:54 UTC |