Safely Backing Up a Zope Data.fs While the Server is Running

I actually don't work with Zope as much as I used to but I was going through some old notes and found this gem to backup a Zope instance's Data.fs file while the server is still running. Between the Data.fs file and the Products and Extensions folders you fundamentally have a full backup of your Zope instance.

/usr/lib/zope2.9/bin/repozo.py -BvzQ -r ./ -f /var/lib/zope2.9/instance/instance_name/var/Data.fs

Depending on where Zope is installed the repozo.py script may be in a different place. The -BvzQ is a basic options set (see the man page for details). The -r is where you are backing up to; in this example it is the current directory. And finally the -f is where the Zope instance's Data.fs is.

Communicating over ssh in Python

I am working on a project in Python and I want to be able to save files to other servers across a network. I did a little research and it seems like paramiko is the way to go.

This link has a good code sample of paramiko in action.

http://stackoverflow.com/questions/68335/how-do-i-copy-a-file-to-a-remote-server-in-python-using-scp-or-ssh

Sending Email with Attachments in Python

Here is a function to make sending email a little easier in Python.

import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

def send_mail(to, subject, text, from="", files=[], cc=[], bcc=[], server="localhost"):
    assert type(to)==list
    assert type(files)==list
    assert type(cc)==list
    assert type(bcc)==list

    message = MIMEMultipart()
    message['From'] = from
    message['To'] = COMMASPACE.join(to)
    message['Date'] = formatdate(localtime=True)
    message['Subject'] = subject
    message['Cc'] = COMMASPACE.join(cc)

    message.attach(MIMEText(text))

    for f in files:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(f, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        message.attach(part)

    addresses = []
    for x in to:
        addresses.append(x)
    for x in cc:
        addresses.append(x)
    for x in bcc:
        addresses.append(x)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(from, addresses, message.as_string())
    smtp.close()

I got most of this code from the link below. I just cleaned it up a bit, re-prioritized the arguments and added support for both carbon copy (Cc) and blind carbon copy (Bcc).

http://snippets.dzone.com/posts/show/2038

{
}