Ecb3a5f6532ed726f15f6f8fadca6134

I code this script for need, it's about 2 years I don't code in Perl...
This code tar all the file on the dir and send it in my ftp server.

#!/usr/bin/perl  
# Load the Net::FTP package 
use Net::FTP;  

$host = 'http://refactormycode.com/';
$path = '/httpdocs/rep/';

#Get Time for filename
$ora = time();
@ltime = localtime($ora);
$data = $ltime[3] . ($ltime[4] + 1) . "0" . ($ltime[5] - 100) . "_" . $ltime[2] . $ltime[1];

$filename = "backup_" . $data  . ".tar";


print "Begin to compress...\n";
$command = "tar -cvf " . $filename . " *"; 
system($command);      
printf "\n\nFINISHED!\n";

$ftp = Net::FTP->new($host, Timeout => 60, Passive => 1)
	or die "Cannot contact $host : $!";
$ftp->login('******', '******')
	or die "Can't login ( $host) : " . $ftp->message;
print "Login eseguito\n";

$ftp->cwd($path)
	or die "Can't change directory ($oath):" . $ftp->message;
print "Switching to dir $path\n";

print "Begin backup upload\n";
$ftp->put($filename)
	or die "Cannot put file : $!";
print "File transfer complete\n";


$ftp->quit ;

print "Backup eseguito con successo\n";
system("rm $filename");

Refactorings

No refactoring yet !

B5603dc8f1e87c251bf9b1d28f31d38f

mrxinu

October 22, 2008, October 22, 2008 13:05, permalink

1 rating. Login to rate!

So, a few things:

1. Added use warnings & strict.
2. The timestamp formatting has been updated.
3. The error message had "$oath" instead of "$path".
4. Replaced system("rm $filename") with unlink($filename).

#!/usr/bin/perl  
# Load the Net::FTP package 
use strict;
use warnings;

use Net::FTP;

my $host = 'http://refactormycode.com/';
my $path = '/httpdocs/rep/';

# build timestamp
my $now = time();
my ($min, $hour, $mday, $mon, $year) = (localtime($now))[1,2,3,4,5];

# format: DDMMYY_HHMM
my $timestamp = sprintf('%d%d%02d_%d%d', ($mday, $mon, $year % 100, $hour, $min));

my $filename = "backup_" . $timestamp  . ".tar";

print "Begin to compress...\n";
my $command = "tar -cvf " . $filename . " *"; 
system($command);      
printf "\n\nFINISHED!\n";

my $ftp = Net::FTP->new($host, Timeout => 60, Passive => 1)
	or die "Cannot contact $host : $!";
$ftp->login('******', '******')
	or die "Can't login ($host) : " . $ftp->message;
print "Login eseguito\n";

$ftp->cwd($path)
	or die "Can't change directory ($path):" . $ftp->message;
print "Switching to dir $path\n";

print "Begin backup upload\n";
$ftp->put($filename)
	or die "Cannot put file : $!";
print "File transfer complete\n";

$ftp->quit;

print "Backup eseguito con successo\n";
unlink($filename);

Your refactoring





Format Copy from initial code

or Cancel