elasticsearch 备份与恢复

本地备份

一 创建仓库

首先需要创建一个仓库,仓库的类型支持Shared filesystem, Amazon S3, HDFS和Azure Cloud,暂时只掌握了文件系统备份

方式一: 通过命令方式创建备份仓库

​ a.修改elasticsearch安装目录下config/elasticsearch.yml,添加如下配置: path.repo: ["/usr/local/wutongyu/backup"] b.使用JAVA API创建仓库,其中my_backup为仓库名:

curl -XPUT http://localhost:9200/_snapshot/my_backup 
{
    "type": "fs", 
    "settings": {
        "location": "/usr/local/wutongyu/backup/my_backup" 
    }
}

方式二: 修改配置文件,调用API创建仓库

​ a.修改elasticsearch安装目录下config/elasticsearch.yml,添加如下配置: path.repo: ["/usr/local/wutongyu/backup"] b.使用JAVA API创建仓库

//链接es
        Settings esSettings = Settings.builder().put("transport.type","netty3").put("http.type", "netty3").put("cluster.name", "elasticsearch").build();
        TransportClient client = new PreBuiltTransportClient(esSettings);
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.212.20"), Integer.parseInt("9300")));
//创建声明仓库
try {
            Settings settings =esSettings.builder().put("location", "/usr/local/wutongyu/backup/my_backup").build();
            PutRepositoryRequestBuilder putRepo = new PutRepositoryRequestBuilder(client.admin().cluster(), PutRepositoryAction.INSTANCE);
            putRepo.setName( "my_backup").setType("fs").setSettings(settings).execute().actionGet();
        } catch (Exception e) {
            e.printStackTrace();
        }

二 备份索引

创建完仓库后,进行备份操作,一个仓库可以包含多个快照(snapshots),快照可以存所有的索引,部分索引或者一个单独的索引,可以给索引指定一个唯一的名字,如下,将正在运行的所有索引备份到my_backup仓库下一个叫mybackup_201807的快照中:

//不等待返回结果
curl -XPUT http://localhost:9200/_snapshot/my_backup/mybackup_201807
//等待返回结果
curl -XPUT http://localhost:9200/_snapshot/my_backup/mybackup_201807?wait_for_completion=true

备份部分索引,linux执行,去掉换行:

curl -XPUT http://localhost:9200/_snapshot/my_backup/mybackup_201807
{
    "indices": "index_1,index_2"
}

三 查看备份信息

curl -XGET http://localhost:9200/_snapshot/my_backup/mybackup_201807

四 备份恢复

方式一 :命令恢复

恢复快照之前需要先将快照涉及的索引关闭,恢复后,索引会自动打开,命令如下:

curl -XPOST http://localhost:9200/logstash-index-log-20171127/_close/ 

关闭索引后进行恢复:

//恢复mybackup_201807整个快照
curl -XPOST http://localhost:9200/_snapshot/my_backup/mybackup_201807/_restore
​
// 只恢复快照中的logstash-index-log-20171127索引
curl -XPOST http://localhost:9200/_snapshot/my_backup/mybackup_201807/_restore
{
    "indices": "logstash-index-log-20171127"
}

方式二:API方式恢复

//链接es同上
List<String> indices = Lists.newArrayList();
        try {
            GetSnapshotsRequestBuilder builder = new GetSnapshotsRequestBuilder(client.admin().cluster(), GetSnapshotsAction.INSTANCE);
            builder.setRepository("my_backup");
            builder.setSnapshots("mybackup_201807");
            GetSnapshotsResponse getSnapshotsResponse = builder.execute().actionGet();
​
            //检查索引是否存在,如果是,请关闭它,然后再恢复。
            indices = getSnapshotsResponse.getSnapshots().get( 0 ).indices();
            CloseIndexRequestBuilder closeIndexRequestBuilder = new CloseIndexRequestBuilder(client.admin().indices(), CloseIndexAction.INSTANCE);
            closeIndexRequestBuilder.setIndices(indices.toArray(new String[indices.size()]));
            closeIndexRequestBuilder.execute().actionGet();
        } catch( Exception e ) {
            log.error( "索引不存在,无法关闭!"+ ToStringBuilder.reflectionToString( indices.toArray(new String[indices.size()]) ));
        }
        // 执行恢复索引操作
        RestoreSnapshotRequestBuilder restoreBuilder = new RestoreSnapshotRequestBuilder(client.admin().indices(), RestoreSnapshotAction.INSTANCE);
        restoreBuilder.setRepository(repositoryName).setSnapshot(snapshot);
        restoreBuilder.execute().actionGet();

远程备份

一 远程备份【json方式】

远程备份有多种方式,当下记录两种远程备份方式,一种是备份成json文件,另外一种是通过文件挂载,个人推荐使用json方式进行备份,如果使用文件挂载,对es了解不够透彻会出现各种坑。

1 安装必要工具:

(1)因为这种备份方式是用node写的,故依赖nodejs,linux安装nodejs:
安装:
    cd /usr/local/wutongyu/tools
    tar -xf node-v8.11.3-linux-x64.tar.xz
    cd node-v8.11.3-linux-x64
    ln -s /usr/local/wutongyu/tools/node-v8.11.3-linux-x64/bin/node /usr/local/bin
    ln -s /usr/local/wutongyu/tools/node-v8.11.3-linux-x64/bin/node /usr/local/bin
配置环境变量,在/etc/profile文件新增:
    export NODE_HOME=/usr/local/wutongyu/tools/node-v8.11.3-linux-x64
    export PATH=$PATH:$NODE_HOME/bin
    export NODE_PATH=$NODE_HOME/lib/node_modules
执行如下命令让环境变量设置生效:
    source /etc/profile 
(2)安装 elasticdump:
    cd /usr/local/wutongyu/tools    
    npm install elasticdump -g

2 本地备份,备份成json格式:

elasticdump --type=data --input="http://localhost:9200/MyIndex" --output=myIndex.json --limit 1000 --sourceOnly --ignore-errors
其中:limit为单批执行的记录数,最大1W

还原:

elasticdump --type=data --input="http://localhost:9200/MyIndex/MyType" --output=myIndexMyType.json --limit 1000 --sourceOnly --ignore-errors

3 集群内数据备份

# 备份 mapping
elasticdump --input="http://1.1.1.1:9200/MyIndex" --output="http://1.1.1.2:9200/MyIndex" --type=mapping
# 备份数据
elasticdump --input="http://1.1.1.1:9200/MyIndex" --output="http://1.1.1.2:9200/MyIndex" --type=data --limit 1000 --sourceOnly --ignore-errors
​

4 ftp远程备份与恢复

根据2中生成的json文件,进行压缩,通过JAVA API上传到ftp服务器,恢复操作的时候,从ftp服务器下载备份文件,解压后,通过还原命令进行数据还原

二 远程备份【文件挂载方式】

​ 可以简单地理解为创建一个远程仓库,最简单的使用nfs进行远程挂载,将仓库的目录挂在到远程的服务器上【转载:https://www.cnblogs.com/freeweb/p/6593861.html】,nfs是网络文件系统,允许一个节点通过网络访问远程计算机的文件系统,远程文件系统可以被直接挂载到本地,文件操作和本地没有区别,如果是局域网的nfs那么io的性能也可以保证,下面就以CentOS 7.x为例,配置NFS

  首先是服务端配置,服务端提供文件系统供客户端来挂载使用,配置过程如下:

  首先检查是否缺少基础环境:

rpm -qa | grep nfs-utils
rpm -qa | grep rpcbind

  如果这两个包存在那么可以直接使用,一般服务器安装的时候都会存在,如果没有的话执行下面命令安装:

yum -y install nfs-utils
yum -y install rpcbind

  安装完成之后配置nfs访问目录,配置文件位置/etc/exports,默认是空的这里添加一行:

/opt/nfs_test 192.168.1.8(rw,no_root_squash,no_all_squash,async)

  这个配置表示开放本地存储目录/nfs_test 只允许192.168.1.8这个主机有访问权限,rw表示允许读写;no_root_squash表示root用户具有完全的管理权限;no_all_squash表示保留共享文件的UID和GID,此项是默认不写也可以;async表示数据可以先暂时在内存中,不是直接写入磁盘,可以提高性能,另外也可以配置sync表示数据直接同步到磁盘;就配置这些就可以,保存退出

  如果想让另外一台主机也可以挂载这个目录,那么直接在后面追加即可,比如:

/opt/nfs_test 192.168.1.8(rw,no_root_squash,no_all_squash,async) 192.168.1.9(rw,no_root_squash,no_all_squash,async) 

  多个目录可以每行配置一个,如果想让这个网段的主机都可以访问,假如此时子网掩码是255.255.255.0,网关是192.168.1.0,那么ip那里可以写成192.168.1.0/24表示允许地址段的所有主机访问

  现在配置完这些配置,启动相关服务:

systemctl start rpcbind.service
systemctl start nfs.service

  启动之后可以通过status来查看状态,如果下次修改了配置,可以重启服务来使配置生效,也可以直接执行如下命令刷新配置:

exportfs -a

  刷新配置即可生效

  现在服务端配置完毕,可以在对应的主机上来配置客户端了,需要的环境和服务端一样,要保证安装nfs-utils和rpcbind

  保证环境没问题和上面一样启动rpcbind服务和nfs服务

  首先创建挂载点: mkdir /mnt/test1

  然后挂载nfs: mount -t nfs 192.168.1.3:/opt/nfs_test /usr/local/suninfo/backup/my_backup

  挂载成功之后通过 df -h 可以查看挂载的情况,nfs可用空间就是服务端/nfs_test目录所能使用的最大空间

  现在就可以往nfs写入数据了,服务端往/nfs_test读写数据和客户端往/mnt/test1读写数据是一样的,这样就实现了文件同步和共享

  卸载nfs和普通文件系统一样,使用: umount /usr/local/suninfo/backup/my_backup

  如果需要设置开机挂载,在/etc/fstab添加一行配置即可: 192.168.1.3:/opt/nfs_test /usr/local/suninfo/backup/my_backup nfs defaults 1 1 然后服务端和客户端都要用enable设置nfs和rpcbind服务开机启动,然后才可以正常挂载

猜你喜欢

转载自blog.csdn.net/wutongyuWxc/article/details/81011284