备份大全(未测试)

2024年 2月 1日 94.9k 0

在日常运行过程中,为防止出现数据库故障、病毒或者用户操作不当而导致数据丢失,必须要有良好的预备方案,特分享主流数据库的Shell脚本给大家。

1.Oracle RMAN备份

1.1 创建目录

[oracle@OEL7 ~]$ mkdir -p /u01/dbbak/script
[oracle@OEL7 ~]$ cd /u01/dbbak
[oracle@OEL7 ~]$ chown -R oracle:oinstall script

[oracle@OEL7 ~]$ mkdir -p /u01/dbbak/db
[oracle@OEL7 ~]$ mkdir -p /u01/dbbak/arch
[oracle@OEL7 ~]$ cd /u01/dbbak
[oracle@OEL7 ~]$ chown -R oracle:oinstall db
[oracle@OEL7 ~]$ chown -R oracle:oinstall arch

1.2 编辑脚本

[oracle@OEL7 ~]$ vi /u01/dbbak/script/rman_full.sh

export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export ORACLE_SID=prod
dt=`date '+%Y%m%d_%H%M%S'`
/u01/app/oracle/product/19.3.0/dbhome_1/bin/rman target / cmdfile=/u01/dbbak/script/backup.sh log=/u01/dbbak/log/log_$dt.log append

[oracle@OEL7 ~]$ vi /u01/dbbak/script/backup.sh

run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup as compressed backupset database format '/u01/dbbak/db/DB%U.bkp' plus archivelog format '/u01/dbbak/arch/ARCH%U.bkp' delete all input;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
report obsolete;
crosscheck copy;
crosscheck archivelog all;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
}
quit;
EOF

1.3 脚本授权

[oracle@OEL7 ~]$ chmod 775 /u01/dbbak/script/backup.sh
[oracle@OEL7 ~]$ chmod 775 /u01/dbbak/script/rman_full.sh

1.4 执行脚本

/u01/dbbak/script/rman_full.sh

1.5 定时任务

crontab -e 为编辑窗口

[oracle@OEL7 ~]$ crontab -l
0 2 * * * /u01/dbbak/script/rman_full.sh

1.6 备份删除

[oracle@OEL7 ~]$ rman target /

RMAN> show all;
--备份策略调整,备份保留7天
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;

2.Oracle逻辑备份

2.1 生成目录

[oracle@OEL7 ~]$ mkdir -p /home/oracle/backup/dump
[oracle@OEL7 ~]$ mkdir -p /home/oracle/backup/tars

#使用expdp时需要先指定转储文件和日志文件所在的目录,可以通过如下命令实现
SQL> CREATE OR REPLACE DIRECTORY scott_backup as '/home/oracle/backup/dump';
SQL> grant read,write on directory scott_backup to public;

2.2 备份脚本


[oracle@OEL7 ~]$ vi /home/oracle/backup/exp_backup.sh

#!/bin/bash
#Oracle 环境变量
NLS_LANG=AMERICAN_AMERICA.AL32UTF8
ORACLE_SID=prod
ORACLE_BASE=/opt/oracle
ORACLE_HOME=/opt/oracle/11g
PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin
export ORACLE_SID ORACLE_BASE ORACLE_HOME NLS_LANG PATH

#获取时间戳
#dump生成时间
export BAKUPTIME=`date +%Y%m%d%H%M%S`
#压缩文件存放目录
export DATA_DIR=/home/oracle/backup/dump
export TAR_DIR=/home/oracle/backup/tars
cd $DATA_DIR
echo "Starting bakup..."
echo "Bakup file path $DATA_DIR/scott_expdp_$BAKUPTIME.dmp"
#parallel=4 为并行度,对于备份大数据库有助于减少备份时间,但会增加CPU负载。
expdp scott/tiger directory=scott_backup dumpfile=scott_expdp_$BAKUPTIME.dump logfile=scott_expdp_$BAKUPTIME.log parallel=4
echo "Starting tar..."
echo "Tar file path $TAR_DIR/SCOTT_$BAKUPTIME.tar.gz"
tar -zcvf $TAR_DIR/scott_expdp_$BAKUPTIME.tar.gz scott_expdp*
echo "Bakup job is done!"
#历史dump文件保留7天
find $DATA_DIR -type f -mtime +7 -exec rm -rf {} ;

