!/bin/bash
./etc/profile
.~/.bash_profile
source /etc/profile
备份路径
BACKUP=/www/backup/mysql
当前时间
DATETIME=\((date +%Y-%m-%d) echo "===备份开始===" echo "备份文件存放于\){BACKUP}/\(DATABASE-\)DATETIME.sql"
数据库地址
HOST=127.0.0.1
数据库用户名
DB_USER=root
数据库密码
DB_PW=JmNetwork@2022
创建备份目录
[ ! -d "\({BACKUP}/\)DATETIME" ] && mkdir -p "\({BACKUP}/\)DATETIME"
后台系统数据库
DATABASE=jm_grid
echo \(DATABASE-\)DATETIME
mysqldump -u\({DB_USER} -p\){DB_PW} --single-transaction --host=$HOST -q -R --databases $DATABASE | gzip > \({BACKUP}/\)DATETIME/$DATABASE.sql
mysqldump -h 127.0.0.1 -u\({DB_USER} -p\){DB_PW} \({DATABASE} >\){BACKUP}/\(DATABASE-\)DATETIME.sql
echo "=导出成功,开始传输="
压缩成tar.gz包
cd $BACKUP
tar -zcvf $DATETIME.tar.gz $DATETIME
备份到服务器B
scp \(DATABASE-\)DATETIME.sql [email protected]:/opt/data/backup
删除备份目录
rm -rf \({BACKUP}/\)DATETIME
删除10天前备份的数据,这边可以自行更改
find $BACKUP -mtime +10 -name "*.sql" -exec rm -rf {} ;
echo "=数据库备份到服务器成功="