linux常用mysql及日志相关操作
数据库备份脚本
#!/bin/sh /path/to/mysql/bin/mysqldump -h127.0.0.1 -uroot -pYourPassWord databasename | gzip > /path/to/save_file_`date +%Y%m%d`.sql.gz find /path/to/dbbackup/ -mtime +7 -name '*[1-9].sql.gz' -exec rm -rf {} \; find/path/to/dbbackup/ -mtime +92 -name '*.sql.gz' -exec rm -rf {} \;
crontab
0 6 * * * /path/to/crontabs/rmlogs.sh 30 6 * * * /path/to/crontabs/dbback.sh */5 * * * * /path/to/listenmysql.sh
监听mysql挂掉后重启
#!/bin/bash pgrep mysqld &> /dev/null if [ $? -gt 0 ] then echo "`date` mysql is stop" >> /alidata/crontab/mysqlrestart.log /etc/init.d/mysqld start else echo "`date` mysql running" >> /alidata/crontab/mysqlrestart.log fi
自动删除30天前的日志
#!/bin/sh find /path/tolog/nginx/ -mtime +30 -name "*.log" -exec rm -rf {} \;