Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to a Remote file with Fabric

Tags:

python

fabric

I am trying to backup databases and move them around to different servers using 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'

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")

EDIT: 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()
like image 963
JiminyCricket Avatar asked Dec 16 '22 13:12

JiminyCricket


1 Answers

In Fabric, you are never "on a remote server". Some Fabric commands run locally, and some run on the remote server. In this case, you are using Python's open function, which tries to open the file on your local computer, and understandably fails. You can use Fabric's put and get functions to move files between your local computer and the remote server.

like image 143
Yavar Avatar answered Dec 19 '22 04:12

Yavar