我的一个项目总是需要部署到服务器上,scp啥的慢的很,Dolphin自带的ftp又不稳还慢,git太麻烦,所以想到了用rsync。需要装rsync,效果非常好。用法啥的已经写在代码里了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
""" A simple script to sync files with your server. 2023 zjkimin.
requires: rsync
USEAGE: rsync2server push #push localfiles to remote server rsync2server push -d #push localfiles to remote server (dry run) rsync2server pull #pull remote files to localproject rsync2server pull -d #pull remote files to localproject (dry run) """
import sys import os
LOCAL_PATH = './' REMOTE_PATH = 'user@server:/path/to/'
IGNORED_PATHS = [ '**/__pycache__', '**/.*', 'dev/' ]
RSYNC_FLAGS = [ '-av', '--delete' ]
RSYNC_CMD = 'rsync'
def exec_and_log(*args,**kwargs): print('$>>>',*args,**kwargs) os.system(*args)
if __name__=='__main__': mode = 1 RSYNC_FLAGS.extend([f'--exclude "{ignored}"' for ignored in IGNORED_PATHS]) for arg in sys.argv: if arg == 'push': mode += 1 elif arg == 'pull': mode += 2 if mode == 1 or mode > 3: print('mode unsupport') exit(1) for arg in sys.argv: if arg == '-d' or arg == '--dry': RSYNC_FLAGS.append('-n') tmp = [LOCAL_PATH,REMOTE_PATH] RSYNC_FLAGS.extend( tmp if mode == 2 else reversed(tmp) ) final_cmd = ' '.join([RSYNC_CMD]+RSYNC_FLAGS) exec_and_log(final_cmd)
|