import os
from ftplib import FTP
import subprocess
from datetime import datetime
# config
ftp_uname = '?????????????????'
ftp_pwd = '?????????????'
ftp_server = '111.111.111.111'
# end of config
fileroot = 'DBBackup_'
os.chdir('c:/')
files = []
logfile = open('e:/dbbackup/getmdbackups.log','a+')
def makelist(line):
if (line.startswith(fileroot)):
fs = [line,0]
files.append(fs)
def log(line):
ll = str(datetime.now()) + ' : ' + str(line)
print ll
logfile.write(ll + '\n')
def fileexists(ff, size):
if (os.path.exists(ff)):
stat = os.stat(ff)
if (stat.st_size == size):
return True
return False
try:
# first connect using ftp to get a list of valid backup failes available
log('Connecting to ftp server')
ftp = FTP(ftp_server)
ftp.set_pasv(False)
#ftp.set_debuglevel(2)
resp = ftp.login(ftp_uname,ftp_pwd)
log(resp)
ftp.retrlines('NLST',makelist)
log(str(files))
ftp.quit()
# fetch files in a loop using wget.
for ff in files:
ftp = FTP(ftp_server)
ftp.set_pasv(False)
resp = ftp.login(ftp_uname,ftp_pwd)
log(resp)
size = ftp.size(ff[0])
log('Size of server file = ' + str(size))
ftp.quit()
try:
if (not fileexists(ff[0],size)):
log('Transferring: ' + ff[0])
# make parameters to wget the backup file
params = ' http://' + ftp_server + '/?????????/' + ff[0]
rcode = subprocess.call('e:/wget/wget.exe ' + params)
log('Return code from wget = ' + str(rcode))
if (rcode == 0):
ff[1] = 1
else:
log('File ' + ff[0] + ' already exists locally, not transferring')
ff[1] = 1
except Exception, e:
log(str(e))
log('Transfer complete')
# delete the server files that have been transferred or are already here with the right filesize.
for ff in files:
if (ff[1] == 1):
log('delete ' + ff[0])
except Exception,e:
log(str(e))
# clean up temp files
log('Finished.')
logfile.close()
|