SFTP 在 cronjob 上传文件

SFTP 在 cronjob 上传文件

我想使用SFTP将文件从A上传到B。

A 的路径:/tmp/A B 的路径:/tmp/B

服务器 B 有自己的用户名和密码。我的 cronjob 将每 10 分钟运行一次,检查 /tmp/A 下是否有新文件,如果有则传输文件,如果没有则不传输。

答案1

您可以使用 rsync 命令进行此类操作。

如何使用 rsync 与远程系统同步

如果您可以通过 SSH 访问远程计算机并且双方都安装了 rsync,那么同步到远程系统就很简单了。您需要设置 SSH 密钥,这里详细描述了如何执行此操作:https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2

一旦您在两台计算机之间验证了 SSH 访问权限,您就可以使用以下语法将 dir1 文件夹从之前的文件夹同步到远程计算机(请注意,在本例中我们要传输实际目录,因此我们省略尾部斜杠):

rsync -avz ~/dir1 username@remote_host:destination_directory

这称为“推送”操作,因为它将目录从本地系统推送到远程系统。

答案2

如果我理解正确的话,我发现某些 SFTP 服务器不支持 ssh 密钥,因此 rsync 无法工作(以 reports.paypal.com 为例)。在这种情况下,您需要编写执行 sftp 操作的脚本,您可以使用 Expect 来完成此操作...这是一个适合您的脚本:

#!/bin/sh

bname=`basename $0`

usage () #usage instructions
{
  cat <<EOF
  syncs LOCALPATH to REMOTEPATH on sftp://SERVICE

  Usage : ./$bname [-s SERVICE -l LOCALPATH -r REMOTEPATH] [-h]
    -h  display this help text.

  Example: ./$bname -s your.sftpserver.com -l /tmp/A -r /tmp/B
            will upload any missing files from /tmp/A to /tmp/B

EOF
  exit 0
}

upload ()
{
  security find-generic-password -s _$SERVICE_sftp >/dev/null 2>/dev/null #test if entry exists in keychain
  if [ $? -ne 0 ]; then
    echo "Please enter username for sftp://"$SERVICE
    read username
    echo $username > .credentials_tmp
    read -p "Password:" -s pass
    echo $pass >> .credentials_tmp
  else
    echo `security find-generic-password -s _$SERVICE_sftp | grep "acct" | cut -d \" -f 4` > .credentials_tmp
    echo `security find-generic-password -s _$SERVICE_sftp -w` >> .credentials_tmp
  fi
  echo $SERVICE >> .credentials_tmp
  echo $REMOTEPATH >> .credentials_tmp
  echo $LOCALPATH >> .credentials_tmp
  ls -1 $LOCALPATH > .LocalFileList.txt
  echo "
Connecting to sftp://"$SERVICE
  expect << 'EOS'
  log_user 0
  set send_human {.1 .3 1 .05 2}
  spawn sftp [exec sed "1p;d" .credentials_tmp]@[exec sed "3p;d" .credentials_tmp]:[exec sed "4p;d" .credentials_tmp]
  set timeout 30
  expect {
    "Password:" {
    # exp_continue
    } timeout {
    send_user "
  *** Connection timed out. Check connection and verify the username/password"; exit 1
    }
  }
  puts "Authenticating"
  send "[exec sed "2p;d" .credentials_tmp]\n"
  #log_user 1
  expect {
    "sftp>" {
    # exp_continue
    } timeout {
    send_user "
  *** Connection timed out. Check connection"; exit 1
    }
  }
  puts "Getting Remote File List"
  log_file -a -noappend .RemoteFileList.txt
  send "ls -1\n"
  expect {
    "sftp>" {
    # exp_continue
    } timeout {
    send_user "
  *** Connection timed out. Check connection"; exit 1
    }
  }
  log_file
  system sed -i '' '/ls -1/d' ./.RemoteFileList.txt
  system sed -i '' '/sftp>/d' ./.RemoteFileList.txt
  puts "Comparing with files in [exec sed "5p;d" .credentials_tmp]"
  sleep 1
  set rc [catch {exec diff -aw --changed-group-format=%< --unchanged-group-format= .LocalFileList.txt .RemoteFileList.txt} output]
  if {$rc == 0} {
    puts "no difference"
  } else {
    if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
      if {[lindex $::errorCode 2] == 1} {
        # send output without "child process exited abnormally" message
        set filename ".newfiles.txt"
        set fileId [open $filename "w"]
        puts -nonewline $fileId [string replace $output end-31 end ""]
        close $fileId
      } else {
        puts "diff error: $output"
      }
    } else {
      puts "error calling diff: $output"
    }
  }
  sleep 1
  if {[file size .newfiles.txt] == 0} {
    sleep 1
    send "bye\n"
    puts "
There are no local files missing from the remote server"
    interact
  } else {
    set f [open ".newfiles.txt"]
    while {[gets $f line] != -1} {
      send "put [exec sed "5p;d" .credentials_tmp]/$line\n"
      puts "Uploading $line..."
      expect {
        "sftp>" {
        # exp_continue
        } timeout {
        send_user "
      *** Connection timed out. Check connection"; exit 1
        }
      }
  }
  close $f
  send "bye\r"
  puts "
Finished syncing from [exec sed "5p;d" .credentials_tmp] to sftp://[exec sed "3p;d" .credentials_tmp]:[exec sed "4p;d" .credentials_tmp]"
  }
EOS
security find-generic-password -s _$SERVICE_sftp >/dev/null 2>/dev/null #test if entry exists in osx keychain
if [ $? -ne 0 ]; then
  while true; do
  read -p "
Do you want to save these login details for next time?
y/n " yn
    case $yn in
      [Yy]* )
        security add-generic-password -s _$SERVICE_sftp -a `sed '1p;d' .credentials_tmp` -w `sed '2p;d' .credentials_tmp` -U
        rm -f .credentials_tmp
        echo "credentials saved."
        break
        ;;
      [Nn]* )
        rm -f .credentials_tmp
        exit 1
        ;;
      * ) echo "Please answer yes or no.
y/n ";;
    esac
  done
fi
rm -f .credentials_tmp
rm -f .newfiles.txt
rm -f .LocalFileList.txt
rm -f .RemoteFileList.txt
}

# From here down is all to do with handling the flags you type into the command line, after the name of the script.
if [ ! $# == 6 ] || [ "$1" = "-h" ] || [ "$1" = "-" ]
then
  usage
fi

while getopts ":s:l:r:" opt; do
  case $opt in
    s) SERVICE=$OPTARG;;
    l) LOCALPATH=$OPTARG;;
    r) REMOTEPATH=$OPTARG;;
    \?)
      echo "Invalid option: -$OPTARG
      type ./"$bname" -h for help
      " >&2
      exit 1
      ;;
  esac
  #shift $(( OPTIND - 1 ))
done
upload $SERVICE $LOCALPATH $REMOTEPATH

要使用,只需将代码保存到文件中,例如 uploadsftp.sh 并运行chmod u+x uploadsftp.sh

然后,您可以将 cron 设置为 cd 进入脚本目录并运行脚本:./uploadsftp.sh -s SERVICE -l LOCALPATH -r REMOTEPATH无论何时您需要它。

相关内容