2.3 定时任务

crontab -e 为编辑窗口

[oracle@OEL7 ~]$ crontab -l
0 3 * * * /home/oracle/backup/exp_backup.sh

3.MySQL逻辑全|增备份

3.1 开启binlog日志

binlog日志默认不开启,修改参数后重启MySQL数据库
mysql> show variables like '%log_bin%';

# vim /etc/my.cnf
log_bin=ON
log_bin_basename=/home/mysql/mysql-bin
log_bin_index=/home/mysql/mysql-bin.index
## 参数说明
log_bin:开启binlog日志文件,默认值为OFF。
log_bin_basename:binlog日志的基本文件名。MySQL会在该文件名后追加标识来表示每一个binlog文件。
log_bin_index:binlog文件的索引文件,管理所有的binlog文件。

3.2 全备脚本

# mkdir -p /home/mysql/daily
# mkdir -p /home/mysql/backup
# vi /home/mysql/Mysql-FullyBak.sh

#mysqldump to Fully backup mysql data per week!
source /etc/profile
BakDir=/home/mysql/backup
LogFile=/home/mysql/backup/bak.log
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
/usr/bin/mysqldump -uroot -proot --quick --events --databases wmp --flush-logs --delete-master-logs --single-transaction >$DumpFile
/bin/tar -zvcf $GZDumpFile $DumpFile
/bin/rm $DumpFile
oldDate=`date -d '7 days ago' +%Y%m%d`
oldBakFile=${oldDate}".sql.tgz"
/bin/rm $oldBakFile
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
cd $BakDir/daily
/bin/rm -f *

3.3 增备脚本

# vi /home/mysql/Mysql-DailyBak.sh
#use cp to bakup mysql data everyday!
source /etc/profile
BakDir=/home/mysql/backup/daily
BinDir=/home/mysql/mysql-bin
LogFile=/home/mysql/backup/bak.log
BinFile=/home/mysql/mysql-bin/mysql-bin.index
/usr/bin/mysqladmin -uroot -proot flush-logs
Counter=`wc -l $BinFile |awk '{print $1}'`
#产生新的mysql-bin.00000*文件
NextNum=0
#比对$Counter和¥NextNum这两个值来确定文件是不是最新的
for file in `cat $BinFile`
do
base=`basename $file`
#basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
NextNum=`expr $NextNum + 1`
if [[ $NextNum -eq $Counter ]]
then
echo $base skip! >> $LogFile
else
dest=$BakDir/$base
if(test -e $dest)
#test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去
then
echo $base exist! >> $LogFile
else
cp $BinDir/$base $BakDir
echo $base copying >> $LogFile
fi
fi
done

3.4 定时任务

在命令行输入:
#crontab -e
添加相应的任务,wq存盘退出
#每个星期日凌晨3:00执行完全备份脚本
0 3 * * 0 /bin/bash -x /home/mysql/Mysql-FullyBak.sh >/dev/null 2>&1
#周一到周六凌晨3:00做增量备份
0 3 * * 1-6 /bin/bash -x /home/mysql/Mysql-DailyBak.sh >/dev/null 2>&1

3.5 恢复步骤

4.MySQL PXB全/增备份

Xtrabackup是一个开源的MySQL数据库备份工具,由Percona公司开发和维护

4.1 备份脚本

vi /home/mysql/scripts/backup.sh

#!/bin/bash

echo ""
START_TIME=`date`
echo "############## backup start at $START_TIME ##############"
echo ""
###you need install xtrabackup!###
# Set env
source /home/mysql/.bash_profile
which xtrabackup
# Database Info
DB_USER="root"
DB_PASS="jeames@123"
CONF="/data/mysqldb/conf/mysql.conf"
SOCKET="/data/mysqldb/socket/mysql.sock"
BAK_BASE="/db_bak/mysql_bak/mysql"
DATE=`date +%F`
YESTERDAY=`date +%F -d "-1 days"`
WEEK_DAY=`date +%w`
BAK_DIR=$BAK_BASE/$DATE-$WEEK_DAY
# Create Directory and backup
if [ "$WEEK_DAY" == "6" ]; then
xtrabackup --defaults-file=$CONF --socket=$SOCKET --backup --user=$DB_USER --password=$DB_PASS --target-dir=$BAK_DIR --compress
elif [ "$WEEK_DAY" == "0" ]; then
INCRE_BASE=$BAK_BASE/$YESTERDAY-6
xtrabackup --defaults-file=$CONF --socket=$SOCKET --backup --user=$DB_USER --password=$DB_PASS --target-dir=$BAK_DIR --incremental-basedir=$INCRE_BASE --compress
else
INCRE_BASE=$BAK_BASE/$YESTERDAY-$[WEEK_DAY-1]
xtrabackup --defaults-file=$CONF --socket=$SOCKET --backup --user=$DB_USER --password=$DB_PASS --target-dir=$BAK_DIR --incremental-basedir=$INCRE_BASE --compress
fi
echo ""
END_TIME=`date`
echo "############## backup end at $END_TIME ##############"
echo ""

4.2 备份删除

vi /home/mysql/scripts/cleanup.sh
#!/bin/bash

echo ""
START_TIME=`date`
echo "############## clean up start at $START_TIME ##############"
echo ""

find /db_bak/mysql_bak/mysql -maxdepth 1 -type d -mtime +30
find /db_bak/mysql_bak/mysql -maxdepth 1 -type d -mtime +30 -exec rm -rf {} ;

echo ""
END_TIME=`date`
echo "############## clean up end at $END_TIME ##############"
echo ""

4.3 定时任务

运行脚本
每天凌晨 4:10 分清理 30 天之前的备份,
每天 4:30 分使用 xtrabackup 进行备份,
注意只有周六是全备,其他时间均是增备。
#crontab -e
10 4 * * * /home/mysql/scripts/cleanup.sh >> /home/mysql/scripts/cleanup.log 2>&1
30 4 * * * /home/mysql/scripts/backup.sh >> /home/mysql/scripts/backup.log 2>&1

5.PostgreSQL逻辑备份

以下是一个用于定时备份 PostgreSQL 数据库的示例脚本。这个脚本将使用 pg_dump 工具来创建数据库备份,然后将备份文件保存到指定的目录中,并可选择保留最近一段时间内的备份文件

5.1 备份脚本

vi /data/script_name.sh

#!/bin/bash

# PostgreSQL数据库相关信息
db_host="localhost"
db_port="5432"
db_name="database_name"
db_user="database_user"
db_password="database_password"

# 备份存储目录
backup_dir="/data/backup/folder"

# 保留备份的天数
retention_days=7

# 创建备份目录
mkdir -p $backup_dir

# 备份文件名
backup_file="$backup_dir/backup_$(date +'%Y%m%d%H%M%S').sql"

# 执行备份
PGPASSWORD=$db_password
pg_dump -h $db_host -p $db_port -U $db_user -F c -b -v -f "$backup_file" $db_name

if [ $? -eq 0 ]; then
echo "数据库备份成功: $backup_file"

# 删除旧的备份文件
find $backup_dir -name "backup_*.sql" -type f -mtime +$retention_days -exec rm -f {} ;
else
echo "数据库备份失败."
fi

5.2 定时任务

在命令行输入:
#crontab -e
#每天定时凌晨2点定时任务
0 2 * * * /data/script_name.sh

5.3 备份恢复

--恢复
drop database jmedb;
create database jmedb;;
psql --file=jmedb.sql --先查看可否有创建数据库的语句
psql --dbname=db2 --file=jmedb.sql --先查看可否有创建数据库的语句

