Daily backups (files + mysql) to yandex.disk in Ubuntu 12.04

2012-08-30
#backup #ubuntu #yandex.disk #server

We all do backups. This note is about doing them on Ubuntu 12.04.

Tasks:

  • archive important folders
  • dump mysql databases
  • upload all to yandex.disk (online web storage with 10GB space)
  • delete old backups

I’ll save only backups for last five days.

§ Archiving

There are many different backup tools, see wiki list. I’ve chosen DAR.

One can install it in Ubuntu 12.04 by typing

# apt-get install dar

It is a console utility and it can be confusing for beginner. Here is great short how-to with explanations.

I will perform full backups every day. For incremental ones read manual.

I need to backup /srv folder, so my command is:

dar -m 256 -z -s 600M -D -R /srv -c `date -I`_data -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png"

It creates file currentdate_data.X.dar, for example 2012-08-30_data.1.dar, with a backup. If archive is bigger than 600M (-s 600M), it splits in two slices. X in the filename represents slice number. For the full description read how-to.

§ Dumping MySQL

Dumping MySQL is performed by mysqldump utility. You can list your databases in MySQL by entering into console with

mysql -uroot -ppassword

and type there

SHOW DATABASES;

I have drupal database I want to backup. So, I type:

mysqldump -u root -ppassword drupal > `date -I`_drupal.mysqldump

It creates file with all necessary stuff to create the same table from nothing.

§ Uploading

Yandex.Disk can be accessed by WebDAV. Install vebdavfs2:

# apt-get install davfs2

Then edit /etc/davfs2/secrets. Just add line with address, login and password:

https://webdav.yandex.ru username@yandex.ru "userpass"

Now you cat mount yandex.disk as an ordinary drive to folder /mount/yandex:

mount -t davfs https://webdav.yandex.ru /media/yandex

Now it is possible to store backups in cloud.

Old files can be removed by:

rm `ls *.dar | head -n -5`

It leaves only last five backups.

§ Adding to cron

All commands from above in one script backup.sh:

#!/bin/bash
mount -t davfs https://webdav.yandex.ru /media/yandex
cd /media/yandex/cudacer
dar -m 256 -z -s 600M -D -R /srv -c `date -I`_data -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png"
rm `ls *.dar | head -n -5`
mysqldump -u root -p<password> <dbname>l > `date -I`_drupal.mysqldump
rm `ls *.mysqldump | head -n -5`

Run it every day at 1am. Open cron tasks

# crontab -e

and paste there line:

 0 1 * * * /home/kenarius/backup.sh

Now you have backups.