Jackiejarvis has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone, I'm new in perl and I'm a student in ICT. I need to write some scripts for my internship. Over 3 weeks there is a jury who judge over the internship and they will see my scripts. But it could be that some of the people are experts in perl or scripting. I would like to ask if some of you will check my script and give some suggestions and feedback. The script is to make a dump of all the databases from remote. The scripts works fine normally. Many thanks for the suggestions!
#!/usr/bin/perl # (c) Odeurs Dieter # Script to back-up the databases use strict; use warnings; use Getopt::Long; use Net::Ping; use Time::Local; # Variables my $host; my $engine; my $user; my $password; my $port; my $cmd; my @args = (); my $ping; my $all; my $timestamp; my $filename; my $help; my $error; my $result = ""; my @result; # Read Options GetOptions( "host=s" => \$host, "engine=s" => \$engine, "password=s" => \$password, "user=s" => \$user, "port=s" => \$port, "all" => \$all, "help" => \$help ); if ($help){ showUsage(); } #Creating timestamp my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = gm +time(time); $timestamp = sprintf("%4d-%02d-%02d_%02d-%02d", $year + 1900, $month + + 1, $day, $hour, $min); #------Check options----- # Check if host is defined and alive if (!defined $host){ print "\nNo host specified\n\n"; showUsage(); } else { print "\nChecking if $host is alive.. \n"; $ping = Net::Ping->new(); if ($ping->ping($host)){ print "$host is alive\n\n"; }else { print "$host is unreachable\n\n"; exit; } $ping->close(); } #If a password is set we need to set PGPASSWORD environment variable if ($password && $password ne ''){ if ($engine eq "postgres" ){ $ENV{'PGPASSWORD'} = $password; } else { push(@args, qq{--password=$password}); } } if ($host && $host ne ''){ push(@args, qq{--host $host}); } if ($port && $port ne ''){ push(@args, qq{--port $port}); } if ($user && $user ne ''){ if ($engine eq "postgres" ){ push(@args, qq{--username $user}); } else { push(@args, qq{--user $user}); } } #Check if database engine is defined and valid if (!defined $engine) { print "Please specify the database engine postgres|mysql\n"; showUsage(); } else { # When a engine is defined the trigt sub will be called if ($engine eq "postgres" ){ postgres(); } elsif ($engine eq "mysql" ){ mysql(); } else { print "Unknown database engine\n"; showUsage(); } } #------------------------------------------------ # Postgres #------------------------------------------------ sub postgres{ if (!$all) { #Find al the databases on the specified host print "Checking databases.. \n"; $cmd = "psql -At @args -c 'SELECT datname FROM pg_database'"; #Print the non-standard databases and store them in an array my @databases; @result = `$cmd`; if ($? !=0) { print "Some errors occured..\n\n"; exit; } print "Databases to back-up: \n"; foreach $result (@result) { chomp $result; if ($result !~ "template" && $result !~ "postgres"){ print " - " . $result . "\n"; push(@databases, $result); } } #Back-up each database foreach my $database (@databases){ #Generate a filename for each database $filename = "/home/dieter/dumps/postgres/" . $database . " +_" . $timestamp . ".sql"; $cmd = "pg_dump -C @args $database > $filename"; + print $cmd . "\n" ; @result = `$cmd`; if ($? !=0) { print "Done but with errors\n\n"; exit; }else { print "DONE \n"; } } #-------All database in one file------ } else { $filename = "/home/dieter/dumps/postgres/" . "Fulldump" . "_" +. $timestamp . ".sql"; print "Taking dump of all databases...\n"; $cmd = "pg_dumpall @args > $filename"; @result = `$cmd`; if ($? !=0) { print "Done but with errors\n\n"; } else { print "Dump saved in $filename\n\n"; } } } #----------------------------------------------- # MySQL #----------------------------------------------- sub mysql{ #-------One database in one file------ if (!$all) { #list databases and exclude the default my @databases; print "Checking databases..\n"; $cmd = "mysql @args -Bse 'show databases'"; @result = `$cmd`; if ($? !=0) { print "Some errors occured..\n\n"; exit; } print "Databases to back-up: \n"; foreach $result (@result) { if ($result !~ "information_schema" && $result !~ "mysql") +{ print " - $result \n"; push (@databases, $result); } } push(@args, qq{--single-transaction}); #back-up of the selected databases foreach my $database (@databases){ chomp $database; $filename = "/home/dieter/dumps/mysql/" . $database . "_" +. $timestamp . ".sql"; print "Taking dump of $database ...\n"; $cmd = "mysqldump @args $database > $filename"; @result = `$cmd`; if ($? !=0) { print "Done but with errors\n\n"; exit; }else { print "Dump saved in $filename\n"; } } #-------All database in one file------ } else { $filename = "/home/dieter/dumps/mysql/" . "Fulldump_" . $times +tamp . ".sql"; print "Taking dump of all databases...\n"; $cmd = "mysqldump @args --all-databases > $filename"; @result = `$cmd`; if ($? !=0) { print "Done but with errors\n\n"; exit; }else { print print "Full dump saved in $filename\n"; } } print "Dump completed!\n\n"; } #----------------------------------------------- # Show usage #----------------------------------------------- sub showUsage{ print "\n------------------------------------------\n"; print "-- Usage for the database backup script --\n"; print "------------------------------------------\n"; print "\n--host \n"; print " Specify the host IP\n\n"; print "--engine \n"; print " Specify the database engine. This could be postgres o +r mysql. \n\n"; print "--password \n"; print " Give the password needed to connect with the database + server\n\n"; print "--user \n"; print " Specify user to connect to the database server.\n"; print " The user must have permissions to connect to the data +base(s)\n\n"; print "--port \n"; print " If another port than the standard must be used to con +nect you could \n"; print " specify this with the --port option\n\n"; exit; } exit;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Review of my script
by Tux (Canon) on May 15, 2013 at 08:24 UTC | |
by Jackiejarvis (Novice) on May 15, 2013 at 08:28 UTC | |
Re: Review of my script
by fisher (Priest) on May 15, 2013 at 08:46 UTC | |
by Jackiejarvis (Novice) on May 15, 2013 at 08:54 UTC | |
by Anonymous Monk on May 16, 2013 at 09:04 UTC | |
by tobyink (Canon) on May 16, 2013 at 09:31 UTC | |
by Anonymous Monk on May 16, 2013 at 10:06 UTC | |
by Tux (Canon) on May 16, 2013 at 12:10 UTC | |
Re: Review of my script
by fisher (Priest) on May 15, 2013 at 08:53 UTC | |
Re: Review of my script
by space_monk (Chaplain) on May 15, 2013 at 13:01 UTC | |
Re: Review of my script
by TomDLux (Vicar) on May 15, 2013 at 20:10 UTC | |
Re: Review of my script
by NetWallah (Canon) on May 16, 2013 at 04:06 UTC | |
Re: Review of my script ( s/...$year + 1900/POSIX::strftime/)
by Anonymous Monk on May 16, 2013 at 09:08 UTC | |
Re: Review of my script
by graff (Chancellor) on May 17, 2013 at 02:09 UTC | |
by Jackiejarvis (Novice) on May 17, 2013 at 06:34 UTC |
Back to
Seekers of Perl Wisdom