6.PostgreSQL物理备份

pg_rman是一个开源的PostgreSQL备份软件,pg_rman跑的不是流复制协议,而是文件拷贝,所以pg_rman必须和数据库Server安装在一起.

6.1 备份脚本

#!/bin/bash

source /home/postgres/.bash_profile

DATE=`date +%Y%m%d`;
PG_HOME=/home/postgres
BACK_LOG=/home/postgres/log/pg_rman_${DATE}.log

#START BACKUP
echo "START BACKUP" > $BACK_LOG
#执行备份命令
pg_rman backup --backup-mode=full -B /rmanbk >> $BACK_LOG
#备份集校验
pg_rman validate >> $BACK_LOG
#检查备份是否成功
error_num=`pg_rman show | awk 'BEGIN{n=0}{if(NR > 3 && $8 != "OK")n++}END{print n}'`
if [ $error_num > 0 ];then
    message="Postgres 数据库服务器${hostname}在${DATE}备份失败"
    echo $message
fi
#清理无效备份集
pg_rman purge >> $BACK_LOG
echo "BACKUP  END" >> $BACK_LOG

6.2 备份恢复

--原地恢复,使用新的$PGDATA恢复
pg_ctl stop
rm -rf /postgresql/pgdata/
pg_rman restore -B /rmanbk

-- 检查配置文件是否有问题,若无问题则可以启动PG
pg_ctl start

--检验数据是否正确
启动PG后,会删除recovery.signal文件

7.openGauss备份

7.1 备份脚本

vi /home/omm/backup.sh

# database dump shell
# you should change the GAUSSHOME GAUSSPORT GAUSSDATA DUMP_USER DUMP_PASSWORD
#!/bin/bash
source /etc/profile
source /home/omm/.bash_profile
export GAUSSHOME=/opt/gaussdb/app
export GAUSSPORT=26000
export GAUSSDATA=/gaussdb/data/dn1
export PATH=$PGHOME/bin:$PATH
DUMP_USER=ysla
DUMP_PASSWORD='jeames007@HW'
CUR_DATE=`date "+%Y-%m-%d-%H%M"`
dbnamelist=`cat db.txt`

#Loading DBLIST
gsql -p ${GAUSSPORT} postgres -c "select datname from pg_database where datname not in ('template1','template0','postgres')" -t | grep -v '^$' >db.txt

#save directory
SAVE_BASE_DIR="/gaussdb/dump_dir"
DAT_FILE_DIR="${SAVE_BASE_DIR}/${CUR_DATE}"
if [ -d ${DAT_FILE_DIR} ]
then :
else
mkdir -p ${DAT_FILE_DIR}
fi
# The real backup step!
echo "`date "+%Y-%m-%d-%H%M"` begin backup db "
for dbname in ${dbnamelist}
do
gs_dump -E UTF8 ${dbname} -U ${DUMP_USER} -W ${DUMP_PASSWORD} -p ${GAUSSPORT} -F p -f ${DAT_FILE_DIR}/${dbname}_${CUR_DATE}.sql
gs_dumpall -l ${dbname} -U ${DUMP_USER} -W ${DUMP_PASSWORD} -p ${GAUSSPORT} -g -f ${DAT_FILE_DIR}/global_data_${dbname}_${CUR_DATE}.sql
done
tar -cjvf ${DAT_FILE_DIR}.tar.gz /${DAT_FILE_DIR} --remove-files
echo "`date "+%Y-%m-%d-%H%M"` end backup db "

7.2 定时任务

在命令行输入:
#crontab -e
30 02 * * * sh /home/omm/backup.sh

7.3 备份清理

