-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.py
48 lines (36 loc) · 1.23 KB
/
rsync.py
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
#!/usr/bin/env python
"""
rsync data
Author: Ryan Feathers jrf296
Date: 11/29/2021
"""
import os, sys, subprocess, pipes, time, argparse
parser = argparse.ArgumentParser()
parser.add_argument('--u', type=str, required=True, help='remote user name')
parser.add_argument('--i', type=str, required=True, help='remote ip address')
parser.add_argument('--p', type=str, required=True, help='full path to data dir')
parser.add_argument('--d', type=str, required=True, help='full path to remote dest')
args = parser.parse_args()
def exists_remote(host, path):
"""Test if a file exists at path on a host accessible with SSH."""
status = subprocess.call(
['ssh', host, 'test -f {}'.format(pipes.quote(path))])
if status == 0:
return True
if status == 1:
return False
raise Exception('SSH failed')
ssh = args.u+"@"+args.i
datadir = args.d
rsync = "rsync -aP "+args.p+" "+ssh+":"+datadir
while True:
try:
os.system(rsync)
except:
print("rsync command failed:"+rsync)
time.sleep(300)
current_time = time.strftime("%H:%M:%S", time.localtime())
print('\n'+'Directory is current as of: '+current_time+'\n')
if exists_remote(ssh, datadir+"stop"):
break
print("Stop file detected, ending rsync"+'\n')