备份脚本

cronjob备份脚本

mongo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: backup-mongo
namespace: db
spec:
schedule: 0 1 * * *
startingDeadlineSeconds: 600
concurrencyPolicy: Replace
suspend: false
jobTemplate:
metadata:
creationTimestamp: null
spec:
template:
metadata:
creationTimestamp: null
spec:
volumes:
- name: shell
configMap:
name: backup-mongo
defaultMode: 420
- name: data
persistentVolumeClaim:
claimName: dbbackup
containers:
- name: pod
image: bitnami/mongodb:4.4.13-debian-10-r52
command:
- sh
- '-c'
args:
- echo "start backup";bash /root/backup-mongo.sh
resources:
limits:
cpu: '1'
memory: 300Mi
requests:
cpu: 50m
memory: 300Mi
volumeMounts:
- name: shell
mountPath: /root
- name: data
mountPath: /data
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
apiVersion: v1
kind: ConfigMap
metadata:
name: backup-mongo
namespace: db
data:
backup-mongo.sh: >
#!/bin/bash

DB_HOST='xxxxx:27017'

DB_USER='xxxx' #数据库账号

DB_PASS='xxxxxxxxx' #数据库密码

DAYS=20 #DAYS=20代表删除20天前的备份,即只保留近20天的备份




OUT_DIR=/data/mongodb_bak/mongodb_bak_now #临时备份目录

TAR_DIR=/data/mongodb_bak/mongodb_bak_list #备份存放路径

DATE=`date +%Y_%m_%d` #获取当前系统时间


TAR_BAK="mongodb_bak_$DATE.tar.gz" #最终保存的数据库备份文件

cd $OUT_DIR

echo "删除临时备份目录"

rm -rf $OUT_DIR/*

mkdir -p $OUT_DIR/$DATE

echo "删除失败文件"

rm -rf $TAR_DIR/$TAR_BAK

echo "开始备份"

mongodump -h $DB_HOST -u $DB_USER -p $DB_PASS --authenticationDatabase
"admin" -o $OUT_DIR/$DATE #备份全部数据库

echo "开始压缩"

tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE #压缩为.tar.gz格式

echo "删除之前的备份文件"

find $TAR_DIR/ -mtime +$DAYS -delete #删除20天前的备份文件

Postgres:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: backup-postgres
namespace: db
spec:
schedule: 0 1 * * *
startingDeadlineSeconds: 600
concurrencyPolicy: Replace
suspend: false
jobTemplate:
spec:
template:
spec:
volumes:
- name: shell
configMap:
name: backup-postgres
defaultMode: 420
- name: data
persistentVolumeClaim:
claimName: dbbackup
containers:
- name: pod
image: nbugs.tencentcloudcr.com/nbugs-middleware/postgresql:10
command:
- sh
- '-c'
args:
- echo "start backup";bash /root/backup-postgres.sh
resources:
limits:
cpu: '1'
memory: 300Mi
requests:
cpu: 50m
memory: 300Mi
volumeMounts:
- name: shell
mountPath: /root
- name: data
mountPath: /data
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Never
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext: {}
schedulerName: default-scheduler
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
apiVersion: v1
kind: ConfigMap
metadata:
name: backup-postgres
namespace: db
data:
backup-postgres.sh: >-
postgreSQL_user="postgres" #postgreSQL备份用户

postgreSQL_host="xxxxxxxx" #数据库地址

postgreSQL_port="5432" #端口号

postgreSQL_pw="xxxxxxx"

postgreSQL_charset="utf8" #postgreSQL编码

backup_location='/data/pg_back' #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹

expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭

expire_days=3 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效

backup_time=`date +%Y%m%d%H%M` #定义备份详细时间

backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间

backup_3ago=`date -d '3 days ago' +%Y-%m-%d` #3天之前的日期

backup_dir=$backup_location/$backup_Ymd #备份文件夹全路径

# backup_db_arr=("konga") #要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")

backup_db_arr=`echo "SELECT datname FROM pg_database;" |
PGPASSWORD=admin123Nbugs psql -h$postgreSQL_host -p$postgreSQL_port
-U$postgreSQL_user | egrep -v "rows|datname|-----"`

rm -rf $backup_dir/*

welcome_msg="Welcome to use PostgreSQL backup tools!" #欢迎语

PGPASSWORD=$postgreSQL_pw psql -h$postgreSQL_host -p$postgreSQL_port
-U$postgreSQL_user <<end

end

flag=`echo $?`

if [ $flag != "0" ]; then
echo "ERROR:Can't connect postgreSQL server! backup stop!"
exit 1
else
echo "postgreSQL connect ok! 开始备份 Please wait......"
# 判断有没有定义备份的数据库,如果定义则开始备份,否则退出备份
if [ "$backup_db_arr" != "" ];then
#dbnames=$(cut -d ',' -f1-5 $backup_database)
#echo "arr is (${backup_db_arr[@]})"
for dbname in ${backup_db_arr[@]}
do
echo "database $dbname backup start..."
`mkdir -p $backup_dir`
`PGPASSWORD=$postgreSQL_pw pg_dump -h$postgreSQL_host -p$postgreSQL_port -U$postgreSQL_user -d$dbname | gzip > $backup_dir/$dbname-$backup_time.sql.gz`
flag=`echo $?`
if [ $flag == "0" ];then
echo "database $dbname success backup to $backup_dir/$dbname-$backup_time.sql.gz"
else
echo "database $dbname backup fail!"
exit 1
fi
done
else
echo "ERROR:No database to backup! backup stop"
exit 1
fi
# 如果开启了删除过期备份,则进行删除操作
if [ "$expire_backup_delete" == "ON" -a "$backup_location" != "" ];then
`find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`
echo "Expired backup data delete complete!"
fi
echo "All database backup success! Thank you! 数据库备份完成!!!!!"
exit
fi