#!/bin/bash
# 备份文件要保存的目录
basepath=‘/tsmbak/‘
if [ ! -d "$basepath" ]; then
mkdir -p "$basepath"
fi
# 备份数据库生成SQL文件
/bin/nice -n 19 /usr/local/mysql/bin/mysqldump -u root -p密码 --all-databases > $basepath$(date +%Y%m%d).sql
# 将生成的SQL文件压缩
/bin/nice -n 19 tar zPcf $basepath$(date +%Y%m%d).tar.gz $basepath$(date +%Y%m%d).sql
# 删除7天之前的备份数据
find $basepath -mtime +7 -name "*.tar.gz" -exec rm -rf {} ;
# 删除生成的SQL文件
rm -rf $basepath/*.sql
-----操作步骤
1、mkdir /tsmbak/
2、crontab -e
0 2 * * * sh /root/baksql.sh 然后保存wq退出
3、ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/mysqldump
--数据库恢复命令
tar zxPvf XXXXX.tar.gz
mysql -u root -pmima < 20190921.sql
操作截图查看:
修改配置文件:
路径:/etc/my.cnf.d/mysql-clients.cnf
修改参数:
max_connections = 1024
添加参数:
wait_timeout = 31536000
interactive_timeout = 31536000
innodb_buffer_pool_size = 5368709120
--修改8小时自动断开的问题
show variables like ‘%timeout%‘;
set global wait_timeout=31536000
set global interactive_timeout=31536000
set session wait_timeout=31536000
set session interactive_timeout=31536000
--测试Mysql的数据库的压力情况
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb -uroot -ppassword
cd /usr/local/mysql-8.0.17/bin
./mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb -uroot -ppassword
#!/bin/bash
BACKUP_PATH=/opt/backup/<backup_dir>
MYSQL_DUMP=/usr/bin/mysqldump
BACKUP_TIME=$(date +%Y%m%d.%H%M)
BACKUP_DB=<db_name>
DB_USER=<username>
DB_PAWD=<password>
${MYSQL_DUMP} -u${DB_USER} -p${DB_PAWD} ${BACKUP_DB} > ${BACKUP_PATH}/${BACKUP_DB}.sql
if [ $? -ne 0 ];then
echo "${BACKUP_TIME} mysqldump error " >> ${BACKUP_PATH}/dump.log
exit 1
fi
tar -zcvf ${BACKUP_PATH}/${BACKUP_DB}.${BACKUP_TIME}.tar.gz ${BACKUP_PATH}/${BACKUP_DB}.sql
rm -f ${BACKUP_PATH}/${BACKUP_DB}.sql
#删除10天前的文件
find ${BACKUP_PATH} -name "*.tar.gz" -mtime +10 -exec rm -rfv {} ;
exit 0
#!/bin/bash
#功能说明:本功能用于备份mysql数据库
#编写日期:2018/05/17
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin
export PATH
#数据库用户名
dbuser=‘dbuser‘
#数据库密码
dbpasswd=‘dbpasswd‘
#数据库名,可以定义多个数据库,中间以空格隔开,如:test test1 test2
dbname=‘dbname‘
#备份时间
backtime=`date +%Y%m%d%H%M%S`
#日志备份路径
logpath=‘/opt/mysqlbackup/log‘
#数据备份路径
datapath=‘/opt/mysqlbackup‘
#日志记录头部
echo ‘”备份时间为${backtime},备份数据库表 ${dbname} 开始” >> ${logpath}/mysqllog.log
#正式备份数据库
for table in $dbname; do
source=`mysqldump -u${dbuser} -p${dbpasswd} --single-transaction ${table}> ${datapath}/${backtime}.sql` 2>> ${logpath}/mysqllog.log;
#备份成功以下操作
if [ "$?" == 0 ];then
cd $datapath
#为节约硬盘空间,将数据库压缩
tar jcf ${table}${backtime}.tar.bz2 ${backtime}.sql > /dev/null
#删除原始文件,只留压缩后文件
rm -f ${datapath}/${backtime}.sql
echo “数据库表 ${dbname} 备份成功!!” >> ${logpath}/mysqllog.log
else
#备份失败则进行以下操作
echo “数据库表 ${dbname} 备份失败!!” >> ${logpath}/mysqllog.log
fi
done