每天都进行备份,如果备份天数过多不清理,
可能使目录打满,因此需要添加备份清理策略
在命令行输入:
#crontab -e
30 03 * * * find /gaussdb/dump_dir -not -path '*/.*' -mtime +30 -type f -name *.tar.gz -exec rm -rf在日常运行过程中,为防止出现数据库故障、病毒或者用户操作不当而导致数据丢失,必须要有良好的预备方案,特分享主流数据库的Shell脚本给大家。
1.Oracle RMAN备份
1.1 创建目录
[oracle@OEL7 ~]$ mkdir -p /u01/dbbak/script
[oracle@OEL7 ~]$ cd /u01/dbbak
[oracle@OEL7 ~]$ chown -R oracle:oinstall script

[oracle@OEL7 ~]$ mkdir -p /u01/dbbak/db
[oracle@OEL7 ~]$ mkdir -p /u01/dbbak/arch
[oracle@OEL7 ~]$ cd /u01/dbbak
[oracle@OEL7 ~]$ chown -R oracle:oinstall db
[oracle@OEL7 ~]$ chown -R oracle:oinstall arch
1.2 编辑脚本
[oracle@OEL7 ~]$ vi /u01/dbbak/script/rman_full.sh

export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export ORACLE_SID=prod
dt=`date '+%Y%m%d_%H%M%S'`
/u01/app/oracle/product/19.3.0/dbhome_1/bin/rman target / cmdfile=/u01/dbbak/script/backup.sh log=/u01/dbbak/log/log_$dt.log append

[oracle@OEL7 ~]$ vi /u01/dbbak/script/backup.sh

run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup as compressed backupset database format '/u01/dbbak/db/DB%U.bkp' plus archivelog format '/u01/dbbak/arch/ARCH%U.bkp' delete all input;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
report obsolete;
crosscheck copy;
crosscheck archivelog all;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
}
quit;
EOF
1.3 脚本授权
[oracle@OEL7 ~]$ chmod 775 /u01/dbbak/script/backup.sh
[oracle@OEL7 ~]$ chmod 775 /u01/dbbak/script/rman_full.sh
1.4 执行脚本
/u01/dbbak/script/rman_full.sh

1.5 定时任务
crontab -e 为编辑窗口

[oracle@OEL7 ~]$ crontab -l
0 2 * * * /u01/dbbak/script/rman_full.sh
1.6 备份删除
[oracle@OEL7 ~]$ rman target /

RMAN> show all;
--备份策略调整,备份保留7天
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
2.Oracle逻辑备份
2.1 生成目录
[oracle@OEL7 ~]$ mkdir -p /home/oracle/backup/dump
[oracle@OEL7 ~]$ mkdir -p /home/oracle/backup/tars

#使用expdp时需要先指定转储文件和日志文件所在的目录,可以通过如下命令实现
SQL> CREATE OR REPLACE DIRECTORY scott_backup as '/home/oracle/backup/dump';
SQL> grant read,write on directory scott_backup to public;
2.2 备份脚本

[oracle@OEL7 ~]$ vi /home/oracle/backup/exp_backup.sh

#!/bin/bash
#Oracle 环境变量
NLS_LANG=AMERICAN_AMERICA.AL32UTF8
ORACLE_SID=prod
ORACLE_BASE=/opt/oracle
ORACLE_HOME=/opt/oracle/11g
PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin
export ORACLE_SID ORACLE_BASE ORACLE_HOME NLS_LANG PATH

#获取时间戳
#dump生成时间
export BAKUPTIME=`date +%Y%m%d%H%M%S`
#压缩文件存放目录
export DATA_DIR=/home/oracle/backup/dump
export TAR_DIR=/home/oracle/backup/tars
cd $DATA_DIR
echo "Starting bakup..."
echo "Bakup file path $DATA_DIR/scott_expdp_$BAKUPTIME.dmp"
#parallel=4 为并行度,对于备份大数据库有助于减少备份时间,但会增加CPU负载。
expdp scott/tiger directory=scott_backup dumpfile=scott_expdp_$BAKUPTIME.dump logfile=scott_expdp_$BAKUPTIME.log parallel=4
echo "Starting tar..."
echo "Tar file path $TAR_DIR/SCOTT_$BAKUPTIME.tar.gz"
tar -zcvf $TAR_DIR/scott_expdp_$BAKUPTIME.tar.gz scott_expdp*
echo "Bakup job is done!"
#历史dump文件保留7天
find $DATA_DIR -type f -mtime +7 -exec rm -rf {} ;
2.3 定时任务
crontab -e 为编辑窗口

