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 10 GB of space)
  • delete old backups

I’ll save only backups for the last five days.

§ Archiving

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

One can install it in Ubuntu 12.04 by typing

1# apt-get install dar

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

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

I need to back up the /srv folder, so my command is:

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

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

§ Dumping MySQL

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

1mysql -uroot -ppassword

and type there

1SHOW DATABASES;

I have a Drupal database I want to back up. So, I type:

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

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

§ Uploading

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

1# apt-get install davfs2

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

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

Now you can mount yandex.disk as an ordinary drive to the folder /media/yandex:

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

Now it is possible to store backups in the cloud.

Old files can be removed by:

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

It leaves only the last five backups.

§ Adding to cron

All commands from above in one script backup.sh:

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

Run it every day at 1 am. Open the cron tasks

1# crontab -e

and paste this line there:

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

Now you have backups.