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 t开发者_如何学运维he 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()
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.
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 )
- You need to read the Fabric tutorial again.
- You should be using os.path.join to assemble your filepath.
- That open() call is trying to open the file on your local machine, NOT the remote server.
精彩评论