Linux下使用cron实现mysql数据库自动备份|安全的非交互式登录|免密码

文章目录[隐藏]

起因

我们数据库与网站做了站库分离,因此习惯用的宝塔自动备份就不能用了。
传统的mysqldump备份需要使用通过交互式的输入密码进行,而非交互式的方式直接在命令中加上密码非常的不安全!因为只要有人ps aux就能看到你的密码!切记不能在生产环境下这样做!

安全的非交互式备份

其实MySQL官方是有给出方法的,参考这里:https://dev.mysql.com/doc/refman/8.0/en/option-files.html
也就是说在~/.my.cnf中输入以下内容:



[client]

password="MySQL密码"

user=MySQL用户名

即可在mysql命令中直接读取其中的用户名和密码实现非交互式登录,而且非常的安全。不过有时候cron会使用非主目录,因此需要在mysql命令中加上一行参数指定配置文件,这一方法同样适用于指定不同的mysql身份:
--defaults-file=/folder/custom.cnf

代码实操



mysqldump --defaults-file=/root/.my.cnf -h mysql.cloudraft-ha-cluster.com cloudraft_whmcs | gzip > "/www/backup/database/cloudraft-whmcs-"`date +"%Y-%m-%d"`".sql.gz"


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/475.html
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy