Skip to content

Commit

Permalink
Merge "changes to backup_cassandra to backup only snapshots" into R3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Mar 7, 2016
2 parents 7a9e703 + caf32ba commit 6b3c31f
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions fabfile/tasks/backup_restore.py
Expand Up @@ -8,7 +8,7 @@
from fabfile.tasks.zookeeper import *
import string
import time

import re
# Define global path for taking backup and restore
backup_path = '~/contrail_bkup_data/'

Expand Down Expand Up @@ -165,6 +165,7 @@ def backup_nova_instance_data():
def backup_cassandra(db_datas, store_db='local', cassandra_backup='full'):
"""Backup cassandra data in all databases """
global backup_path, final_dir
snapshot_list=[]
host = env.host_string
msg = "Processing the Cassandra DB backup and default path for backup DB data is ~/contrail_bkup_data/hostname/data/ in ({HOST}) \n"
with settings(host_string=host):
Expand Down Expand Up @@ -203,27 +204,32 @@ def backup_cassandra(db_datas, store_db='local', cassandra_backup='full'):
else:
nodetool_cmd = 'nodetool -h localhost -p 7199 snapshot'
sudo(nodetool_cmd)
snapshot_list = sudo("find %s/ -name 'snapshots' " % db_path)
snapshot_list = snapshot_list.split('\r\n')
snapshot_list = snapshot_list[0]
with cd(snapshot_list):
snapshot_dirs = sudo("find %s/ -name 'snapshots' " % db_path)
snapshot_dirs = snapshot_dirs.split('\r\n')
#get relative path to cassandra from db_path
path_to_cassandra = re.search('.*/cassandra/',db_path).group(0)
for snapshot_dir in snapshot_dirs:
snapshot_list.append(snapshot_dir.replace(path_to_cassandra,''))
#get current snap_shot name from any snapshots folder created by nodetool
snapshot_list_name = snapshot_dirs[0]
with cd(snapshot_list_name):
snapshot_name = sudo('ls -t | head -n1')
print "Cassandra DB Snapshot Name: %s" % (snapshot_name)
if store_db == 'local':
sudo('cp -R %s %s ' % (db_path, dir_name))
with cd(path_to_cassandra):
for snapshot in snapshot_list:
sudo('cp --parents -R %s/%s %s ' % (snapshot,snapshot_name,dir_name))
execute(backup_info_file, dir_name, backup_type='Cassandra')
if store_db == 'local':
sudo('cp backup_info.txt %s' % dir_name)
if store_db == 'remote':
execute(ssh_key_gen)
source_path = '%s backup_info.txt' % (db_path)
remote_path = '%s' % (dir_name)
remote_cmd = 'scp -o "StrictHostKeyChecking no" -r %s %s:%s' % (
source_path,
remote_host,
remote_path)
sudo(remote_cmd)

with cd(path_to_cassandra):
for snapshot in snapshot_list:
remote_path = '%s' % (dir_name)
remote_cmd = 'rsync -avzR -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" %s/%s %s:%s' %(snapshot,snapshot_name,remote_host,remote_path)
sudo(remote_cmd)

# end backup_cassandra


Expand Down Expand Up @@ -515,6 +521,7 @@ def restore_cassandra_db():
host)
backup_node = getattr(testbed, 'backup_node', None)
backup_data_path = getattr(testbed, 'backup_db_path', None)
cassandra_backup = getattr(testbed, 'cassandra_backup', None)
store_db = 'local'
if backup_node:
store_db = 'remote'
Expand All @@ -523,7 +530,7 @@ def restore_cassandra_db():
execute(stop_cfgm)
execute(stop_database)
execute(stop_collector)
execute(restore_cassandra, backup_data_path, store_db)
execute(restore_cassandra, backup_data_path, store_db,cassandra_backup)
execute(start_cfgm)
execute(start_database)
execute(start_collector)
Expand Down Expand Up @@ -644,7 +651,7 @@ def restart_analytics():

@task
@roles('database')
def restore_cassandra(backup_data_path='', store_db='local'):
def restore_cassandra(backup_data_path='', store_db='local',cassandra_backup='full'):
"""Restore cassandra data to all databases .and usuage is restore_cassadra_db """
global backup_path
host = env.host_string
Expand Down Expand Up @@ -685,7 +692,14 @@ def restore_cassandra(backup_data_path='', store_db='local'):
remote_host,
remote_path)
sudo(remote_cmd)
with settings(host_string=host):
with settings(host_string=host):
skip_key=''
if cassandra_backup == 'custom':
skip_key = getattr(testbed, 'skip_keyspace', None)
if not skip_key:
print "Need to Define the keyspace names in testbed.py if your are selected as custom snapshot. So that it will omit those keyspace during restoration"
raise SystemExit()
skip_key = ','.join(skip_key)
if store_db == 'local':
print (msg.format(HOST=host_name))
remote_path = '%s' % (snapshot_dir_path)
Expand Down Expand Up @@ -718,10 +732,10 @@ def restore_cassandra(backup_data_path='', store_db='local'):
if snapshot_list:
with cd(snapshot_list):
snapshot_name = sudo('ls -t | head -n1')
cmd = '/opt/contrail/utils/cass-db-restore.sh -b %s -s %s -n %s' % (
cmd = '/opt/contrail/utils/cass-db-restore.sh -b %s -s %s -n %s -k %s' % (
db_path,
snapshot_dir,
snapshot_name)
snapshot_name,skip_key)
sudo(cmd)
if store_db == 'remote':
sudo('rm -rf ~/contrail-data/')
Expand Down

0 comments on commit 6b3c31f

Please sign in to comment.