inkedmn
05-03-2002, 05:13 PM
i wrote this for work (a "custom solution" for one of our clients).
it's a very cheesy backup script.
and it works pretty well, i've never used the zipfile module before.
anyway, here's ALL the code :)
#!/usr/bin/env python
# Green Kamut's very basic backup script
# Author: Brett Kelly - inkedmn@inkedmn.net
import os.path, smtplib, sys, string, time, zipfile, shutil
#####################
## VARIABLES
#####################
log = []
log.append('This is an automatically generated message reporting your backup status.\n')
toarchive = 'ziptest'
path = 'c:\\'
server = 'f:\\users\\brett\\'
timenow = time.asctime()
splittime = timenow.split(' ')
date = splittime[1] + splittime[2]
newarchive = 'icv' + date + '.zip'
#####################
## FUNCTIONS
#####################
def addToArchive(file):
try:
print file
zip.write(path + toarchive + '\\' + file)
except zipfile.error, e:
appendToLogFailed('add file to archive')
def appendToLogPassed(task):
log.append(task + ' was completed successfully at ' + timenow + '\n')
def appendToLogFailed(task):
log.append(task + ' did not complete successfully. time now is ' + timenow + '\n')
def checkPath(path):
if os.path.exists(path) == 0:
appendToLogFailed('path verification')
sys.exit()
else:
return 1
def writeLog(log):
file = open('backup.log', 'a')
for item in log:
file.write(item)
file.close()
def getFileList():
files = os.listdir(path + toarchive)
return files
def verifyFile(path, file):
if os.path.exists(path + file) == 1:
appendToLogPassed('new path check')
return 1
else:
appendToLogFailed('new path check')
return 0
def sendMail(log):
msg = ''
toaddr = 'bkelly@alinco.net'
fromaddr = 'gkbackup@alinco.net'
for line in log:
msg += line + '\n'
mail = smtplib.SMTP('mail.alinco.net')
mail.sendmail(fromaddr, toaddr, msg)
mail.quit()
#####################
## THE STUFF, YO
#####################
# verify relevant directories exist
if checkPath(server) == 1:
if checkPath(path + toarchive) == 1:
appendToLogPassed('path verification')
else:
appendToLogFailed('path verification')
sendMail(log)
sys.exit()
# create zip file
try:
zip = zipfile.ZipFile('c:\\' + newarchive, mode= 'w')
except zipfile.error, e:
appendToLogFailed('archive file creation')
sendMail(log)
sys.exit()
# verify file now exists, then add files to archive
if verifyFile('c:\\', newarchive) == 1:
files = getFileList()
try:
for file in files:
addToArchive(file)
appendToLogPassed('add files to new archive')
except zipfile.error, e:
appendToLogFailed('add files to new archive')
zip.close()
else:
appendToLogFailed('check new archive path')
sendMail(log)
sys.exit()
# copy new archive to server
try:
copying = shutil.copy(path + newarchive, server + newarchive)
appendToLogPassed('archive copy')
except (IOError, os.error), message:
appendToLogFailed('archive copy')
sendMail(log)
sys.exit()
# verify that new archive exists
if verifyFile(server, newarchive) == 1:
appendToLogPassed('new archive path verification')
else:
appendToLogFailed('new archive path verification')
sendMail(log)
sys.exit()
# remove local archive
try:
os.system('del ' + path + newarchive)
appendToLogPassed('local archive removal')
except OSError:
appendToLogFailed('local archive removal')
sendMail(log)
sys.exit()
# email log file
sendMail(log)
sys.exit()
:)
it's a very cheesy backup script.
and it works pretty well, i've never used the zipfile module before.
anyway, here's ALL the code :)
#!/usr/bin/env python
# Green Kamut's very basic backup script
# Author: Brett Kelly - inkedmn@inkedmn.net
import os.path, smtplib, sys, string, time, zipfile, shutil
#####################
## VARIABLES
#####################
log = []
log.append('This is an automatically generated message reporting your backup status.\n')
toarchive = 'ziptest'
path = 'c:\\'
server = 'f:\\users\\brett\\'
timenow = time.asctime()
splittime = timenow.split(' ')
date = splittime[1] + splittime[2]
newarchive = 'icv' + date + '.zip'
#####################
## FUNCTIONS
#####################
def addToArchive(file):
try:
print file
zip.write(path + toarchive + '\\' + file)
except zipfile.error, e:
appendToLogFailed('add file to archive')
def appendToLogPassed(task):
log.append(task + ' was completed successfully at ' + timenow + '\n')
def appendToLogFailed(task):
log.append(task + ' did not complete successfully. time now is ' + timenow + '\n')
def checkPath(path):
if os.path.exists(path) == 0:
appendToLogFailed('path verification')
sys.exit()
else:
return 1
def writeLog(log):
file = open('backup.log', 'a')
for item in log:
file.write(item)
file.close()
def getFileList():
files = os.listdir(path + toarchive)
return files
def verifyFile(path, file):
if os.path.exists(path + file) == 1:
appendToLogPassed('new path check')
return 1
else:
appendToLogFailed('new path check')
return 0
def sendMail(log):
msg = ''
toaddr = 'bkelly@alinco.net'
fromaddr = 'gkbackup@alinco.net'
for line in log:
msg += line + '\n'
mail = smtplib.SMTP('mail.alinco.net')
mail.sendmail(fromaddr, toaddr, msg)
mail.quit()
#####################
## THE STUFF, YO
#####################
# verify relevant directories exist
if checkPath(server) == 1:
if checkPath(path + toarchive) == 1:
appendToLogPassed('path verification')
else:
appendToLogFailed('path verification')
sendMail(log)
sys.exit()
# create zip file
try:
zip = zipfile.ZipFile('c:\\' + newarchive, mode= 'w')
except zipfile.error, e:
appendToLogFailed('archive file creation')
sendMail(log)
sys.exit()
# verify file now exists, then add files to archive
if verifyFile('c:\\', newarchive) == 1:
files = getFileList()
try:
for file in files:
addToArchive(file)
appendToLogPassed('add files to new archive')
except zipfile.error, e:
appendToLogFailed('add files to new archive')
zip.close()
else:
appendToLogFailed('check new archive path')
sendMail(log)
sys.exit()
# copy new archive to server
try:
copying = shutil.copy(path + newarchive, server + newarchive)
appendToLogPassed('archive copy')
except (IOError, os.error), message:
appendToLogFailed('archive copy')
sendMail(log)
sys.exit()
# verify that new archive exists
if verifyFile(server, newarchive) == 1:
appendToLogPassed('new archive path verification')
else:
appendToLogFailed('new archive path verification')
sendMail(log)
sys.exit()
# remove local archive
try:
os.system('del ' + path + newarchive)
appendToLogPassed('local archive removal')
except OSError:
appendToLogFailed('local archive removal')
sendMail(log)
sys.exit()
# email log file
sendMail(log)
sys.exit()
:)