Skip to content

Commit

Permalink
Create backup script on control nodes, add metadat for backupninja
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Vlasov committed May 12, 2017
1 parent 53c019e commit 8f19bf5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
7 changes: 7 additions & 0 deletions opencontrail/cassandra_backup.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/usr/bin/cassandra_backup:
file.managed:
- user: root
- group: root
- mode: 755
- source: salt://opencontrail/files/cassandra_backup
- template: jinja
55 changes: 55 additions & 0 deletions opencontrail/files/cassandra_backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

set -uef -o pipefail

# Define vars
BACKUP_DIR=/var/backups/cassandra
DATA_DIR=/var/lib/cassandra/data
NODETOOL=$(which nodetool)

TODAY_DATE=$(date +%F)
NODENAME=$(salt-call pillar.get linux:network:fqdn --out=newline_values_only)
CONTROL_NODES_DB_TAG="OPENCONTRAIL_CONTROL_DB"
BACKUP_SNAPSHOT_DIR="${BACKUP_DIR}/${CONTROL_NODES_DB_TAG}/${NODENAME}/${TODAY_DATE}/SNAPSHOTS"
BACKUP_SCHEMA_DIR="${BACKUP_DIR}/${CONTROL_NODES_DB_TAG}/${NODENAME}/${TODAY_DATE}/SCHEMA"

SNAPSHOT_NAME=snp-$(date +%F-%H%M-%S)
DATE_SCHEMA=$(date +%F-%H%M-%S)

DB_BIND_HOST=$(salt-call pillar.get opencontrail:database:bind:host --out=newline_values_only)
DB_BIND_PORT=$(salt-call pillar.get opencontrail:database:bind:port --out=newline_values_only)
CQL_SH="$(which cqlsh) ${DB_BIND_HOST} ${DB_BIND_PORT}"
## List All Keyspaces
declare -a KEYSPACE_LIST=( $( ${CQL_SH} -e "DESC KEYSPACES" | awk '{RS="\\s+"; if(NF>0){print}}' | sort ) )

# Make sure backup Directory exists
mkdir -p "${BACKUP_SCHEMA_DIR}"
mkdir -p "${BACKUP_SNAPSHOT_DIR}"

# Dump cassandra version
${NODETOOL} version > "${BACKUP_DIR}/${CONTROL_NODES_DB_TAG}/${NODENAME}/${TODAY_DATE}/.cassandra_version"
# Remove previous snapshot in order to prevent disk usage increase
${NODETOOL} clearsnapshot

# SCHEMA BACKUP
# Create directory inside backup SCHEMA directory. As per keyspace name.
for VAR_KEYSPACE in ${KEYSPACE_LIST[*]}
do
mkdir -p "${BACKUP_SCHEMA_DIR}/${VAR_KEYSPACE}"
# SCHEMA Backup - All Keyspace and All tables
${CQL_SH} -e "DESC KEYSPACE ${VAR_KEYSPACE}" > "${BACKUP_SCHEMA_DIR}/${VAR_KEYSPACE}/${VAR_KEYSPACE}_schema-${DATE_SCHEMA}.cql"
done

# Create snapshots for all keyspaces
${NODETOOL} snapshot -t "${SNAPSHOT_NAME}"

###### Get Snapshot directory path
declare -a SNAPSHOT_DIR_LIST=( $(find ${DATA_DIR} -type d -name snapshots) )

## Create directory inside backup directory. As per keyspace name.
for SNP_PATH in ${SNAPSHOT_DIR_LIST[*]}
do
i=$(echo "${SNP_PATH}" | awk '{gsub("'${DATA_DIR}'/", "");print}')
mkdir -p "${BACKUP_SNAPSHOT_DIR}/${i}"
cp -prf "${SNP_PATH}/${SNAPSHOT_NAME}" "${BACKUP_SNAPSHOT_DIR}/${i}/"
done
3 changes: 2 additions & 1 deletion opencontrail/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include:
{% endif %}
{% if pillar.opencontrail.control is defined %}
- opencontrail.control
- opencontrail.cassandra_backup
{% endif %}
{% if pillar.opencontrail.database is defined %}
- opencontrail.database
Expand All @@ -26,4 +27,4 @@ include:
{%- endif %}
{% if pillar.opencontrail.common is defined %}
- opencontrail.common
{% endif %}
{% endif %}
8 changes: 8 additions & 0 deletions opencontrail/meta/backupninja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
backup:
cassandra:
fs_excludes:
- /var/backups/cassandra/OPENCONTRAIL_CONTROL_DB/*
fs_includes:
- /var/backups/cassandra/OPENCONTRAIL_CONTROL_DB/{{ system.name }}.{{ system.domain }}/
actions:
- cmd: /usr/bin/cassandra_backup

0 comments on commit 8f19bf5

Please sign in to comment.