Elasticsearch备份
#!/bin/sh
set -x
today=`date +%Y%m%d`
curl --location --request PUT http://localhost:22063/_snapshot/es_bak --header 'Content-Type: application/json' --data-raw '{"type": "fs","settings":{"compress": true,"location": "/data/backup/es_bak"}}'
curl --location --request PUT http://localhost:22063/_snapshot/es_bak/snapshot_$today
while true; do
status=$(curl --silent --location --request GET "http://localhost:22063/_snapshot/es_bak/$snapshot_name" | jq '.snapshots[0].state')
if [ "$status" = '"SUCCESS"' ]; then
echo "Snapshot $snapshot_name completed successfully."
break
elif [ "$status" = '"FAILED"' ]; then
echo "Snapshot $snapshot_name failed."
exit 1
else
echo "Snapshot in progress. Waiting..."
sleep 60
fi
done
seven_days_ago=`date -d "7 day ago" +"%Y%m%d"`
curl --location --request DELETE http://localhost:22063/_snapshot/es_bak/snapshot_$seven_days_ago
ssh casia@172.18.116.127 "rm -rf /data/bakup/es_bak/*"
scp -r /data/elasticsearch/backup/es_bak root@192.168.2.1:/data/bakup/
gitlab备份
#!/bin/sh
set -x
gitlab-rake gitlab:backup:create
path=/var/opt/gitlab/backups/
find $path -type f -mtime +7 | xargs rm -rvf
find $path -type f -ctime 0| xargs -i scp {} root@192.168.2.1:/data/bakup/gitlab
postgresql备份
#!/bin/sh
set -x
today=`date +%Y%m%d`
docker exec postgres bash -c "pg_dumpall -h localhost -p 5432 -U postgres > /var/lib/postgresql/data/pg_bak/$today.bak"
path=/data/postgis/pg_bak/
find $path -type f -mtime +7 | xargs rm -rvf
find $path -type f -ctime 0| xargs -i scp {} root@192.168.2.1:/data/bakup/pg_bak/
k8s中的mysql
#!/bin/bash
set -x
BACKUP=/bitnami/mysql/bakup
DATETIME=$(date +%Y-%m-%d)
echo "===备份开始==="
echo "备份文件存放于${BACKUP}/$DATABASE-$DATETIME.sql"
HOST=localhost
DB_USER=root
DB_PW=123456
DATABASE=mysql_bak
kubectl exec -it my-release-mysql-primary-0 -- bash -c "mysqldump -h localhost -u${DB_USER} -p${DB_PW} --databases db1 db2 db3 > ${BACKUP}/$DATABASE-$DATETIME.sql"
echo "===导出成功,开始传输==="
kubectl cp my-release-mysql-primary-0:${BACKUP}/$DATABASE-$DATETIME.sql /data/bakup/$DATABASE-$DATETIME.sql
kubectl exec -it my-release-mysql-primary-0 -- bash -c "rm ${BACKUP}/$DATABASE-$DATETIME.sql"
scp /data/bakup/$DATABASE-$DATETIME.sql 192.168.2.1:/data/bakup/mysql_bak/
find /data/bakup/ -mtime +7 -name "*.sql" -exec rm {} \;
echo "===数据库备份到服务器成功==="
删除7天前的文件
path1=/data/bakup/cp/
path2=/data/bakup/gitlab/
path3=/data/bakup/pg_bak
path1_file_count=$(ls -1 "$path1" | grep -v '^d' | wc -l)
if [ "$path1_file_count" -gt 7 ]; then
find $path1 -type f -mtime +7 | xargs rm -rvf
else
echo "跳过 $path1"
fi
path2_file_count=$(ls -1 "$path2" | grep -v '^d' | wc -l)
if [ "$path2_file_count" -gt 7 ]; then
find $path2 -type f -mtime +7 | xargs rm -rvf
else
echo "跳过 $path2"
fi
path3_file_count=$(ls -1 "$path3" | grep -v '^d' | wc -l)
if [ "$path3_file_count" -gt 7 ]; then
find $path3 -type f -mtime +7 | xargs rm -rvf
else
echo "跳过 $path3"
fi