#!/bin/env python # -*- coding: utf-8 -*- # #------------------------------------------------------------------------------ #ftp_backup.py v1.0, Copyright Bjoern Olausson #------------------------------------------------------------------------------ #This program is free software; you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation; either version 2 of the License, or #(at your option) any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #To view the license visit #http://www.gnu.org/licenses/old-licenses/gpl-2.0.html #or write to #Free Software Foundation, Inc. #51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ import time import os import smtplib import sys from ftplib import FTP from glob import glob from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders HOST = "" HOST_BACKUP_PATH = "/ispcp" USER = "" PASSWORD = "" DOMAINS_TO_BACKUP = ["domaina.tld", "domainb.tld"] PATH_TO_VIRTUAL = "/var/www/virtual" DAYS_TO_KEEP = 3 USE_MAIL = True mail_user = "" mail_pwd = "" mail_server = "" mail_smtp_port = "25" # 465 mail_from = "" mail_to = "" mail_subject = "Backup Failed" def mail(text, attach="false"): # http://kutuma.blogspot.com/2007/08/sending-emails-via-gmail-with-python.html if not USE_MAIL: raise Exception(text) msg = MIMEMultipart() msg['From'] = mail_user msg['To'] = mail_to msg['Subject'] = mail_subject msg.attach(MIMEText(text)) if attach != "false": part = MIMEBase('application', 'octet-stream') part.set_payload(open(attach, 'rb').read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attach)) msg.attach(part) mailServer = smtplib.SMTP(mail_server, int(mail_smtp_port)) mailServer.ehlo() try: mailServer.starttls() except Exception, e: pass else: mailServer.ehlo() mailServer.login(mail_user, mail_pwd) mailServer.sendmail(mail_user, mail_to, msg.as_string()) try: # Should be mailServer.quit(), but that crashes... mailServer.quit() except Exception, e: mailServer.close() sys.exit(1) timestr = time.strftime("%Y%m%d_%H%M%S", time.localtime()) TODAY = int(time.strftime("%Y%m%d")) BACKUP_DICT = {} for DOM in DOMAINS_TO_BACKUP: DIR = "%(VIRTUAL)s/%(DOMAIN)s/backups" \ % {"VIRTUAL": PATH_TO_VIRTUAL, "DOMAIN": DOM} for FILE in glob(DIR + "/*"): FILE_NAME = os.path.basename(FILE) BACKUP_NAME = "%(DOMAIN)s_-_%(DATE)s_-_%(FILENAME)s" \ % {"DOMAIN": DOM, "DATE": timestr, "FILENAME": FILE_NAME} BACKUP_DICT[BACKUP_NAME] = FILE if len(BACKUP_DICT) == 0: e = "No files found to backup" print e mail(e) try: ftp = FTP(HOST, timeout=10) except Exception, e: CONNECTED = False RECONNECTS = 1 while not CONNECTED and (RECONNECTS <= 3): try: ftp = FTP(HOST, timeout=10) except Exception, e: pass else: CONNECTED = True if not CONNECTED and RECONNECTS >= 3: print e mail(e) RECONNECTS += 1 try: lgn = ftp.login(USER, PASSWORD) except Exception, e: LOGGEDIN = False RETRIES = 1 while not LOGGEDIN and (RETRIES <= 3): try: ftp = FTP(HOST, timeout=10) except Exception, e: pass else: LOGGEDIN = True if not LOGGEDIN and RETRIES >= 3: print e mail(e) RETRIES += 1 try: ftp.mkd(HOST_BACKUP_PATH) except Exception, e: if str(e) == "550 /ispcp: File exists": pass else: print e mail(e) try: ftp.cwd(HOST_BACKUP_PATH) except Exception, e: print e mail(e) for ITEM in BACKUP_DICT: FILE = BACKUP_DICT[ITEM] FILE_NAME = os.path.basename(FILE) F = open(FILE, "r") try: ftp.storbinary("STOR %(ITEM)s" % {"ITEM": ITEM}, F) except Exception, e: print e mail(e) F.close() FILES_ON_HOST = [] try: FILES_ON_HOST = ftp.nlst() except Exception, e: print e mail(e) FILES_ON_HOST.sort() for FILE in FILES_ON_HOST: CREATE_DAY = int(FILE.split("_-_")[1].split("_")[0]) DAY_DIFF = TODAY - CREATE_DAY if DAY_DIFF > DAYS_TO_KEEP: try: ftp.delete(FILE) except Exception, e: print e mail(e) try: ftp.quit() except Exception, e: ftp.close()