Backup files using Gmail
I have some files that I want to backup. And I really don't have any ultra reliable place where I can backup some important files. Or so I thought, until I thought of Gmail and my last adventure in scripting Gmail.
I have created a little Python script that can backup any file to your Gmail account. It has following features:
I currently use it to backup Orangoo's database. I have a cron job running every 2 hours that backups the database and sends it to my Gmail account. Kind of nifty :) What do you need?How it works?The usage is: python gbackup.py <user@gmail.com> <password> <identifier> <file> Identifier is pretty important and should be unique. The script deletes all the older "files" with the same identifier. The codeCopy this code in gbackup.py and you are ready to go. import os
import sys
import datetime
from libgmail import *
class GBackup:
def __init__(self, email, password):
print 'Logging in to Google using %s' % email
self.email = email
self.ga = GmailAccount(email, password)
self.ga.login()
print 'Logged in'
def deleteOld(self, file_id):
first = True
threads = self.ga.getMessagesByQuery(file_id)
if len(threads) > 0:
for thread in threads:
if len(thread) > 0:
for msg in thread:
if not first:
self.ga.trashMessage(msg)
else:
first = False
def backupFile(self, file_id, file_name):
subject = "[GBackup - %s]" % file_id
print 'Sending message with subject: %s...' % subject
now = datetime.datetime.now()
body = "Backup of %s.\nTime of backup: %s" % (file_name, now)
msg = GmailComposedMessage(self.email, subject, body, filenames=[file])
cur_msg = self.ga.sendMessage(msg)
self.deleteOld(subject)
print 'Message successfully sent!'
if __name__ == "__main__":
if len(sys.argv) < 5:
print 'Usage is: python gbackup.py <user@gmail.com> <pw> <ident> <file>'
else:
py, user, pw, ident, file = sys.argv
gb = GBackup(user, pw)
gb.backupFile(ident, file)
|
|