Security Basics mailing list archives

Re: Re: Help writing a back up script


From: jason.gerfen () gmail com
Date: 26 Oct 2005 11:53:50 -0000

Here, hope this helps.  You can modify it to suit your needs.  You can replace the system calls I make to create the 
backup with the perl package to create tarballs etc.  But this works for an easy backup solution.

#!/usr/bin/perl
# Backup script with FTP
# Author: Jason Gerfen <jason.gerfen () gmail com>

# Declare modules to use
use strict;
use Getopt::Long;
use POSIX qw(strftime);
use Net::FTP;

# Globals
my ( $host, $user, $pass, $dir, $gz ) = '';

# Grab our arguments
GetOptions( "host=s" => \$host, "user=s" => \$user, "pass=s" => \$pass, "dir=s"  => \$dir );

# Check for required arguments
if( ( defined( $host ) ) && ( defined( $user ) ) && ( defined( $pass ) ) && ( defined( $dir ) ) ) {
    &FTPConn( $host, $user, $pass );
} else {
    &ShowSyntax;
}

sub GetDate
{
  # Current date stamp
  my $date = strftime('%Y%m%d', localtime());
  return $date;
}

sub CreateArchive
{
  $date = &GetDate;
  $gz = system('tar -cf $dir-$date.tar $dir');
  $gz = system('gzip -9 $dir.tar');
  return $gz;
}

sub FTPConn
{
  $gz = &CreateArchive( $dir );
  my $ftp = Net::FTP->new( $host, Debug => 1 )or die "Could not open Connection to $host - $!";
     $ftp->login( $user,  $pass )or die "Could not login to $host: $@";

  foreach my $bup (glob('$dir-$date.tar.gz')) {
      $ftp->binary;
      $ftp->put($bup) or die "Could not transfer files ", $ftp->message; }
}

sub ShowSyntax
{
  print <<EOF;

Backup Script
-----------------
Creates gziped archive of specified
directory and moves to remote FTP server

Usage:  ./backup.pl --host=<IPADDRESS> --user=<USERNAME> --pass=<PASSWORD> --dir=<DIRECTORY_TO_BACKUP>

Revision: 0.30 <jason.gerfen\@gmail.com>

Supported options are:

  host    STRING   Specify IP or Hostname of remote FTP server

  user    STRING   Specify the username to connect with
                
  pass    STRING   Provide the FTP connection password
                
  dir     STRING   Specify the directory to backup

EOF
  exit;
}


Current thread: