Archive for March 28th, 2008

Autobackup de la base de datos

Friday, March 28th, 2008

Since this days the server has been out for holidays I have been tempted of moving everything to another server, just until the service is back. However I have faced the problem that I didn’t have access to an updated database, so I couldn’t do anything. I wished to have at least a version of the database from the same day or the day before of the server crash.

The question is that I have founded a quite easy solution: to send myself a mail with a copy of the database dump once or twice a day (this is not possible on every server)

In order to do that you only have to create a script like this.

#!/bin/sh

mysqldump -u user -ppassword -h host.domain database | gzip > ~/database.sql.gz

echo “This is an autogenerated mail with the backup” | mutt -s “[databasebak]” -a ~/database.sql.gz miemailparabackup@servidor.com

It’s necessary to have mutt installed because this command sends an email in MIME format so we can send a binary file attached

We can call this script “~/scripts/dbbak.sh” so now we only need a “crontab.txt” looking like:

SHELL=/bin/bash

#h m dom mon dow command

35 0,12 * * * ~/scripts/dbback.sh

And then we install the crontab file with

crontab crontab.txt

I hope it will be helpful for you.