Reputation: 99
How to do automatic MySQL Database backup? Is it possible? Can anyone help?
I have a database name 'contacts'.
How can create an automatic backup for it?
Upvotes: 0
Views: 715
Reputation: 425
Guessing that you are using an unix-like OS. You need to do two steps
Backup your current database
First you need to know how to use mysqldump. This should be a good start point.
mysqldump --all-databases --single-transaction --quick --lock-tables=false > full-backup-$(date +%F).sql -u root -p
Cron your mysqldump
You need to create a file with your credentials. Something like this
sudo nano /home/example_user/.mylogin.cnf
[client]
user = root
password = MySQL root user's password
chmod 600 /home/example_user/.mylogin.cnf
Then, you have to add this line to your cron
sudo nano crontab -e
0 1 * * * /usr/bin/mysqldump --defaults-extra-file=/home/example_user/.my.cnf -u root --single-transaction --quick --lock-tables=false --all-databases > full-backup-$(date +\%F).sql
And that's all.
Source: https://www.linode.com/docs/databases/mysql/use-mysqldump-to-back-up-mysql-or-mariadb/
Upvotes: 2