Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os
- from ftplib import FTP
- ########### Local Path ####################
- path = 'backup/'
- ########### MODIFY ########################
- USER = ''
- PASS = ''
- ########### MODIFY IF YOU WANT ############
- SERVER = ''
- PORT = 21
- BINARY_STORE = True # if False then line store (not valid for binary files (videos, music, photos...))
- ###########################################
- def print_line(result):
- print(result)
- def connect_ftp():
- #Connect to the server
- ftp = FTP()
- ftp.connect(SERVER, PORT)
- ftp.login(USER, PASS)
- return ftp
- def upload_file(ftp_connetion, upload_file_path):
- #Open the file
- try:
- upload_file = open(upload_file_path, 'r')
- #get the name
- path_split = upload_file_path.split('/')
- final_file_name = path_split[len(path_split)-1]
- #transfer the file
- print('Uploading ' + final_file_name + '...')
- if BINARY_STORE:
- ftp_connetion.storbinary('STOR '+ final_file_name, upload_file)
- else:
- #ftp_connetion.storlines('STOR ' + final_file_name, upload_file, print_line)
- ftp_connetion.storlines('STOR '+ final_file_name, upload_file)
- print('Upload finished.')
- except IOError:
- print ("No such file or directory... passing to next file")
- #Take all the files and upload all
- #backup path
- os.chdir(path)
- # backup folder
- files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
- # newest file
- send_file = files[-1]
- # upload ftp
- ftp_conn = connect_ftp()
- upload_file(ftp_conn, send_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement