赋权
chmod +x backup.sh
凌晨3点执行
crontab -e
0 3 * * * /home/dmbak/backup.sh
脚本内容:
#!/bin/bash# 定义数据库连接信息
DB_HOST="192.168.1.1"
DB_PORT="5236"
DB_USER=""
DB_PASSWORD=""
DB_NAME=""
# 数据库 bin 路径
DM_BIN="/home/dmdba/dmdbms/bin"
# 保留备份的天数
DAYS_TO_KEEP=180# 定义备份文件存储路径
BACKUP_DIR="/home/dmbak"
BACKUP_FILE="${BACKUP_DIR}/backup_${DB_NAME}_$(date +%Y%m%d%H%M%S).dmp"# 创建备份目录
mkdir -p $BACKUP_DIR# 执行备份命令
echo "开始备份数据库..."
cd ${DM_BIN}
./dexp ${DB_USER}/${DB_PASSWORD}@${DB_HOST}:${DB_PORT} directory=${BACKUP_DIR} file=${BACKUP_FILE} SCHEMAS=${DB_NAME}
echo "备份完成。"# 删除过期备份
find $BACKUP_DIR -mtime +$DAYS_TO_KEEP -type f -name \*.dmp -exec rm -f {} \;# 检查备份是否成功
if [ $? -eq 0 ]; thenecho "备份完成: $BACKUP_FILE"
elseecho "备份失败。"
fi