JiminyCricket
JiminyCricket

Reputation: 7410

Write to a Remote file with 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()

Upvotes: 4

Views: 11997

Answers (3)

Yavar
Yavar

Reputation: 414

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

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

Upvotes: 5

Yavar
Yavar

Reputation: 414

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.

Upvotes: 7

Michael Kent
Michael Kent

Reputation: 1734

  1. You need to read the Fabric tutorial again.
  2. You should be using os.path.join to assemble your filepath.
  3. That open() call is trying to open the file on your local machine, NOT the remote server.

Upvotes: -1

Related Questions