|
#!/usr/bin/python
# This script walks throught a directory structure and uploads all files afterwards.
# If ARCHIVE or upload is in the name or the path of the file it is skipped.
# You have to set host, username, password, basePath, archivePath, logFile and remotePath
# basePath is the local path which should be backed up. archivePath is used to put the files
# gzipped in an archive locally. remotePath is the path on the server where you want to
# put your archive
from ftplib import FTP
import sys
import re
import os
import time
import gzip
import shutil
host = "hostname"
port = 21
username = "username"
password = "password"
basePath = '/opt/backup/'
archivePath = '/opt/archive/'
logFile= '/opt/ftp.log'
remotePath = './'
allfiles = []
relativefilepaths = []
noarchivfiles = []
temppath = []
##################################################
def walk():
for root, dirs, files in os.walk(basePath):
for f in files:
allfiles.append(os.path.join(root,f))
for file in allfiles:
if "ARCHIVE" not in file and "upload" not in file:
noarchivfiles.append(file)
for file in noarchivfiles:
temppath = re.split(basePath, file)
relativefilepaths.append(temppath[1])
##################################################
def put():
try:
ftp = FTP(host)
ftp.login(username,password)
except:
log("Error in ftp initialization")
for file in relativefilepaths:
path = os.path.join(remotePath, file)
localpath = os.path.join(basePath, file)
try:
f = open(localpath,"r")
ftp.cwd("/")
ftp.cwd(os.path.dirname(file))
ftp.storbinary("STOR"+os.path.basename(file), f)
f.close()
log("Upload done for "+file)
zip(localpath)
except:
log("Error in ftp transaction for "+file)
try:
ftp.close()
except:
log("Error while closing ftp")
##################################################
def log(message):
logfile = open(logFile,"a")
logfile.write(time.ctime()+" :"+message+"\n")
logfile.close
##################################################
def zip(localpath_file):
extention = os.path.splitext(localpath_file)
if extention[1] == ".gz":
try:
archivepath_file = archivePath+os.path.basename(localpath_file)
shutil.move(localpath_file, archivePath+os.path.basename(localpath_file))
log("File not compressed but archived as "+archivepath_file)
except:
log("Error occured while archiving file "+archivepath_file)
else:
archivepath_file = archivePath+os.path.basename(localpath_file)+".gz"
try:
f_in = open(localpath_file, 'rb')
f_out = gzip.open(archivepath_file, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
log("File compressed and archived as "+archivepath_file)
os.remove(localpath_file)
except:
log("Error occured while compressing and archiving file "+archivepath_file)
walk()
put()
|