Linux/Unix实现自动ftp的shell脚本

Posted ouyangbuxiu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux/Unix实现自动ftp的shell脚本相关的知识,希望对你有一定的参考价值。

#!/bin/ksh
. /tbill/app/.profile

sqlplus err/err123@billing @/tbill/data/recyclepath/tmp/ftp_file_data.sql

onInit()
{
  USERHOME=$HOME
  if [ -f "/tbill/data/recyclepath/tmp/autoftp.conf" ];then
    . /tbill/data/recyclepath/tmp/autoftp.conf
    echo machine $host > $USERHOME/.netrc
    echo login $user >> $USERHOME/.netrc
    echo password $passwd >> $USERHOME/.netrc

    chmod 600 $USERHOME/.netrc
    echo =============================
    echo "host=$host"
    echo "user=$user"
    echo "password=x"
    echo "remote dir=$remoteDir"
    echo "source dir=$sourceDir"
    echo "success dir=$backupDir"
    echo "pattern=$pattern "

    echo =============================
    return 0
  else
    return 1
  fi

}

showinfo()
{
  echo NAME
  echo "      autoftp  -automatic upload file to ftp server"
  echo SYNOPSIS
  echo "      autoftp"
}
checkdir()
{
  if [ ! -d $sourceDir ];then
    echo "ERROR!    source dir $sourceDir is not found!"
    return 1
  fi
  if [ ! -d $backupDir ];then
    mkdir $backupDir
  fi
  return 0
}
putFiles()
{
ftp -i $host << EOF
cd $remoteDir
mput $@
bye
EOF
}
#############  main procedure ###################
DIRNAME=`date +%Y%m%d`
if [ $# -eq 1 -a "$1" = "-v" ] ;then
  showinfo
  exit
fi
#======================================================
#backup .netrc file
if [ -f ~/.netrc ]; then
  mv ~/.netrc ~/.netrc.old
fi
#======================================================

onInit
if [ $? -ne 0 ];then
  echo "ERROR!    autoftp.conf not exist! please prepare it"   
  exit 1
fi

checkdir
if [ $? -ne 0 ];then
  exit 1
fi
#begin ftp
date
echo "begin ftp"
cd $sourceDir

while read file;
do
    if [ -f $sourceDir$file ]; then
     touch "$file.chk"
     mv $file "$file.txt"
  putFiles "$file.txt $file.chk"
  mv $file.txt /tbill/data/recyclepath/tmp/$backupDir
  rm $file.chk
  echo "put $file.txt $file.chk done!"
    fi
done <<!
`ls -1|grep "$pattern"`
!

date
echo "ftp end"


#=============================================
if [ -f ~/.netrc ]; then
  rm -f ~/.netrc
fi
if [ -f ~/.netrc.old ]; then
  mv ~/.netrc.old ~/.netrc
fi
#============================================


###*************************************配置文件
host=10.100.5.6
user=noserv
passwd=noserv
sourceDir=/tbill/data/other_path/
backupDir=`date +%Y%m%d`
remoteDir=./
#使用正则表达式描述
pattern="^report.*"

以上是关于Linux/Unix实现自动ftp的shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

Linux/Unix shell 自动发送AWR report

shell脚本实现FTP自动上传文件

Linux使用Shell脚本实现ftp的自动上传下载

Linux入门——基础shell

mysql+ftp+shell脚本自动实现数据库的备份

shell编程基础-上