[英]How do I copy a directory to a remote machine using Fabric?
我在本地計算機上有一個目錄,我想使用Fabric將其復制到遠程計算機(並重命名)。 我知道我可以使用put()
復制文件,但是目錄呢。 我知道使用scp很容易,但如果可能的話,我更願意在fabfile.py
進行。
您也可以使用put
(至少在1.0.0中):
local_path
可以是相對或絕對本地文件或目錄路徑 ,並且可以包含shell樣式的通配符 ,如Python glob模塊所理解的。 還執行了Tilde擴展(由os.path.expanduser實現)。
請參閱: http : //docs.fabfile.org/en/1.0.0/api/core/operations.html#fabric.operations.put
更新:此示例在1.0.0上工作正常(對我而言):
from fabric.api import env
from fabric.operations import run, put
env.hosts = ['frodo@middleearth.com']
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
# ...
我還會看一下Project Tools模塊:fabric.contrib.project 文檔
這有一個upload_project
函數,它接受一個源目錄和一個目標。 更好的是,有一個使用rsync的rsync_project
函數。 這很好,因為它只更新已更改的文件,並接受額外的args,如“exclude”,這對於排除.git
目錄這樣做很有用。
例如:
from fabric.contrib.project import rsync_project
def _deploy_ec2(loc):
rsync_project(local_dir=loc, remote_dir='/var/www', exclude='.git')
對於那些使用Fabric 2的人來說, put
不能再上傳目錄,只能上傳文件。 此外, rsync_project
不再是主要Fabric包的一部分。 的contrib
包已被刪除, 因為這里解釋 。 現在, rsync_project
已重命名為rsync
,您需要安裝另一個包才能使用它:
pip install patchwork
現在,假設您已經創建了與服務器的連接:
cxn = fabric.Connection('username@server:22')
你可以使用rsync
如下:
import patchwork.transfers
patchwork.transfers.rsync(cxn, '/my/local/dir', target, exclude='.git')
有關更多信息,請參閱fabric-patchwork文檔 。
根據TGO的回答,如果您希望在Windows系統上使用它,則需要一個rsync源,例如cygwin。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.