[oracle@OEL7 ~]$ crontab -l
0 3 * * * /home/oracle/backup/exp_backup.sh
3.MySQL逻辑全|增备份
3.1 开启binlog日志
binlog日志默认不开启,修改参数后重启MySQL数据库
mysql> show variables like '%log_bin%';

# vim /etc/my.cnf
log_bin=ON
log_bin_basename=/home/mysql/mysql-bin
log_bin_index=/home/mysql/mysql-bin.index
## 参数说明
log_bin:开启binlog日志文件,默认值为OFF。
log_bin_basename:binlog日志的基本文件名。MySQL会在该文件名后追加标识来表示每一个binlog文件。
log_bin_index:binlog文件的索引文件,管理所有的binlog文件。
3.2 全备脚本
# mkdir -p /home/mysql/daily
# mkdir -p /home/mysql/backup
# vi /home/mysql/Mysql-FullyBak.sh

#mysqldump to Fully backup mysql data per week!
source /etc/profile
BakDir=/home/mysql/backup
LogFile=/home/mysql/backup/bak.log
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
/usr/bin/mysqldump -uroot -proot --quick --events --databases wmp --flush-logs --delete-master-logs --single-transaction >$DumpFile
/bin/tar -zvcf $GZDumpFile $DumpFile
/bin/rm $DumpFile
oldDate=`date -d '7 days ago' +%Y%m%d`
oldBakFile=${oldDate}".sql.tgz"
/bin/rm $oldBakFile
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
cd $BakDir/daily
/bin/rm -f *
3.3 增备脚本
# vi /home/mysql/Mysql-DailyBak.sh
#use cp to bakup mysql data everyday!
source /etc/profile
BakDir=/home/mysql/backup/daily
BinDir=/home/mysql/mysql-bin
LogFile=/home/mysql/backup/bak.log
BinFile=/home/mysql/mysql-bin/mysql-bin.index
/usr/bin/mysqladmin -uroot -proot flush-logs
Counter=`wc -l $BinFile |awk '{print $1}'`
#产生新的mysql-bin.00000*文件
NextNum=0
#比对$Counter和¥NextNum这两个值来确定文件是不是最新的
for file in `cat $BinFile`
do
base=`basename $file`
#basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
NextNum=`expr $NextNum + 1`
if [[ $NextNum -eq $Counter ]]
then
echo $base skip! >> $LogFile
else
dest=$BakDir/$base
if(test -e $dest)
#test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去
then
echo $base exist! >> $LogFile
else
cp $BinDir/$base $BakDir
echo $base copying >> $LogFile
fi
fi
done
3.4 定时任务
在命令行输入:
#crontab -e
添加相应的任务,wq存盘退出
#每个星期日凌晨3:00执行完全备份脚本
0 3 * * 0 /bin/bash -x /home/mysql/Mysql-FullyBak.sh >/dev/null 2>&1
#周一到周六凌晨3:00做增量备份
0 3 * * 1-6 /bin/bash -x /home/mysql/Mysql-DailyBak.sh >/dev/null 2>&1
3.5 恢复步骤

4.MySQL PXB全/增备份
Xtrabackup是一个开源的MySQL数据库备份工具,由Percona公司开发和维护

4.1 备份脚本
vi /home/mysql/scripts/backup.sh

#!/bin/bash

