Poor mans offsite backup

Before I got some proper offsite backup space* I used to make tarballs of files and dump them in my gmail. Not the most elegant solution but it worked and is free (I know about gmailfs, but I could never get it to work reliably).

This is the script I used to send the files:

#!/usr/bin/env python
import getpass
import optparse
import smtplib
import sys

from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart

opts = optparse.OptionParser()
opts.add_option(‘-u’, ‘–username’)
opts.add_option(‘-p’, ‘–password’)
(options, args) = opts.parse_args()
if len(args) < 1:
    print "Usage: send_file_to_gmail.py [-u user] [-p pass] filename"
    sys.exit(0)

if options.username == None:
    options.username = raw_input("Username: ")
if options.password == None:
    options.password = getpass.getpass()
if options.username[len(options.username)-10:] != "@gmail.com":
    options.username += "@gmail.com"

msg = MIMEMultipart()
msg[‘Subject’] = "Backup of %s" % args[0]
msg[‘From’] = options.username
msg[‘To’] = options.username

msg.preamble = "Backup of %s" % args[0]
msg.epilogue = " "

fp = open(args[0], "rb")
atfile = MIMEBase("application", "octet-stream")
atfile.set_payload(fp.read())
fp.close()
Encoders.encode_base64(atfile)
atfile.add_header("Content-Disposition", "attachment", filename=args[0])
msg.attach(atfile)

s = smtplib.SMTP()
#s.connect("localhost", "8025")
s.connect("smtp.gmail.com")
s.ehlo()
s.starttls()
s.ehlo()
s.login(options.username, options.password)
print "Sending %s" % args[0]
s.sendmail(options.username, options.username, msg.as_string())
s.close()
 

The above code is made available under the MIT license.

* While I am writing this the Dreamhost backup space I use has been down for about two weeks (it came back briefly a couple of days ago but is back down again now). I can’t complain too much as it is a free extra they throw in with the hosting package but I may have to restart my gmail sending if this keeps up.