Backup of MySQL database with shell script

Source: Internet
Author: User

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`   #判断现在的备份数量是否大于$numbercount=`ls -l -crt  $backup_dir/*.dupm | awk ‘{print $9 }‘| wc -l`   if$count-gt $numberthen   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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.