echo ""
START_TIME=`date`
echo "############## backup start at $START_TIME ##############"
echo ""
###you need install xtrabackup!###
# Set env
source /home/mysql/.bash_profile
which xtrabackup
# Database Info
DB_USER="root"
DB_PASS="jeames@123"
CONF="/data/mysqldb/conf/mysql.conf"
SOCKET="/data/mysqldb/socket/mysql.sock"
BAK_BASE="/db_bak/mysql_bak/mysql"
DATE=`date +%F`
YESTERDAY=`date +%F -d "-1 days"`
WEEK_DAY=`date +%w`
BAK_DIR=$BAK_BASE/$DATE-$WEEK_DAY
# Create Directory and backup
if [ "$WEEK_DAY" == "6" ]; then
xtrabackup --defaults-file=$CONF --socket=$SOCKET --backup --user=$DB_USER --password=$DB_PASS --target-dir=$BAK_DIR --compress
elif [ "$WEEK_DAY" == "0" ]; then
INCRE_BASE=$BAK_BASE/$YESTERDAY-6
xtrabackup --defaults-file=$CONF --socket=$SOCKET --backup --user=$DB_USER --password=$DB_PASS --target-dir=$BAK_DIR --incremental-basedir=$INCRE_BASE --compress
else
INCRE_BASE=$BAK_BASE/$YESTERDAY-$[WEEK_DAY-1]
xtrabackup --defaults-file=$CONF --socket=$SOCKET --backup --user=$DB_USER --password=$DB_PASS --target-dir=$BAK_DIR --incremental-basedir=$INCRE_BASE --compress
fi
echo ""
END_TIME=`date`
echo "############## backup end at $END_TIME ##############"
echo ""
4.2 备份删除
vi /home/mysql/scripts/cleanup.sh
#!/bin/bash

echo ""
START_TIME=`date`
echo "############## clean up start at $START_TIME ##############"
echo ""

find /db_bak/mysql_bak/mysql -maxdepth 1 -type d -mtime +30
find /db_bak/mysql_bak/mysql -maxdepth 1 -type d -mtime +30 -exec rm -rf {} ;

echo ""
END_TIME=`date`
echo "############## clean up end at $END_TIME ##############"
echo ""
4.3 定时任务
运行脚本
每天凌晨 4:10 分清理 30 天之前的备份,
每天 4:30 分使用 xtrabackup 进行备份,
注意只有周六是全备,其他时间均是增备。
#crontab -e
10 4 * * * /home/mysql/scripts/cleanup.sh >> /home/mysql/scripts/cleanup.log 2>&1
30 4 * * * /home/mysql/scripts/backup.sh >> /home/mysql/scripts/backup.log 2>&1
5.PostgreSQL逻辑备份
以下是一个用于定时备份 PostgreSQL 数据库的示例脚本。这个脚本将使用 pg_dump 工具来创建数据库备份,然后将备份文件保存到指定的目录中,并可选择保留最近一段时间内的备份文件

5.1 备份脚本
vi /data/script_name.sh

#!/bin/bash

# PostgreSQL数据库相关信息
db_host="localhost"
db_port="5432"
db_name="database_name"
db_user="database_user"
db_password="database_password"

# 备份存储目录
backup_dir="/data/backup/folder"

# 保留备份的天数
retention_days=7

# 创建备份目录
mkdir -p $backup_dir

# 备份文件名
backup_file="$backup_dir/backup_$(date +'%Y%m%d%H%M%S').sql"

# 执行备份
PGPASSWORD=$db_password
pg_dump -h $db_host -p $db_port -U $db_user -F c -b -v -f "$backup_file" $db_name

if [ $? -eq 0 ]; then
echo "数据库备份成功: $backup_file"

# 删除旧的备份文件
find $backup_dir -name "backup_*.sql" -type f -mtime +$retention_days -exec rm -f {} ;
else
echo "数据库备份失败."
fi
5.2 定时任务
在命令行输入:
#crontab -e
#每天定时凌晨2点定时任务
0 2 * * * /data/script_name.sh
5.3 备份恢复
--恢复
drop database jmedb;
create database jmedb;;
psql --file=jmedb.sql --先查看可否有创建数据库的语句
psql --dbname=db2 --file=jmedb.sql --先查看可否有创建数据库的语句
6.PostgreSQL物理备份
pg_rman是一个开源的PostgreSQL备份软件,pg_rman跑的不是流复制协议,而是文件拷贝,所以pg_rman必须和数据库Server安装在一起.

