PDA

View Full Version : i'm pretty proud of this one...


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()


:)

kmj
05-03-2002, 06:28 PM
qool; glad to see you're getting to use python at work!

Vanquish+
05-04-2002, 06:04 AM
your write python is, really good ... lol nice sig inkedmn

inkedmn
11-25-2002, 11:20 AM
RESURRECTION:

have you ever looked back on some code you wrote a long time ago and wondered how you EVER thought it was well written?

GnuVince
11-25-2002, 11:28 AM
inkedmn: funny that you are resurecting this _very_ thread, because I just used it (well, the code) to test 60 emails at work. Gonna check if everything went all right later.

And I suggest you take that code, and rewrite it again. Then, in another 4-6 months, look at it again, and see if you can improve it again.

"Learn to program in 10 years"

inkedmn
11-25-2002, 11:40 AM
well, i would, but it was for a client at my old job, so i'll have to just implement it differently.

i never did get the zip compression to work when i wrote that, i think i'll play with that a bit more...