Created
November 23, 2013 15:20
-
-
Save chappyhome/7615718 to your computer and use it in GitHub Desktop.
利用rsync自动同步服务器与本地代码的python脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
利用rsync自动同步服务器与本地代码的python脚本 | |
四 07 二月 2013 | |
tags: Python | |
菜鸟表示写web时 每次修改完后把文件往VPS上丢很麻烦 | |
用ssh sshfs scp都太慢 小东西又懒得用git | |
sublimetext上得sftp不支持中文 emacs干脆就只能ssh | |
于是用linux自带的rsync写了个同步脚本 | |
可以手动或自动往服务器上差异性同步 不用scp一堆东西 也不用忍受ssh那个渣速度 | |
把脚本copy到你的目录 修改脚本内的配置 如果在脚本内设置了密码 那每次上次就会自动输入密码 否则除非你设置本地key 否则都要输密码 | |
如果代码在服务器上 先执行 : python rsync.py -down 把代码download下来 , 个人觉得先down下来再修改 不用忍受每次load一遍 比较舒服 | |
每次修改完了 可以手动上传:python rsync,py 或者也可以放在后台自动上传:python rsync.py -auto 他会监视改目录的文件 如果新建 修改或删除 都会sync 可以用 nohup python rsync.py -auto& 来后台运行 | |
只根据emacs优化了 vim党自己改 | |
sync时会忽略隐藏文件和临时文件 | |
# use rsync to sync code from server | |
# coding = UTF8 | |
# @author Kleist Zhou | |
# 2013/2/5 | |
# Need pyinotify, pexpect | |
# please use easy_install to add in | |
import os | |
import sys | |
import pyinotify | |
import re | |
# your sync config | |
server = '199.245.60.73' | |
user = 'root' | |
password = '' | |
port = '' | |
source = './' | |
target = '/srv/www/love/public_html/' | |
options = "-rtvuC --delete --progress --exclude='rsync.py'" | |
#custom regular expression | |
rulers = (r"[#~.][\s\S]*", r"[\s\S]*[#~]", r"[\s\S]*_flymake.[\s\S]*") | |
if port != '': | |
port = "-e 'ssh -p %d'" % (port) | |
def runCmd(cmd): | |
global password | |
if password == '': | |
os.system(cmd) | |
else: | |
import pexpect | |
print cmd | |
child = pexpect.spawn(cmd) | |
try: | |
i = child.expect(['password: ', 'continue connecting (yes/no)?']) | |
if i == 0: | |
child.sendline(password) | |
elif i == 1: | |
child.sendline('yes') | |
except pexpect.EOF: | |
child.close() | |
else: | |
child.expect(pexpect.EOF) | |
child.close() | |
def sync(): | |
global server, user, port, source, target, option | |
cmd = "rsync %s %s %s %s@%s:%s" % (port, options, source, user, server, target) | |
#print cmd | |
runCmd(cmd) | |
def download(): | |
global server, user, port, source, target, option | |
cmd = "rsync %s %s %s@%s:%s" % (port, options, user, server, target) | |
print cmd | |
runCmd(cmd) | |
class OnChangeHandler(pyinotify.ProcessEvent): | |
def checkFileName(self, fileName): | |
global rulers | |
for ruler in rulers: | |
p = re.compile(ruler) | |
if p.match(fileName) != None: | |
return False | |
return True | |
def syncFile(self, fileName): | |
if self.checkFileName(fileName): | |
sync() | |
def process_IN_CREATE(self, event): | |
#print "Create file: %s " % os.path.join(event.path,event.name) | |
self.syncFile(event.name) | |
def process_IN_DELETE(self, event): | |
#print "Delete file: %s " % os.path.join(event.path,event.name) | |
self.syncFile(event.name) | |
def process_IN_MODIFY(self, event): | |
#print "Modify file: %s " % os.path.join(event.path,event.name) | |
self.syncFile(event.name) | |
def auto_sync(): | |
wm = pyinotify.WatchManager() | |
mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY | |
notifier = pyinotify.Notifier(wm, OnChangeHandler()) | |
wm.add_watch("./", mask, rec=True, auto_add=True) | |
notifier.loop() | |
def main(): | |
if len(sys.argv) == 2: | |
option = sys.argv[1] | |
if option == '-down': | |
download() | |
elif option == '-auto': | |
auto_sync() | |
else: | |
sync() | |
# main | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment