标签:fail code $0 else dbn exit nta error oda
1.MySQL备份脚本[root@localhost ~]# mkdir -p /root/data/backup #备份文件存放于此路径下
[root@localhost ~]# vim /root/mysql.back.sh
#!/bin/bash
backup_path="/root/data/backup"
user="root"
passwd="123456"
dbname="test"
host="localhost"
today=`date +"%Y%m%d-%H%M%S"`
sqlname=$dbname$today.sql
if [ -e /root/data/log_function.sh ]
then
source /root/data/log_function.sh
else
echo -e "\033[41;37m /root/data/log_function.sh is not exist. \033[0m"
exit 1
fi
backup(){
log_correct "开始执行备份脚本,删除15天过期备份"
#backup
mysqldump -h$host -u$user -p$passwd $dbname >$backup_path/$sqlname
}
delete(){
#delete expired 15 days
find /root/data/backup -mtime +15 -type f -name ‘*.sql‘ -exec rm -f {} \;
#-mtime +15 15天以前
}
size(){
cd /root/data/backup
dd=`du -sh $sqlname`
if [ -s ./$sqlname ] ; then
log_correct ‘fsl_prod备份正常‘
log_correct $dd
else
log_error ‘fsl_prod备份失败‘
fi
}
backup
delete
size
[root@localhost ~]# vim /root/data/log_function.sh
#!/bin/bash
#打印备份OK的记录日志输出到日志文件
function log_correct () {
DATE=`date +"%Y%m%d-%H%M%S"` #显示打印日志的时间
USER=$(whoami) #哪个用户在操作
echo "${DATE} ${USER} execute $0 [INFO] $@" >>/root/data/log_info.log #($0脚本本身,$@将参数作为整体传输调用)
}
#log_error打印shell脚本中错误的输出到日志文件
function log_error ()
{
DATE=`date +"%Y%m%d-%H%M%S"`
USER=$(whoami)
echo "${DATE} ${USER} execute $0 [INFO] $@" >>/root/data/log_error.log #($0脚本本身,$@将参数作为整体传输调用)
}
#fn_log函数 通过if判断执行命令的操作是否正确,并打印出相应的操作输出
function fn_log ()
{
if [ $? -eq 0 ]
then
log_correct "$@ sucessed!"
echo -e "\033[32m $@ sucessed. \033[0m"
else
log_error "$@ failed!"
echo -e "\033[41;37m $@ failed. \033[0m"
exit
fi
}
[root@localhost ~]# crontab -e
*/1 * * * * bash /root/mysql.back.sh #每分钟执行一次
根据备份需求,设定备份执行计划
解释如下:
| | | | | |
| | | | | + year [optional]
| | | | +----- day of week (0 - 7) (Sunday=0 or 7)
| | | +---------- month (1 - 12)
| | +--------------- day of month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)
备份成功记录日志:
[root@localhost ~]# tail -f /root/data/log_info.log
20190618-021501 root execute /root/mysql.back.sh [INFO] 开始执行备份脚本,删除15天过期备份
20190618-021501 root execute /root/mysql.back.sh [INFO] fsl_prod备份正常
20190618-021501 root execute /root/mysql.back.sh [INFO] 4.0K test20190618-021501.sql
20190618-021601 root execute /root/mysql.back.sh [INFO] 开始执行备份脚本,删除15天过期备份
20190618-021601 root execute /root/mysql.back.sh [INFO] fsl_prod备份正常
20190618-021601 root execute /root/mysql.back.sh [INFO] 4.0K test20190618-021601.sql
备份文件:
[root@localhost ~]# ll /root/data/backup/
总用量 108
-rw-r--r--. 1 root root 1891 6月 18 01:46 test20190618-014657.sql
-rw-r--r--. 1 root root 1891 6月 18 01:49 test20190618-014916.sql
-rw-r--r--. 1 root root 1891 6月 18 01:52 test20190618-015202.sql
-rw-r--r--. 1 root root 1891 6月 18 01:53 test20190618-015301.sql
-rw-r--r--. 1 root root 1891 6月 18 01:54 test20190618-015401.sql
-rw-r--r--. 1 root root 1891 6月 18 01:55 test20190618-015501.sql
-rw-r--r--. 1 root root 1891 6月 18 01:56 test20190618-015601.sql
-rw-r--r--. 1 root root 1891 6月 18 01:57 test20190618-015701.sql
-rw-r--r--. 1 root root 1891 6月 18 01:58 test20190618-015801.sql
mysql自动备份脚本,MySQLdump+shell+crontab模式(附备份记录日志)
标签:fail code $0 else dbn exit nta error oda
原文地址:https://blog.51cto.com/11573159/2410419