1. 备份某个数据库


[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. ##################################################################  

  2. # 备份某个数据库  

  3. ##################################################################  

  4.   

  5. # root 用户,创建备份目录  

  6. mkdir -p /usr/local/cncounter/mysql_dump  

  7. cd /usr/local/cncounter/mysql_dump  

  8.   

  9. # 导出数据库,热备  

  10. mysqldump -u root -pmypasssecret cncounter > cncounter_dump.sql.20140414_1333  


2. 还原某个数据库

[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. ##################################################################  

  2. # 还原某个数据库  

  3. ##################################################################  

  4. # 修改密码  

  5. # mysqladmin -u root password "mypasssecret"  

  6. # mysqladmin -u root password oldpass "mypasssecret"  

  7.   

  8. # 登录  

  9. mysql -u root -pmypasssecret  

  10.   

  11. -- 热备只是备份数据库内部的表,以及数据  

  12. use cncounter;  

  13. source /usr/local/cncounter/mysql_dump/cncounter_dump.sql.20140414_1333;  

  14. exit;  

3. crontab 定时备份

3.1 编辑备份脚本

[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. ##################################################################  

  2. # crontab 定时备份  

  3. ##################################################################  

  4. #   

  5. # root 用户,创建执行脚本  

  6. mkdir -p /root/mysql_dump/data  

  7. cd /root/mysql_dump  

  8. touch mysql_back.sh  

  9. chmod 755 mysql_back.sh  

  10.   

  11. # 编辑备份脚本  

  12. vim mysql_back.sh  

  13.   

  14. ################下面是备份脚本的内容  

  15. #!/bin/sh  

  16. # File: /root/mysql_dump/mysql_back.sh  

  17. # Database info  

  18. DB_NAME="cncounter"  

  19. DB_USER="root"  

  20. DB_PASS="mypasssecret"  

  21. # Others vars  

  22. # whereis mysqldump  

  23. # IS ` but not '  

  24. BIN_DIR="/usr/bin"  

  25. BCK_DIR="/root/mysql_dump/data"  

  26. DATE=`date +%Y%m%d_%H%M%S`  

  27. # TODO  

  28. mkdir -p $BCK_DIR  

  29. $BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME \  

  30.  > $BCK_DIR/$DB_NAME.dump_$DATE.sql  


当然,执行的脚本也可以不用那么灵活: 其中行末尾的 反斜线(\) 的意思是shell 指令换行,如果写在单行内部,就不再需要.


[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. /usr/bin/mysqldump --opt -uroot -pmypasssecret cncounter \  

  2. > /root/mysql_dump/data/cncounter.dump_`date +%Y%m%d_%H%M%S`.sql  

dump出来的sql文件有可能很大,我们也可以开启 gzip 压缩,一般来说可以实现10倍压缩比例:也就是讲输出到文件的内容通过管道操作符让 gzip 程序处理一遍.


[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. /usr/bin/mysqldump --opt -uroot -pmypasssecret cncounter | gzip \  

  2. > /root/mysql_dump/data/cncounter.dump_`date +%Y%m%d_%H%M%S`.sql.gz  



3.2 添加到crontab


[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. # 添加到crontab  

  2. crontab -e  

  3.   

  4. # 添加一行,root用户不需要指定执行的用户名,ESC,wq  

  5. 1 1 * * * /root/mysql_dump/mysql_back.sh  

  6.   

  7. # 不一定需要重启crontab服务  

  8. # service crond restart  

3.3 crontab 简单说明



[plain] view plain copy  Linux下MySQL备份以及crontab定时备份_linux Linux下MySQL备份以及crontab定时备份_linux_02

  1. # cat /etc/crontab   

  2. SHELL=/bin/bash  

  3. PATH=/sbin:/bin:/usr/sbin:/usr/bin  

  4. MAILTO=root  

  5. HOME=/  

  6.   

  7. # For details see man 4 crontabs  

  8.   

  9. # Example of job definition:  

  10. # .---------------- minute (0 - 59)  

  11. # |  .------------- hour (0 - 23)  

  12. # |  |  .---------- day of month (1 - 31)  

  13. # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...  

  14. # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat  

  15. # |  |  |  |  |  

  16. # *  *  *  *  * user-name command to be executed  

可以发现,crontab 的执行周期有5个部分组成,其中第一个是分钟数,第二个是小时数,第三个是一个月中的第几天。。。 如果是 * 就表示每天都会调度。


user-name 部分,如果需要以其他用户调度,则可以指定,否则不能指定,比如 root 用户就不能指定 root,否则有调度日志,但是没有实际执行。