Linux下实现MySQL数据库自动备份的实例详解

1.给mysql创建用户备份的角色,并且授予角色select, reload, show databases, lock tables等权限。

mysql> create user 'backuper'@'localhost' identified by '********';  Query OK, 0 rows affected (0.00 sec)    mysql> grant SELECT, RELOAD, SHOW DATABASES, LOCK TABLES on *.* to backuper@localhost;  Query OK, 0 rows affected (0.00 sec)

2.在系统中找到存储空间比较大的硬盘创建备份目录,并且创建shell脚本

[root@qxyw backup]# vim backup_qianyu_veeker_db.sh    #!/bin/bash  mysqldump -ubackuper -p******** qianyu_veeker_db > /home/mysql/backup/qianyu_veeker_db_$(date +%Y%m%d_%H%M%S).sql

注意:-u和用户名中间是没有空格的,-p和密码也是这样的。

3.添加计划任务,需要安装crontab
vixie-cron 软件包是 cron 的主程序;
crontabs 软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。

[root@qxyw ~]# yum -y install vixie-cron  [root@qxyw ~]# yum -y install crontabs

4.设置开机启动

[root@qxyw ~]# chkconfig --level 345 crond on

5.修改/etc/crontab文件,添加需要周期执行的任务

[root@qxyw etc]# vim crontab    SHELL=/bin/bash  PATH=/sbin:/bin:/usr/sbin:/usr/bin  MAILTO=root  HOME=/# For details see man 4 crontabs    # Example of job definition:  # .---------------- minute (0 - 59)  # |  .------------- hour (0 - 23)  # |  |  .---------- day of month (1 - 31)  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat  # |  |  |  |  |# *  *  *  *  * user-name command to be executed0 0 * * * /home/mysql/backup/backup_qianyu_veeker_db.sh

6.启动crontab服务

[root@qxyw etc]# service crond start  Starting crond: [  OK  ]

 ,

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享