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"
backup_location='/data/pg_back'
expire_backup_delete="ON"
expire_days=3
backup_time=`date +%Y%m%d%H%M`
backup_Ymd=`date +%Y-%m-%d`
backup_3ago=`date -d '3 days ago' +%Y-%m-%d`
backup_dir=$backup_location/$backup_Ymd
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 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
|