1234567891011121314151617181920212223242526272829303132333435 |
#!/bin/bash
#保存备份个数
number=3
#备份保存路径
backup_dir=/root/mysqlbackup
#日期
dd=`
date
+%Y%m%d`
#备份工具
tool=mysqldump
#用户名
username=mracale
#密码
password=456852
#将要备份的数据库
database_name=test
#简单写法 mysqldump -u root -p123456 users > /root/mysqlbackup/users-
$filename
.dump
$tool
-u
$username -p
$password
$database_name
>
$backup_dir
/
$database_name
-
$dd
.dump
#写创建备份日志
echo
"create $backup_dir/$database_name-$dd.dupm"
>>
$backup_dir
/log.txt
#找出需要删除的备份
delfile=`ls -l -crt
$backup_dir
/*.dump | awk
‘{print $9 }‘
| head -1`
#判断现在的备份数量是否大于
$number
count
=`ls -l -crt
$backup_dir
/*.dupm | awk
‘{print $9 }‘
| wc -l`
if
[
$count
-gt
$number
]
then
rm
$delfile
//删除最早生成的备份,只保留number数量的备份
#写删除文件日志
echo
"delete $delfile"
>>
$backup_dir
/log.txt
fi
|
You need to add the following code to the/etc/crontab when timing
5 * * */root/users.sh
The top represents a backup of the database at 5:30 every day.
Backup of MySQL database with shell script