Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from ftplib import FTP
  5.  
  6. ########### Local Path ####################
  7.  
  8. path = 'backup/'
  9.  
  10. ########### MODIFY ########################
  11.  
  12. USER = ''
  13. PASS = ''
  14.  
  15. ########### MODIFY IF YOU WANT ############
  16.  
  17. SERVER = ''
  18. PORT = 21
  19. BINARY_STORE = True # if False then line store (not valid for binary files (videos, music, photos...))
  20.  
  21. ###########################################
  22.  
  23. def print_line(result):
  24. print(result)
  25.  
  26. def connect_ftp():
  27. #Connect to the server
  28. ftp = FTP()
  29. ftp.connect(SERVER, PORT)
  30. ftp.login(USER, PASS)
  31.  
  32. return ftp
  33.  
  34. def upload_file(ftp_connetion, upload_file_path):
  35.  
  36. #Open the file
  37. try:
  38. upload_file = open(upload_file_path, 'r')
  39.  
  40. #get the name
  41. path_split = upload_file_path.split('/')
  42. final_file_name = path_split[len(path_split)-1]
  43.  
  44. #transfer the file
  45. print('Uploading ' + final_file_name + '...')
  46.  
  47. if BINARY_STORE:
  48. ftp_connetion.storbinary('STOR '+ final_file_name, upload_file)
  49. else:
  50. #ftp_connetion.storlines('STOR ' + final_file_name, upload_file, print_line)
  51. ftp_connetion.storlines('STOR '+ final_file_name, upload_file)
  52.  
  53. print('Upload finished.')
  54.  
  55. except IOError:
  56. print ("No such file or directory... passing to next file")
  57.  
  58.  
  59. #Take all the files and upload all
  60.  
  61. #backup path
  62. os.chdir(path)
  63.  
  64. # backup folder
  65. files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
  66.  
  67. # newest file
  68. send_file = files[-1]
  69.  
  70. # upload ftp
  71. ftp_conn = connect_ftp()
  72. upload_file(ftp_conn, send_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement