如何使用find命令输出通过ftp传输目录中的多个文件(到远程) [英] How to make ftp transfer of multiple files in directory (to remote) using find command output
问题描述
我必须做好将超过100个备份 .tar.gz 文件传输到ftp备份服务器的任务.卡在组合查找命令上
I've got to make a cron job of transfering over 100 backup .tar.gz files to ftp backup server. Stuck upon combining find command
find /home/backup -mtime -1 -mmin +59 -type f -name "*.tar.gz*"
这部分工作正常,而脚本部分:
this part works fine, and script part:
#!/bin/sh
USERNAME="user"
PASSWORD="password"
SERVER="someip"
FILE="/home/backup"
DATE="`date +%Y-%m-%d-%H.%M.%S `"
BACKUPDIR="/backup/${DATE}/"
ftp -inv $SERVER <<EOF
user $USERNAME $PASSWORD
mkdir $BACKUPDIR
cd $BACKUPDIR
mput $FILE/*.tar.gz*
quit
EOF
为此
00 12 * * * find /home/backup -mtime -1 -mmin +59 -type f -name "*.tar.gz*" -exec /root/ftp.sh {} \;
不起作用.不需要scp/ssh建议)必须使用ftp.
doesn't work. No scp/ssh advice please) have to do it with ftp.
推荐答案
我建议您减小crontab命令的大小.并不是说它不应该按自己的方式工作,而是会更容易理解正在发生的事情.
I advise you to make the crontab command smaller. Not that it shouldn't working your way, but it will be easier to understand what is happening.
00 12 * * * sh /root/ftpjob.sh
和
#!/bin/sh
username="user"
password="password"
server="someip"
sourcedir="/home/backup"
date="`date +%Y-%m-%d-%H.%M.%S `"
remotedir="/backup/${DATE}/"
find /home/backup -mtime -1 -mmin +59 -type f -name "*.tar.gz*" |
while read filename ; do
/bin/ftp -inv $server >> /tmp/ftpjob.log <<EOF
user $username $password
mkdir $remotedir
cd $remotedir
put $sourcedir/$filename
EOF
echo "$date copied $filename" >> /tmp/ftpjob.log
done
只要您确定tar.gz文件名中没有空格,这将起作用.
This will work, as long as you are sure that your tar.gz filenames don't have spaces in them.
另一方面,如果您能够使用mput进行ftp,则根本没有理由进行查找:
On the other hand, if you are able to do the ftp with an mput, there is no reason to do the find at all:
#!/bin/sh
username="user"
password="password"
server="someip"
sourcedir="/home/backup"
date="`date +%Y-%m-%d-%H.%M.%S `"
remotedir="/backup/${DATE}/"
/bin/ftp -inv $server >> /tmp/ftpjob.log <<EOF
user $username $password
mkdir $remotedir
cd $remotedir
mput $sourcedir/*.tar.gz.*
EOF
因此,您可以使用find
遍历文件,如果tar.gz文件所在的目录有多个级别,这是一个好主意,或者如果在ftp
中使用mput
所有档案始终位于同一目录中.
So, you would either use find
to loop over the files, which is a good idea if there are multiple levels of directories where the tar.gz files are, or you would use mput
in ftp
if all the archives are always in the same directory.
这篇关于如何使用find命令输出通过ftp传输目录中的多个文件(到远程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!