Last active
July 4, 2018 11:08
-
-
Save BatuhanKucukali/0bc93b08b7c3e4ccb340 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment