备份consul key value 保存到S3
最终实现aws SSM 自动化 备份consul ,然后传输到S3
1、所有的consul 服务节点安装 SSM agent
2、各区创建备份用到的S3存储桶
3、在各区的SSM控制台创建自动化,远程执行备份脚本
#!/bin/bash ts=$(date +%Y%m%d) source_folder="/data/backup" find /data/backup/ -maxdepth 1 -type f -mtime +1 -iname "consul_kv_bk*" -exec rm -rfv {} \;
if [ ! -d "$source_folder" ];then mkdir -p $source_folder RETVAL=$? if [ $RETVAL -eq 0 ];then echo "create folder: "$source_folder else echo "create folder fial,exit." exit 1 fi fi consul snapshot save ${source_folder}/consul_kv_bk_$ts.snap SNA_REVAL=$? if [ $SNA_REVAL -ne 0 ];then echo "consul key val backup fail........" exit 1 fi aws s3 ls S3-bucket-consulbak/${HOSTNAME%%-*}/${HOSTNAME}/consul_kv_bk_$ts.snap --region cn-northwest-1 REVAL=$? if [ $REVAL -ne 0 ];then aws s3 cp ${source_folder}/consul_kv_bk_$ts.snap s3://S3-bucket-consulbak/${HOSTNAME%%-*}/${HOSTNAME}/ --region cn-northwest-1 SYNC_REVAL=$? if [ $SYNC_REVAL -ne 0 ];then echo "sync snap file to s3 fail........" exit 1 else echo " sync snap file to S3 success!!!" exit 0 fi else echo "file already exists!!!" exit 0 fi
备注:由于consul 是多节点集群,本想着每个集群只需一个备份即可,但考虑到备份文件不大,并且多备份文件更加保险。干脆所有节点都做一个备份。