6.1 备份脚本
#!/bin/bash

source /home/postgres/.bash_profile

DATE=`date +%Y%m%d`;
PG_HOME=/home/postgres
BACK_LOG=/home/postgres/log/pg_rman_${DATE}.log

#START BACKUP
echo "START BACKUP" > $BACK_LOG
#执行备份命令
pg_rman backup --backup-mode=full -B /rmanbk >> $BACK_LOG
#备份集校验
pg_rman validate >> $BACK_LOG
#检查备份是否成功
error_num=`pg_rman show | awk 'BEGIN{n=0}{if(NR > 3 && $8 != "OK")n++}END{print n}'`
if [ $error_num > 0 ];then
message="Postgres 数据库服务器${hostname}在${DATE}备份失败"
echo $message
fi
#清理无效备份集
pg_rman purge >> $BACK_LOG
echo "BACKUP END" >> $BACK_LOG
6.2 备份恢复
--原地恢复,使用新的$PGDATA恢复
pg_ctl stop
rm -rf /postgresql/pgdata/
pg_rman restore -B /rmanbk

-- 检查配置文件是否有问题,若无问题则可以启动PG
pg_ctl start

--检验数据是否正确
启动PG后,会删除recovery.signal文件

7.openGauss备份
7.1 备份脚本
vi /home/omm/backup.sh

# database dump shell
# you should change the GAUSSHOME GAUSSPORT GAUSSDATA DUMP_USER DUMP_PASSWORD
#!/bin/bash
source /etc/profile
source /home/omm/.bash_profile
export GAUSSHOME=/opt/gaussdb/app
export GAUSSPORT=26000
export GAUSSDATA=/gaussdb/data/dn1
export PATH=$PGHOME/bin:$PATH
DUMP_USER=ysla
DUMP_PASSWORD='jeames007@HW'
CUR_DATE=`date "+%Y-%m-%d-%H%M"`
dbnamelist=`cat db.txt`

#Loading DBLIST
gsql -p ${GAUSSPORT} postgres -c "select datname from pg_database where datname not in ('template1','template0','postgres')" -t | grep -v '^$' >db.txt

#save directory
SAVE_BASE_DIR="/gaussdb/dump_dir"
DAT_FILE_DIR="${SAVE_BASE_DIR}/${CUR_DATE}"
if [ -d ${DAT_FILE_DIR} ]
then :
else
mkdir -p ${DAT_FILE_DIR}
fi
# The real backup step!
echo "`date "+%Y-%m-%d-%H%M"` begin backup db "
for dbname in ${dbnamelist}
do
gs_dump -E UTF8 ${dbname} -U ${DUMP_USER} -W ${DUMP_PASSWORD} -p ${GAUSSPORT} -F p -f ${DAT_FILE_DIR}/${dbname}_${CUR_DATE}.sql
gs_dumpall -l ${dbname} -U ${DUMP_USER} -W ${DUMP_PASSWORD} -p ${GAUSSPORT} -g -f ${DAT_FILE_DIR}/global_data_${dbname}_${CUR_DATE}.sql
done
tar -cjvf ${DAT_FILE_DIR}.tar.gz /${DAT_FILE_DIR} --remove-files
echo "`date "+%Y-%m-%d-%H%M"` end backup db "
7.2 定时任务
在命令行输入:
#crontab -e
30 02 * * * sh /home/omm/backup.sh
7.3 备份清理
每天都进行备份,如果备份天数过多不清理,
可能使目录打满,因此需要添加备份清理策略
在命令行输入:
#crontab -e
30 03 * * * find /gaussdb/dump_dir -not -path '*/.*' -mtime +30 -type f -name *.tar.gz -exec rm -rf

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论