Advertisement
metalni

[self_coded py script] ftp_sync

Jun 27th, 2021
1,441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. """
  2. Synchronize local files with remote files via FTP Protocol using python
  3.  
  4. This script automatically synchronizes local with remote folder, checking for already synced files along the way(If the file is already there, it will not re-upload it again) and also generates & uploads a summary report of which songs were last synced.
  5.  
  6. Usage: python3 ftp_sync.py
  7.  
  8. All you need to do is change line 10, 11, 12, 13 (you need to fill these variables with your own ftp information) and lines 21 and 22(to set local and remote folder for synchronization).
  9.  
  10. I hope this will be some sort of help to you. Enjoy.
  11. """
  12.  
  13. import os
  14. from ftplib import FTP
  15. from datetime import datetime as date
  16. import time
  17.  
  18. # Start Elapsed timer
  19. start = time.time()
  20.  
  21. # FTP Information
  22. server = 'host'
  23. port = 21 # Set custom port
  24. username = 'username'
  25. password = 'password'
  26.  
  27. # Connect to Remote FTP Server
  28. ftp = FTP()
  29. ftp.connect(server, port)
  30. ftp.login(username, password)
  31.  
  32. # Set local and remote path
  33. local_path = r'Absolute path of local directory'
  34. remote_path = r'Absolute path of remote directory'
  35. ftp.cwd(remote_path)
  36.  
  37. # Open summary file
  38. summary = open(local_path + "\summary.txt", "w")
  39. summary.close()
  40. summary = open(local_path + "\summary.txt", "a")
  41. summary.write("-----------------------------")
  42. summary.write(" Last sync: " + date.now().strftime("%H:%M:%S - %B %d, %Y") + " ")
  43. summary.write("-----------------------------\n")
  44.  
  45.  
  46. # Recursive function that handles file uploading to remote server
  47. def upload_song(path):
  48.     os.chdir(path)
  49.     files = os.listdir(path)
  50.  
  51.     for f in files:
  52.         if os.path.isfile(path + r'\{}'.format(f)):
  53.             if f not in ftp.nlst():
  54.                 fh = open(f, 'rb')
  55.                 print("----> Now uploading: " + f.title())
  56.                 ftp.storbinary('STOR ' + f, fh, 262144)
  57.                 summary.write("----> " + f.title() + "\n")
  58.                 fh.close()
  59.             else:
  60.                 print("----> Song " + f.title() + " already synced")
  61.                 summary.write("----> " + f.title() + "\n")
  62.  
  63.         elif os.path.isdir(path + r'\{}'.format(f)):
  64.             if f not in ftp.nlst():
  65.                 ftp.mkd(f)
  66.             ftp.cwd(f)
  67.             print("\n->Now uploading folder: " + f.title())
  68.             summary.write("-> " + f.title() + "\n")
  69.             upload_song(path + r'\{}'.format(f))
  70.     ftp.cwd('..')
  71.     os.chdir('..')
  72.  
  73.  
  74. upload_song(local_path)  # call the recursive function
  75. print("Successfully synced all local files with remote directory")
  76.  
  77. # End the Elapsed Timer
  78. end = time.time()
  79. elapsed = end - start
  80. print(
  81.     "Total Time Elapsed: %02d hours, %02d minutes & %02d seconds" % (elapsed // 3600, elapsed // 60 % 60, elapsed % 60))
  82.  
  83. summary.write("\nTotal Time Elapsed: %02d hours, %02d minutes & %02d seconds" % (
  84.     elapsed // 3600, elapsed // 60 % 60, elapsed % 60))
  85. summary.close()
  86.  
  87. # Upload the summary file
  88. print("Uploading summary file")
  89. fh = open(local_path + '\summary.txt', 'rb')
  90. ftp.cwd(remote_path)
  91. ftp.storbinary('STOR ' + 'summary.txt', fh, 262144)
  92. fh.close()
  93. ftp.close()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement