shell脚本实现mysql数据库备份

转载自:


#!/bin/bash

#保存备份数量
number = 3

#备份保存路径 
backup_dir = /root/mysqlBackup

#日期
dd = `date +%Y%m%d`

#备份工具
tool = mysqldump

#用户名
username = root

#密码
password = 123456

#将要备份的数据库
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.dump" >> $backup_dir/log.txt

# 需要删除的备份
delfile=`ls -l -crt $backup_dir/*.dump | awk '{print $9}' | head -1`

# 判断现在的备份数量是否大于number
count=`ls -l -crt  $backup_dir/*.dump | awk '{print $9}' | wc -l`

if [ $count -gt $number ]
then
  rm $delfile  // 删除最早生成的备份,只保留number数量的备份
  # 写删除文件日志
  echo "delete $delfile" >> $backup_dir/log.txt
fi
添加新评论