#!/bin/bash
#定时任务
#crontab –l
#crontab -r
#crontab -ir
#crontab -e
#59 23 * * * /home/qipaas66/bakmysql.sh
#service crond restart
#当前时间
DATETIME=$(date +%Y-%m-%d)
#备份路径
BACKUP_DIR=/appdata/bak/mysql
BACKUP=${BACKUP_DIR}/${DATETIME}
echo "===备份开始==="
echo "备份文件存放于${BACKUP}"
#数据库地址
HOST=localhost
#数据库用户名
DB_USER=root
#数据库密码
DB_PW=root
#创建备份目录
[ ! -d "${BACKUP}" ] && mkdir -p "${BACKUP}"
#备份数据库
DATABASE=jpaas_bpm
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_config
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_datart
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_form
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_job
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_portal
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_seata
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_system
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_ureport
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=jpaas_user
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
#备份数据库
DATABASE=nacos
echo "开始备份...${DATABASE}"
#mysqldump -u${DB_USER} -p${DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql
mysqldump -h ${HOST} -u${DB_USER} -p${DB_PW} ${DATABASE} >${BACKUP}/$DATABASE.sql
echo "===备份完成==="
#echo "===导出成功,开始传输==="
#压缩成tar.gz包
#cd $BACKUP
#tar -zcvf $DATETIME.tar.gz $DATETIME
#备份到服务器B
#scp $DATABASE-$DATETIME.sql root@1.1.1.2:/appdata/bak/mysql
#删除备份目录
#rm -rf ${BACKUP}/$DATETIME
#删除10天前备份的数据,这边可以自行更改
find $BACKUP_DIR -mtime +10 -name "*.sql" -exec rm -rf {} \;
#echo "===数据库备份到服务器成功==="
脚本文件。
设置执行权限
chmod +x bakmysql.sh
添加执行文件
crontab -e
使用“crontab”命令打开定时任务编辑器,添加一行备份脚本执行的计划,例如每天凌晨3点备份。
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/1 * * * * /home/mysqlbackup/bakmysql.sh
0 3 * * * /bin/bash /path/to/backup.sh >/dev/null 2>&1