zoukankan      html  css  js  c++  java
  • MySql定时备份脚本

    今天数据库有挂了,真心伤不起!又花了一下午时间学习写了一个定时保存数据的脚本文件,所以记录一下!

    1,每天4点备份mysql数据;

    2,为节省空间,删除超过3个月的所有备份数据;

    3,删除超过7天的备份数据,保留3个月里的 10号 20号 30号的备份数据;

    [root@iZ28c26l6pkZ ~]# cd data

    [root@iZ28c26l6pkZ ~]# cd /data/dbdata

    [root@iZ28c26l6pkZ dbdata]# vim backup_mysq.sh

    #输入脚本文件

    #

    /alidata/server/mysql/bin/mysqldump --all-databases > /data/dbdata/mysqlbak/`date +%Y%m%d`.sql
    find /data/dbdata/mysqlbak/ -mtime +7 -name '*[1-9].sql' -exec rm -rf {} ;
    find /data/dbdata/mysqlbak/ -mtime +92 -name '*.sql' -exec rm -rf {} ;

    #这里原来是这样的!

    ######这个mysql.5.6会报一个错误

    mysqldump -uroot -p123456 --all-databases  > /data/dbdata/mysqlbak/`date +%Y%m%d`.sql
    find /data/dbdata/mysqlbak/ -mtime +7 -name '*[1-9].sql' -exec rm -rf {} ;
    find /data/dbdata/mysqlbak/ -mtime +92 -name '*.sql' -exec rm -rf {} ;

    #######

    测试一下

    [root@iZ28c26l6pkZ dbdata]# ./backup_mysql.sh

    -bash: ./backup_mysql.sh: Permission denied

    ###############原因是#####################

    在官网文档找到了缘由,大家可以点击这里看看:http://dev.mysql.com/doc/refman/5.1/en/password-security-user.html

    MySQL users should use the following guidelines to keep passwords secure.

        When you run a client program to connect to the MySQL server, it is inadvisable to specify your password in a way that exposes it to discovery by other users. The methods you can use to specify your password when you run client programs are listed here, along with an assessment of the risks of each method. In short, the safest methods are to have the client program prompt for the password or to specify the password in a properly protected option file.

    英文有点烂,但大概读懂意思,翻译过来大意是在命令行下如果要使用密码可以在执行命令后的提示输入里输入密码,或者在指定的安全文件内指定密码。那安全文件时哪个呢?文档对此给出了答案:

    Store your password in an option file. For example, on Unix, you can list your password in the [client] section of the .my.cnf file in your home directory:

    可以在my.cnf内指定,于是打开我的my.cnf,在[mysqldump]下增加:

    user=root
    password=root

    然保存 运行我那个就可以了 

    然后再写一个定时

    [root@iZ28c26l6pkZ ~]# crontab -e

    ###

    0 4 * * * /data/dbdata/backup_mysql.sh

    ####

    crontab: installing new crontab

    然后就ok了

     #######mysql5.4遇到微问题##########

    /usr/local/mysql/bin/mysqldump -uroot -pxxxxx --all-databases > all.sql

    mysql用mysqldump导出数据库时提示“-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.”

    这是因为mysqldump默认是不备份事件表的,只有加了--events 才会,解决办法:

    加上--events --ignore-table=mysql.events参数即可;

    /usr/local/mysql/bin/mysqldump -uroot -pxxxxx --events --ignore-table=mysql.events --all-databases > all.sql

  • 相关阅读:
    c#基础问题笔记(一)
    自动化技术中的进给电气传动研习笔记2
    自动化技术中的进给电气传动研习笔记1
    汉字在电脑中是如何存储与编码的呢?
    三十分钟掌握STL
    python练习:函数2
    python练习:函数3
    Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.
    vue 数组对象取对象的属性: Cannot read property 'xxxx' of undefined
    python练习:函数4
  • 原文地址:https://www.cnblogs.com/Minxiaotian/p/5140667.html
Copyright © 2011-2022 走看看