linux mysql 定时备份 使用crontab


第一步:在服务器上配置备份目录代码: 

mkdir /var/lib/mysqlbackup 

cd /var/lib/mysqlbackup 
 

第二步:编写备份脚本代码: 

vi dbbackup.sh 

粘帖以下代码,务必更改其中的username,password和dbname。 

代码: 

#!/bin/sh

mysqldump -udbuser -p(qhtech.CN mzc_release --skip-lock-tables  |  gzip > /var/lib/mysqlbackup/mzc_release`date +%Y-%m-%d_%H%M%S`.sql.gz

第三步:用crontab定时执行备份脚本代码: 

-------------------------------------------------------------------------------- 

crontab -e 

-------------------------------------------------------------------------------- 

若每天晚上21点00备份,添加如下代码, 

代码: 

-------------------------------------------------------------------------------- 

00 21 * * * /var/lib/mysqlbackup/dbbackup.sh


注:

1、任务调度设置文件的写法
      可用crontab -e命令来编辑,编辑的是/var/spool/cron下对应用户的cron文件,也可以直接修改/etc/crontab文件
     具体格式如下:
      Minute Hour Day Month Dayofweek   command
      分钟     小时   天     月       天每星期       命令
     每个字段代表的含义如下:
     Minute             每个小时的第几分钟执行该任务
     Hour               每天的第几个小时执行该任务
     Day                 每月的第几天执行该任务
     Month             每年的第几个月执行该任务
     DayOfWeek     每周的第几天执行该任务
     Command       指定要执行的程序
     在这些字段里,除了“Command”是每次都必须指定的字段以外,其它字段皆为可选

    字段,可视需要决定。对于不指定的字段,要用“*”来填补其位置。
    举例如下:
    5       *       *           *     *     ls             指定每小时的第5分钟执行一次ls命令
    30     5       *           *     *     ls             指定每天的 5:30 执行ls命令
    30     7       8         *     *     ls             指定每月8号的7:30分执行ls命令
    30     5       8         6     *     ls             指定每年的6月8日5:30执行ls命令
    30     6       *           *     0     ls             指定每星期日的6:30执行ls命令[注:0表示星期天,1表示星期1,

    以此类推,也可以用英文来表示,sun表示星期天,mon表示星期一等。]

   30     3     10,20     *     *     ls     每月10号及20号的3:30执行ls命令[注:“,”用来连接多个不连续的时段]

    25     8-11 *           *     *     ls       每天8-11点的第25分钟执行ls命令[注:“-”用来连接连续的时段]

    */15   *       *           *     *     ls         每15分钟执行一次ls命令 [即每个小时的第0 15 30 45 60分钟执行ls命令 ]

     30   6     */10         *     *     ls       每个月中,每隔10天6:30执行一次ls命令[即每月的1、11、21、31日是的6:30执行一次ls 命令。 ]

     每天7:50以root 身份执行/etc/cron.daily目录中的所有可执行文件

     50   7       *             *     *     root     run-parts     /etc/cron.daily   [ 注:run-parts参数表示,执行后面目录中的所有可执行文件。 ]

2、新增调度任务可用两种方法:
       1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。
        2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务。



-----------------------------------------------------
问题一:遇到密码特殊字符

解决:  使用转义或添加''

问题二:Access denied for user when using LOCK TABLES

解决 :
 1. 给该普通用户赋予lock tables 权限,建议是删除该用户,重新用mysql命令建
 
2. 加上--skip-lock-tables即可
 
mysqldump -udbuser -p dbname --skip-lock-tables > dbname.sql
 
3. 使用root 备份

警告:Warning: Using a password on the command line interface can be insecure.

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
文中说的在[client]下面加也可以,但那样就所有块的操作都能共享了,所以生产环境上为了安全还是尽量分开。保存退出再dump就ok了。
 代码如下 复制代码
[root@qttc ~]# /usr/local/mysql/bin/mysqldump db > bak.sql
[root@qttc ~]#


 

原文地址:https://www.cnblogs.com/signheart/p/6595307.html