13.4.07

Auto backup to free email account

Big email account providers, like GMail, Yahoo, MSN started providing huge amount of disk space for emails. There is no reason why not to automate your Linux/Unix backup and send backups to your email account. Like this, you do not need to worry about media for storing your emails, you can be sure that you won't lost them.

Due the limitation of email size (1-10 megs, depending on various servers settings), you cannot expect to do backup of huge amount of data. But it can be useful for documents, small pieces of code and similar things.

You might wander how safe it is ? Well, you trust them with your emails, don't you ? Also, you can encrypt your backups using GnuPG, if you really want. You need to take care of your password and encryption key only.

So, let's start. First of all, decide if you are going to use your existing account, or open a new one. Since they are free, there is no reason not to open new one. Let's call it you@backupserver.com

Then, using your favourite editor, create shell script (vi Backup.sh, for example)

Let's explain it step by step

#!/bin/bash

DATETODAY=`date +%m%d%y`

This command will collect todays date and put it in variable DATE_TODAY. We will add this to every filename as timestamp.

BACKUPDIRS="/your/backup/dir/one /your/backup/dir/two"

This line will add all dirs you want to backup to variable BACKUPDIRS.

Then, we need to create archive:

tar jcvf /tmp/backup_$DATETODAY.tbz $BACKUPDIRS

This line will create backup archive, and place it in /tmp.

Send this archive to your mail account. We will use mutt, as one of most standard mail programs, available on almost any Unix platform.

/usr/bin/mutt -a /tmp/backup_$DATETODAY.tbz you@backupserver.com -s "Backup for $DATETODAY" < /dev/null

It will send you file (-a) to you@backupserver.com with subject (-s) "Backup for $DATETODAY". Of course, instead of $DATETODAY we will have actual date.

And finally, delete tmp file:

rm /tmp/backup_$DATETODAY.tbz

OK, we created script. Do not forget to change it to executable (chmod +x Backup.sh).

Now, test it (./Backup.sh). If everything is fine, in few minutes you will have your backup waiting for you in your mailbox. Download it to your own computer, unpack and test the archive.

If backup file is OK, add backup to crontab and automate backups, so it will be daily. Put line (use crontab -e):

10 0 * * * /path/to/your/Backup.sh

And voila, you will have your backup made automatically 10 minutes after midnight and delivered to your mailbox.

Нема коментара: