Bjoern Olausson

ftp_backup.py
Friday, 24 June 2011 19:31

So you are using ispCP Omega to host Domains/Webspace and are looking for a simple offsite backup via FTP for the backup files  created by ispCP?

Well, at least I was and I couldn't find one which fitted my needs, so I decided to hack one by myself.

The script requires nothing but Python by using the Python ftplib

 

I am currently testing version 1.0 myself, so be a bit careful with v1.0!

ftp_backup.py Version:1.0

This Python script just copies all the backupfiles from ispCP Omega for the specified domains to a remote FTP server.

The script is intended to be run on a daily basis and you can configure how many days back you want to keep the backups.

If something goes wrong, the script will notify you via e-mail and bail out.

 GNU/GPL    2011-06-24   English   Linux  4.43 KB  126

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/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()
 
Last Updated ( Saturday, 25 June 2011 09:22 )
 

Comments  

 
0 #2 Bjoern Olausson 2011-11-01 14:54
Looks like this is not the full script. The comments are limited to 1000 characters.

You can send me the script and I'll publish it for you here.

Never the less, the bash script you posted produces no feedback nor does it allow to select domains to backup.

Cheers,
Bjoern
Quote
 
 
0 #1 John Powell 2011-11-01 14:37
Here is a good script I use. Can't remember where I got it from but it will transfer all the backup folders to a remote FTP site. needs to be run as root.

#!/bin/bash

hostname=""
username=""
password=""
keepage="14 days"
remotedir="ispcpbackups/"

# Change paths for your system, here for debian
date=/bin/date
ncftp=/usr/bin/ncftp
ncftpput=/usr/bin/ncftpput

# Do not edit below

today=$($date +%Y%m%d)
old=$($date -d "$keepage ago" +%Y%m%d)

$ncftp -u $username -p $password $hostname
Quote
 

Add comment


Security code
Refresh

Comments

Qt Ambassador

Qt Ambassador

www. is deprecated

Banner

Play OGG

Banner

Gixen

web2sms

Banner