Created
June 4, 2023 03:23
-
-
Save yudhiwidyatama/f6cf9a00b6d2edd4a040ecc95098991a to your computer and use it in GitHub Desktop.
python code to copy file to remote server using sftp, with 5x retry
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
import subprocess | |
import warnings | |
# Suppress the deprecation warning from the cryptography module. | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") | |
import cryptography | |
import sys | |
import pysftp | |
import paramiko | |
import os | |
import logging | |
import sys | |
import time | |
def setup_custom_logger(name): | |
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s', | |
datefmt='%Y-%m-%d %H:%M:%S') | |
handler = logging.FileHandler('log.txt', mode='w') | |
handler.setFormatter(formatter) | |
screen_handler = logging.StreamHandler(stream=sys.stdout) | |
screen_handler.setFormatter(formatter) | |
logger = logging.getLogger(name) | |
logger.setLevel(logging.DEBUG) | |
logger.addHandler(handler) | |
logger.addHandler(screen_handler) | |
return logger | |
l = setup_custom_logger('filesync') | |
for loopcount in range(5): | |
try: | |
ssh_client = paramiko.SSHClient() | |
ssh_client.load_system_host_keys() | |
ssh_client.connect(hostname="x.y.a.b",username="user",password="pass") | |
sftp = ssh_client.open_sftp() | |
thedir = sys.argv[1] | |
srcfile = sys.argv[2] | |
destfile = sys.argv[3] | |
filestat_src = os.stat(srcfile) | |
# thedir = "TEST"+thedir | |
fullfile = thedir+"/"+destfile | |
# print("thedir="+thedir+";srcfile="+srcfile+";destfile="+destfile) | |
try: | |
filestat=sftp.stat(thedir) | |
destPathExists = True | |
except Exception as e: | |
destPathExists = False | |
if destPathExists == False: | |
sftp.mkdir(thedir) | |
try: | |
filestat1=sftp.stat(fullfile) | |
destFileExists = True | |
except Exception as e: | |
destFileExists = False | |
doCopy = True | |
if destFileExists: | |
l.info( "file "+fullfile+" exists with size " + str(filestat1.st_size) + "( source size = " + str(filestat_src.st_size) +")") | |
if filestat1.st_size != filestat_src.st_size: | |
l.error("size mismatch, resending") | |
else: | |
doCopy = False | |
break | |
if doCopy: | |
l.info("copying "+srcfile + " to " + fullfile) | |
try: | |
sftp.chdir(thedir) | |
sftp.put(srcfile,destfile) | |
l.info("completed") | |
except Exception as e: | |
l.error("error occured") | |
continue | |
sftp.close() | |
ssh_client.close() | |
except Exception as e1: | |
l.error("error occurred") | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment