tech and other rants

Small backup script

March 9th, 2009 in programming

Saw a clip recently about some company not having backups and lost a lot of data and realized i didn’t have backups either so I decided to make a short bash script that covers everything i need.
small backup script

For ease of use put in some config variables. Edit these lines with the required info:

email_address=”your.address@gmail.com”
db_name=”wp_blog”
backup_dir=”/kit/backup”
domain_name=”tech.mariusb.com”
www_dir=”/var/www/$domain_name”

Create a file called .my.cnf in your home directory with the user & password for the database connection like so:

[client]
user=wp_user
password=your_pass

If you have a small site you could setup so that this script send you an e-mail with the archived backup. You will have to modify the send_backup_on_mail to yes and install on your linux system mutt.
Depending on the linux flavor you’ll have to do yum install mutt or aptitude install mutt or whatever else.

Now that you have the script ready you will have to put it to run every day using cron and make in executable:

chmod +x script_name.sh # make in executable

Create a symlink in the daily cron folder:
ln -sf /dir/of/script/script_name.sh /etc/cron.daily/script_name.sh

And that’s it, you will permanently have 3 site backups, and the old ones will be automaticaly deleted.
If you have questions please post comments.

Tags: , , ,

2 Responses to “Small backup script”

  • Antoniu
    October 14th, 2009 at 9:23 am

    1. You don’t need to parse /root/.my.cnf file, is used by default by all mysql related binaries.

    2. Under Debian, you can use debian-sys-maint username instead of root, check /etc/mysql/debian.cnf .

    3. mysqldump does not guarantee database consistency when you have tables using InnoDB storage. In this case, the only way to build a consistent backup is to explicitly use –single-transaction with mysqldump. NDB storage is another (love) story.

    4. You may want also to dump the stored routines, check –routines.

  • Zen
    October 25th, 2009 at 6:43 am

    1. seems that on my CentOS system mysqldump does not parse .my.cnf automaticly :)
    2. I’ll check that out, i created this script on a redhat based machine
    3 & 4 thanks, I’ll investigate / modify it accordingly as soon as I get the chance.

Leave a Reply