如何通过bash脚本连接到ftp服务器? [英] How to connect to a ftp server via bash script?

查看:92
本文介绍了如何通过bash脚本连接到ftp服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个bash脚本,用于将备份文件从服务器上传到ftp服务器.但是我总是会出错.

I wrote a bash script for uploading backup files from a server to a ftp server. But I always get an error.

Name (myftpserver:root): Permission denied.
Login failed.
Login with USER first.
Please login with USER and PASS.
Local directory now /backup01
Please login with USER and PASS.
Passive mode refused.

那是我的剧本:

#!/bin/bash

DATE=`date +%Y-%m-%d_%H%M`
LOCAL_BACKUP_DIR="/backup01"
DB_NAME="databasename"
DB_USER="root"

FTP_SERVER="randomIP"
FTP_USERNAME="myname"
FTP_PASSWORT="supersecret"
FTP_UPLOAD_DIR="/home/mydirectory/ftp/upload"

LOG_FILE=/backup01/backup-$DATE.log

mysqldump -u $DB_USER  $DB_NAME | gzip  > $LOCAL_BACKUP_DIR/$DATE-$DB_NAME.sql.gz

ftp $FTP_SERVER << END_FTP
quote USER $FTP_USERNAME 
quote PASS $FTP_PASSWORD
cd $FTP_UPLOAD_DIR
lcd $LOCAL_BACKUP_DIR
put "$DATE-$DB_NAME.sql.gz"
bye
END_FTP


if test $? = 0
then
    echo "Database successfully uploaded to the FTP Server!"
    echo "Database successfully created and uploaded to the FTP Server!" | mail -s "Backup from $DATE" my.email@whereever.com

else
    echo "Error in database upload to Ftp Server" > $LOG_FILE
fi

也许有人可以帮助我,因为我已经尝试了在互联网上找到的所有内容.我已经制作了一个.netrc文件.我配置了vsftpd.conf,启用了被动模式,启用了用户列表,并且做了很多其他的事情...但是现在我不知道要使此脚本按应有的方式工作还需要做什么.而且我不知道为什么它试图通过root连接...

Maybe someone can help me, because I've tried everything I've found on the internet. I've made a .netrc file. I configured the vsftpd.conf, enabled passiv mode, enabled user list and I've made a lot of other stuff... But now I'm having no idea what else I have to do to make this script working the way it should. And I have no idea why it's trying to connect via root...

也许那里有人可以提供帮助.预先感谢.

Maybe there is someone out there who can help. Thanks in advance.

推荐答案

使用这样的东西是常见的主意:

It's a common staple to use something like this:

$: ftp -vn <<!
> open localhost
> user foo
> put someFile
> quit
> !
> ftp: connect :Connection refused
Not connected.
Not connected.
Not connected.
$: echo $?
0

不幸的是,ftp认为它已成功报告了所有问题,因此以0开头退出.

Unfortunately ftp considers that it successfully reported all problems, so it exits with a happy zero.

使用 scp :

if scp "$lcldir/$filename" "$usr/$pw@$svr:$dir/"
then echo "file delivered"
else echo "delivery failed"
fi

如果您不能使用 scp ,请尝试使用 expect 之类的方法,或在 Perl 中编写一些内容-交互式测试和确认每个步骤的方式.

If you can't use scp try something like expect, or write something in Perl - some way you can interactively test and confirm each step.

作为最后的选择,使here-doc发送文件,然后将其拉回到本地尚不存在的tempfile.如果之后可以成功 cmp file1 file2 ,则说明发送正常.

As a last resort, make the here-doc send the file and then pull it back to a tempfile that doesn't already exist locally. If you can successfully cmp file1 file2 afterwards, the send must have worked ok.

这篇关于如何通过bash脚本连接到ftp服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