How do I copy a directory to a remote machine using Fabric?

PythonFabric

Python Problem Overview


I have a directory on my local machine that I would like to copy to a remote machine (and rename it) using Fabric. I know I can copy file using put(), but what about a directory. I know it's easy enough using scp, but I would prefer to do it from within my fabfile.py if possible.

Python Solutions


Solution 1 - Python

You can use put for that as well (at least in 1.0.0):

> local_path may be a relative or absolute local file or directory path, and may contain shell-style wildcards, as understood by the Python glob module. Tilde expansion (as implemented by os.path.expanduser) is also performed.

See: http://docs.fabfile.org/en/1.0.0/api/core/operations.html#fabric.operations.put


Update: This example works fine (for me) on 1.0.0.:

from fabric.api import env
from fabric.operations import run, put

env.hosts = ['[email protected]']

def copy():
    # make sure the directory is there!
    run('mkdir -p /home/frodo/tmp')

    # our local 'testdirectory' - it may contain files or subdirectories ...
    put('testdirectory', '/home/frodo/tmp')

# [frodo@middleearth.com] Executing task 'copy'
# [frodo@middleearth.com] run: mkdir -p /home/frodo/tmp
# [frodo@middleearth.com] put: testdirectory/HELLO -> \
#     /home/frodo/tmp/testdirectory/HELLO
# [frodo@middleearth.com] put: testdirectory/WORLD -> \
#     /home/frodo/tmp/testdirectory/WORLD
# ...

Solution 2 - Python

I would also look at the Project Tools module: fabric.contrib.project http://docs.fabfile.org/en/latest/api/contrib/project.html">Documentation</a>

This has an upload_project function which takes a source and target directory. Even better, there is an rsync_project function that uses rsync. This is nice because it only updates the files that have changed and it accepts extra args like "exclude" which is nice for doing things like excluding your .git directory.

For example:

from fabric.contrib.project import rsync_project

def _deploy_ec2(loc):

    rsync_project(local_dir=loc, remote_dir='/var/www', exclude='.git')

Solution 3 - Python

For those using Fabric 2, put can no longer upload directories, only files. Also, rsync_project is no longer part of the main Fabric package. The contrib package has been removed, as explained here. Now, rsync_project has been renamed to rsync, and you need to install another package in order to be able to use it:

pip install patchwork

Now, assuming you already have created a connection to your server:

cxn = fabric.Connection('username@server:22')

You can use rsync as below:

import patchwork.transfers
patchwork.transfers.rsync(cxn, '/my/local/dir', target, exclude='.git')

Please refer to the fabric-patchwork documentation for more information.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongaviscon_manView Question on Stackoverflow
Solution 1 - PythonmikuView Answer on Stackoverflow
Solution 2 - PythonSeth GottliebView Answer on Stackoverflow
Solution 3 - PythonTGOView Answer on Stackoverflow