使用Fabric写入远程文件 [英] Write to a Remote file with Fabric

查看:118
本文介绍了使用Fabric写入远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Fabric .

在远程服务器上时,打开文件进行写入失败,并显示错误.

When on a remote server, to open a file for writing it fails with the error.

newFile = open('%s%s' % (dumpPath,newFileName) ,'w')
IOError: [Errno 2] No such file or directory: '/home/ec2-user/dbbackup.sql.bz2'

该文件存在,我什至尝试事先创建,以防Fabric没有创建权限,但仍然无法正常工作

That files exists, and I even tried creating beforehand just in case fabric didnt have permissions to create, but it still didnt work

 run("touch dbbackup.sql.bz2")

我知道我可以将文件上传到远程服务器上,但这不是我要使用open命令执行的操作.我正在尝试压缩一个大文件(数据库转储),是否可以在远程服务器上执行此操作,还是我必须将数据库转储复制到本地主机,在那进行压缩,然后再上传回来.这是本地主机上的压缩:

I know that I can upload files on to a remote server but thats not what I am trying to do with the open command. I am trying to compress a large file (a database dump) Is it possible to do this on the remote server, or would I have to copy the DB dump to the local host, compress there and then upload back. Here is compression on local host:

compObj= bz2.BZ2Compressor()
newFile = open('%s%s' % (dumpPath,newFileName) ,'w')
dbFile = file( '%s%s' % (dumpPath,filename), "r" )
block= dbFile.read( BLOCK_SIZE )
while True: #write the compressed data
        cBlock= compObj.compress( block )
        newFile.write(cBlock)
        block= dbFile.read( BLOCK_SIZE )
        if not block:
            break
    cBlock= compObj.flush()

推荐答案

我不知道您是否可以远程打开文件.但是即使可以,这也不是一个好主意,因为您将通过ssh来获取大文件(请记住,Fabric仍在本地计算机上运行).为什么不远程压缩文件,然后获取压缩文件?如果是mysqldump,则如下所示:

I don't know if you can open a file remotely. But even if you can, it may not be a good idea in your case, since you will be fetching the large file over ssh (remember that Fabric is still running on your local machine). Why not compress the file remotely, and then get the compressed file? In case of mysqldump, it would look like this:

run('mysqldump [options] | gzip > outputfile.sql.gz')
get('outputfile.sql.gz')

(有关mysqldump和gzip的更多信息,请参见:压缩mysqldump输出)

(more on mysqldump and gzip here: Compressing mysqldump output )

这篇关于使用Fabric写入远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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