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

查看:23
本文介绍了如何通过 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 认为它成功报告了所有问题,所以它以一个快乐的零退出.

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 发送文件,然后将其拉回本地尚不存在的临时文件.如果之后你可以成功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天全站免登陆