皮肌炎不能吃哪些食物:QingSina Feelings - Windows下用sftp自动下载文件

来源:百度文库 编辑:中财网 时间:2024/04/30 15:06:51
Windows下用sftp自动下载文件
By Qings  
发布: 2007-3-10
打印   

远程服务器是linux操作系统,没有ftp服务,可以ssh,数据库每天2:00会自动创建一个备份文件,本地计算机是windows操作系统,希望用sftp每天3:00下载远程服务器上的备份文件。本地系统是linux的,可以参考另一篇文章“linux下自动sftp下载文件”。

Windows下的sftp工具采用putty工具包中的psftp.exe,下载地址:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

命令行下输入“psftp -h”可以查看psftp的用法。

PuTTY Secure File Transfer (SFTP) client
Release 0.59
Usage: psftp [options] [user@]host
Options:
  -V        print version information and exit
  -pgpfp    print PGP key fingerprints and exit
  -b file   use specified batchfile
  -bc       output batchfile commands
  -be       don't stop batchfile processing if errors
  -v        show verbose messages
  -load sessname  Load settings from saved session
  -l user   connect with specified username
  -P port   connect to specified port
  -pw passw login with specified password
  -1 -2     force use of particular SSH protocol version
  -4 -6     force use of IPv4 or IPv6
  -C        enable compression
  -i key    private key file for authentication
  -noagent  disable use of Pageant
  -agent    enable use of Pageant
  -batch    disable all interactive prompts

可以看到,我们能够通过导入一个脚本文件sftp.txt来自动完成sftp下载文件。命令格式如下:

psftp remotehost -l username -pw password < sftp.txt<><><>

现在的问题是,远程服务器的备份文件采用的文件名是自动创建的,存放在/logs目录,文件名格式为“dumpyyyymmdd.log”,比如dump20070310.log,那么这个每天变化的文件名称如何能传送到脚本文件sftp.txt中呢?

只好google了,终于找到一个可以将date/time用于批处理文件的程序realdate.com,下载地址:
http://www.huweb.hu/maques/realdate.htm

好了,现在开始写批处理脚本sftp.bat。

@echo off

# 写psftp需要的脚本文件sftp.txt
for /f %%i in ('realdate.com /d') do (set remotelogname=%%i)
echo cd /logs > sftp.txt
echo get dump%remotelogname%.log >> sftp.txt
echo bye >> sftp.txt

# 写日志文件sftp.log
echo --------------------------------------- >> sftp.log
for /f %%i in ('realdate.com /f="CCYY-MM-DD"') do (set locallogdate=%%i)
for /f %%i in ('realdate.com /f="hh:mm:ss"') do (set locallogtime=%%i)
echo %locallogdate% %locallogtime% >> sftp.log
psftp remotehost -l username -pw password < sftp.txt > sftp.log
echo. >> sftp.log
echo done. >> sftp.log

将psftp.exe、realdate.com和sftp.bat放在同一文件夹中,为sftp.bat建立计划任务,半夜就可以安心睡觉了。