kiorky / db_smart_backup

Another database(db) smart backup tool (mysql/postgresql/slapd/mongo/es/redis)
http://www.makina-corpus.com
Other
28 stars 7 forks source link

===================================================== Backup Script for various databases:

Simple dump based backup with intelligent rotation and hooks. Supports with battery included for mysql, mongodb, slapd & postgresql

.. contents::

Badges

.. image:: https://travis-ci.org/kiorky/db_smart_backup.png :target: http://travis-ci.org/kiorky/db_smart_backup

Supported databases

- Mongodb
- PostGRESQL
- Redis
- Elasticsearch
- MySQL
- slapd (OpenLDAP)

Why another tool ?

So main features/requirements are:

- Posix shell compliant (goal, but not that tested, the really tested one
  is bash in posix mode)
- **PostgreSQL / MySQL support** for simple database and privileges
  dumps
- Enougthly unit **tested**
- XZ **compression** if available
- Easily **extensible** to add another backup type / Generic backups methods
- **Optional hooks** at each stage of the process addable via configuration
  (bash functions to uncomment)
- **Keep a fixed number of dumps**, recent ones, old ones, and in a smart way.
  More on that later on this document. But for example the default is to keep
  the last 24 dumps, then 14 days (1 per day), 8 weeks (1 per week) and 12
  months (1 per month).

Installation

::

curl -OJLs https://raw.githubusercontent.com/kiorky/db_smart_backup/master/db_smart_backup.sh
curl -OJLs https://raw.githubusercontent.com/kiorky/db_smart_backup/master/run_dbsmartbackups.sh
chmod +x db_smart_backup.sh run_dbsmartbackups.sh

Generate a config file::

./db_smart_backup.sh --gen-config /path/to/config
vim /path/to/config

Backup::

./db_smart_backup.sh /path/to/config

Backup all found databases in cron

We also bundle a script named run_dbsmartbackups.sh which search in /etc/dbsmartbackup for any database configuration:

-  pg: /etc/dbsmartbackup/postgresql.conf
-  mysql: /etc/dbsmartbackup/mysql.conf
-  mongodb: /etc/dbsmartbackup/mongod.conf
-  slapd /etc/dbsmartbackup/slapd.conf
-  redis /etc/dbsmartbackup/redis.conf
-  elasticsearch /etc/dbsmartbackup/elasticsearch.conf

be sure to have the scripts in your path::

curl -OJLs https://raw.githubusercontent.com/kiorky/db_smart_backup/master/db_smart_backup.sh
curl -OJLs https://raw.githubusercontent.com/kiorky/db_smart_backup/master/run_dbsmartbackups.sh
chmod +x db_smart_backup.sh run_dbsmartbackups.sh
mkdir /etc/dbsmartbackup

In /etc/dbsmartbackup, generate a config file (either: mysql.conf, mongod.conf, slapd.conf, postgresql.conf)::

./db_smart_backup.sh --gen-config /etc/dbsmartbackup/<db_type>.conf
vim /path/to/configa

Testing the backup::

./db_smart_backup.sh /etc/dbsmartbackup/<db_type>.conf

Only execute the pruning policy::

./db_smart_backup.sh -p /etc/dbsmartbackup/<db_type>.conf

Test the cron that search for all possible things to backups::

run_dbsmartbackups.sh

Add it to cron::

0 0 * * * root /usr/bin/run_dbsmartbackups.sh --no-colors --quiet

For postgresql, you can configure the path to your postgresql.conf(s) PATH(s) by exporting "PG_CONFS" that is a space separated absolute paths to postgresql.conf's. Note, that for redhat or debian based, PG_CONFS should be OK by default.

Changelog

Credits

The great things

Backup types

PostgreSQL & MySQL specificities ++++++++++++++++++++++++++++++++++++++++

Add another backup type ++++++++++++++++++++++++ You need to first read the implementations for mysql and postgresql, those are really simple, then follow the next guide (you do not need to make the script call your functions, they are introspected):

- Add a function **yourtype_set_connection_vars** to set any necessary extra global variable needed
  at the connect phase to your service
- Add a function **yourtype_check_connectivity** that exit in error if the
  connexion is not possible and die in error else (use the **die_in_error**
  function)
- Add a function **yourtype_set_vars** to set any necessary extra global variable needed
  to handle your service
- Add a function **yourtype_get_all_databases** that return a space separated
  list of your database dbs.
- Add a function **yourtype_dump** that will dump a database to a file, or a
  stub returning 0 as $? (call **/bin/true**) if it is not relevant for your
  backup type.
- Add a function **yourtype_dumpall** even if one of them
  is just an empty stub, the script will then introspect itself to find
  them. Those functions must set the **LAST_BACKUP_STATUS** either to **""**
  on sucess or **"failure"** if the backup failed.
- Add what is needed to load the configuration in the default configuration
  file in the **generate_configuration_file** method
- Hack the defaults and variables in **set_vars**, the same way, if
  necessary.

Hooks

Options

Backup Rotation..

We use hardlinks to achieve that but be aware that it may have filesystem limits:

Default policy ++++++++++++++

Please Note!!

I take no responsability for any data loss or corruption when using this script.. This script will not help in the event of a hard drive crash. If a copy of the backup has not be stored offline or on another PC.. You should copy your backups offline regularly for best protection. Happy backing up...