#!/bin/bash
#可修改如下参数
backup_filename=$(date %Y%m%d%H%M%S)
backup_tmp_dir=/data/backup/
ip=10.0.1.182
user=root
passwd=starcor
databse_1=nn_core
databse_2=nn_cms
#备份目录
if [ ! -d ${backup_tmp_dir}/cms/db ]; then
mkdir -p ${backup_tmp_dir}/cms/db
fi
if [ ! -d ${backup_tmp_dir}/core/db ]; then
mkdir -p ${backup_tmp_dir}/core/db
fi
#备份
function data() {
/usr/local/mysql/bin/mysqldump -u$user -p$passwd -h127.0.0.1 $databse_1 --skip-lock-tables > ${backup_tmp_dir}/core/db/db_${databse_1}_${ip}_${backup_filename}.sql
/usr/local/mysql/bin/mysqldump -u$user -p$passwd -h127.0.0.1 $databse_2 --skip-lock-tables > ${backup_tmp_dir}/cms/db/db_${databse_2}_${ip}_${backup_filename}.sql
}
function package(){
#打包文件
cd ${backup_tmp_dir}/core/db
tar zcf db_${databse_1}_${ip}_${backup_filename}.sql.tar.gz db_$databse_1_$ip_${backup_filename}.sql
find ./ -name "*.sql" -exec rm -rf {} ;
cd ${backup_tmp_dir}/cms/db
tar zcf db_${databse_2}_${ip}_${backup_filename}.sql.tar.gz db_$databse_2_$ip_${backup_filename}.sql
find ./ -name "*.sql" -exec rm -rf {} ;
}
data
package
#保存7天备份文件
find /data/db_backup -type f -mtime 7 -exec rm -rf {} ;