docker 配置文件ansible备份语句
运行备份脚本:
ansible -i /data/www/shengjipintai/docker_nginx_hosts nginx -m script -a 'chdir=/data /data/www/wwwroot/10.10.38.20/shell/backup_peizhi_docker_data_nginx.sh'
Nginx类备份代码:配置Nginx目录在/data/nginx
#!/bin/bash
#source_path 定义配置文件所在目录
#backup_path定义备份位置
#计划每周一备份
#默认删除180天以前备份
#获取当前主机IP
ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" | grep "10."`
#定义备份目录
backup_path=/data/back_up
source_path=/data/nginx
#创建备份时间目录
mkdir -p ${backup_path}/${ip}/$(date +%Y-%m-%d)/nginx
cd ${backup_path}
find ./ -ctime +180 -exec rm -rf {} \;
ls ${source_path} |grep -v logs|xargs -i \cp -a ${source_path}/{} ${backup_path}/${ip}/$(date +%Y-%m-%d)/nginx/
echo -e "备份 ${source_path} 文件到 ${backup_path}/${ip}/$(date +%Y-%m-%d)/ 已完成!" | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
echo -e "ls -l ${backup_path}/${ip}/$(date +%Y-%m-%d)/" | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
ls -la ${backup_path}/${ip}/$(date +%Y-%m-%d)/nginx | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
#收集docker ps -a记录
echo -e "\n===========================当前时间 $(date '+%Y-%m-%d %H:%M:%S') 执行docker ps -a的记录========================="| tee ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_list_info.log
docker ps -a | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_list_info.log
cat ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_list_info.log >> ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
#收集docker volumes的记录
echo -e "\n===============当前时间 $(date '+%Y-%m-%d %H:%M:%S') 各个docker容器映射目录记录===================="| tee ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log
list=`docker ps -a | awk '{print $NF}' | grep -v "NAMES"`
for names in $list
do
echo -e "============容器$names 的映射目录记录==============" | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log
docker inspect -f "{{ .HostConfig.Binds }}" $names | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log
done
cat ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log >> ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
echo "============已全部完成=============="
微服务备份脚本:
#!/bin/bash
#source_path 定义配置文件所在目录
#backup_path定义备份位置
#计划每周一备份
#默认删除180天以前备份
#获取当前主机IP
ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" | grep "10."`
#定义备份目录
backup_path=/data/back_up
#创建备份时间目录
mkdir -p ${backup_path}/${ip}/$(date +%Y-%m-%d)
#定义配置文件所在目录
source_path=/data/ylxf/
#切换到备份目录,删除超过180天的配置目录
cd ${backup_path}/${ip}
find ./ -ctime +180 -exec rm -rf {} \;
#备份配置目录
#\cp -a ${source_path} ${backup_path}/${ip}/$(date +%Y-%m-%d)/
#备份配置目录时排除log|logs日志目录
ls ${source_path} |grep -vE "logs|log" |xargs -i \cp -a ${source_path}/{} ${backup_path}/${ip}/$(date +%Y-%m-%d)/
echo -e "备份 ${source_path} 文件到 ${backup_path}/${ip}/$(date +%Y-%m-%d)/ 已完成!" | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
echo -e "ls -l ${backup_path}/${ip}/$(date +%Y-%m-%d)/" | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
ls -la ${backup_path}/${ip}/$(date +%Y-%m-%d)/ | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
#收集docker ps -a记录
echo -e "\n===========================当前时间 $(date '+%Y-%m-%d %H:%M:%S') 执行docker ps -a的记录========================="| tee ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_list_info.log
docker ps -a | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_list_info.log
cat ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_list_info.log >> ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
#收集docker volumes的记录
echo -e "\n===============当前时间 $(date '+%Y-%m-%d %H:%M:%S') 各个docker容器映射目录记录===================="| tee ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log
list=`docker ps -a | awk '{print $NF}' | grep -v "NAMES"`
for names in $list
do
echo -e "============容器$names 的映射目录记录==============" | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log
docker inspect -f "{{ .HostConfig.Binds }}" $names | tee -a ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log
done
cat ${backup_path}/${ip}/$(date +%Y-%m-%d)/docker_volumes_info.log >> ${backup_path}/${ip}/$(date +%Y-%m-%d)/backup.log
echo "============已全部完成=============="
拉取备份到备份服务器:
#!/bin/bash
#加脚本设定分隔符是因为这台服务器默认为空格,而不是换行,以下是设置换行分割
IFS=$'\n'
#for line in `cat nginx_list.txt`
#do
# echo "File:${line}"
#done
for line in `cat nginx_list.txt`
do
echo "================= ${line} ================="
echo ${line} > /tmp/line.txt
IP=`awk '{print $1}' /tmp/line.txt`
password=`awk '{print $2}' /tmp/line.txt`
echo "=================开始处理 ${IP} 服务器配置数据================="
#echo "=================服务器IP地址为 ${IP}================="
#echo "=================服务器登陆密码为 ${password} ================="
ssh-keyscan -t rsa ${IP} >> ~/.ssh/known_hosts
sshpass -p $password scp -r root@${IP}:/data/back_up/${IP} /data/www/wwwroot/10.10.38.20/
echo "=================处理 ${IP} 服务器配置数据=结束================"
echo '' > /tmp/line.txt
done
文章目录
关闭