Writing to a remote file using Fabric

I am trying to archive databases and move them to different servers using Fabric .

If on a remote server to open a file for writing, it does not work with an error.

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

These files exist, and I even tried to create them in advance just in case the fabric did not have permission to create, but it still did not work

  run("touch dbbackup.sql.bz2") 

EDIT: I know that I can upload files to a remote server, but that’s not what I'm trying to do with the open command. I am trying to compress a large file (database dump). Is it possible to do this on a remote server, or will I need to copy the DB dump to the local host, compress and then load it back. Here is the compression on the 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() 
+3
source share
3 answers

I do not know if it is possible to open the file remotely. But even if it is possible, this may not be very good in your case, since you will receive a large file on top of ssh (remember that Fabric is still running on your local machine). Why not compress the file remotely, and then get the compressed file? In the case of mysqldump, it would look like this:

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

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

+5
source

In Fabric, you are never "on a remote server." Some Fabric commands run locally, and some run on a remote server. In this case, you use the Python open function, which tries to open the file on your local computer and, for obvious reasons, does not work. You can use Fabric to place and receive functions to move files between the local computer and the remote server.

+7
source
  • You need to read the Fabric tutorial again.
  • You must use os.path.join to build your file area.
  • This open () call tries to open the file on your local computer, not on the remote server.
0
source

All Articles