欢迎投稿

今日深度:

cassandra数据备份与迁移,cassandra数据备份

cassandra数据备份与迁移,cassandra数据备份


单机备份

注意备份的时候schema也要备份一下,否则不能恢复快照!

1.备份单个keyspace schema
cqlsh -e "DESC KEYSPACE user" > user_schema.cql
2.备份整个database schema
cqlsh -e "DESC SCHEMA" > db_schema.cql

3.导入keyspace schema
在 user_schema.cql所在的目录下打开cqlsh:
source 'user_schema.cql'
4.导入database schema:
在db_schema.cql所在的目录下打开cqlsh:
source 'db_schema.cql'
5.备份单个keyspace数据
bin/nodetool snapshot -t 20171130 user
6.备份所有的keyspace
bin/nodetool snapshot -t 20171130

-t后面指定快照的名字,备份好的数据在这个目录下
$CASSANDRA_HOME/data/yourkeyspace/table-uuid/snapshots/20171130

7.使用脚本将快照拷贝出来
将如下脚本保存为copy.sh,注意修改keyspacePath和dstKeyspacePath的值。 这个脚本只拷贝某一个keyspace的快照

#!/bin/bash
keyspacePath="cassandra-3.11.1/data/data/your_keysapce_name"
currentIp=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'`
dstKeyspacePath="/home/master/beifen/$currentIp/my_backup"
echo $currentIp
if [ ! -x "$dstKeyspacePath" ]; then  
    mkdir -p $dstKeyspacePath
fi 
for dir in `ls $keyspacePath`; do (
    currentTableName=`basename $dir`
    echo "currentTableName=$currentTableName"
    tableDstPath="$dstKeyspacePath/$currentTableName"
    echo "tableDstPath=$tableDstPath"
    if [ ! -x "$tableDstPath" ]; then  
        mkdir -p $tableDstPath
    fi 
    cp -r "$keyspacePath/$dir/snapshots/" $tableDstPath
); done

给脚本赋予执行权限:
chmod +x ./copy.sh
执行脚本:
./copy.sh
8.恢复快照
将dstKeyspacePath下的SSTable(table-uuid)目录拷贝到新的cassandra的Data目录下,执行如下命令即可
bin/nodetool refresh


集群备份

直接从一个集群导到另一个集群:

1.memTable数据刷新到SSTable
bin/nodetool flush

2.源集群导出schema

cqlsh 192.168.40.x -e "DESC KEYSPACE your_keyspace" > your_keyspace_schema.cql

your_keyspace_schema.cql 拷贝到目录集群某个目录下,然后进入到这个目录,打开cqlsh。
source 'your_keyspace_schema.cql'

3.使用脚本直接导数据
该脚本只能导一个keyspace
将如下脚本保存为 migrate.sh,注意修改keyspacePath和dstClusterIP。keyspacePath是源集群的keyspace所在目录,dstClusterIP是目标集群的IP,可以是多个。

#!/bin/bash
keyspacePath="cassandra-3.11.1/data/data/your_keyspace"
dstClusterIP=192.168.40.1,192.168.40.2,192.168.40.3
for dir in `ls $keyspacePath`; do (
    sstableloader -d $dstClusterIP -t 100 "$keyspacePath/$dir"
); done

脚本赋上执行权限:
chmod +x ./migrate.sh
执行脚本,(请确保你的PATH环境变量里配置过Cassandra的路径)
./migrate.sh

如果要备份所有的数据:

#!/bin/bash
srcDataPath="/data/janus/apache-cassandra-3.11.1/data/data"
# 可以是多个IP,逗号分隔
dstClusterIP=192.168.40.13

for keyspacePath in `ls $srcDataPath`; do (
    echo "currentkeyspace=$keyspacePath"
    for table in `ls $srcDataPath/$keyspacePath`; do (
        echo "$srcDataPath/$keyspacePath/$table"
        sstableloader -d $dstClusterIP -t 100 "$srcDataPath/$keyspacePath/$table"
    );done
);done

dstClusterIP 后面指定的是目标集群的IP,可以是多个IP。根据你的实际情况修改之。

srcDataPath 是源集群的数据目录。根据你的实际情况修改之。

执行脚本的时候,控制台会打印出当前备份的进度。

www.htsjk.Com true http://www.htsjk.com/cassandra/11250.html NewsArticle cassandra数据备份与迁移,cassandra数据备份 单机备份 注意备份的时候schema也要备份一下,否则不能恢复快照! 1.备份单个keyspace schema cqlsh -e "DESC KEYSPACE user" user_schema.cql 2.备份整个databa...
评论暂时关闭