Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl socket backup

Status
Not open for further replies.

twantrd

Technical User
Feb 13, 2005
26
US
Hey guys,

I'm trying to write a perl socket daemon that will listen on a port and accepts a data connection via a tar piped with netcat for backups. The client will run this command:

Code:
tar cvf - tartest | nc 192.168.1.10 9999

my perl code is here:
Code:
#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $client;
my $socket;
my $file;
my $logfile;
my $line;
my $buf;
my $host;

# Supply argument...
if (@ARGV != 1) {
die "Need to supply an argument\nUsage: $0 port\n"
}

my $port=$ARGV[0];

# Setup the socket...
$socket = IO::Socket::INET->new(
        "Proto" => "tcp",
        "LocalPort" => $port,
        "Listen" => 1) or die "ERROR: $!\n";
print "Waiting for connection...\n";

# Accept the connection...
while ($client=$socket->accept()) {
        next if my $pid=fork;
        die "Cannot fork $!\n" unless defined $pid;
        $host=$client->peerhost();
        print "Connection received from: ", $host . "\n";
        $logfile = <$client>;
        print "Backup File: $logfile";

        # This is the part I need help on...
        while (defined($buf=<$client>)) {
        open(BKUP, ">/tmp/$host/backup.tar") or die "Cannot write to /tmp $!";
        print BKUP $buf;
        close(BKUP);
        }
close $client;

I do get output written as backup.tar in the desired path but it's raw data and not a tar archive. Can someone shed me some light on this? Thank you.

-twan
 
I'm not familiar with tar archives, but I guess you should try to write it as a binary file.
Insert following line between open and print:
binmode BKUP;

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Hi rvBasic,

Thanks for your suggestion. I tried that and my output file is still raw data instead of a tar archive. Any other ideas?

Code:
[root@twantrd tmp]# file backup.tar
backup.tar: data
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top