• Home
  • Blog
  • Backup MySQL databases on Vagrant VM automatically to local storage

Backup MySQL databases on Vagrant VM automatically to local storage

Dump/Backup MySQL Databases on Vagrant VM with a Shell Script and Cron

I have a file: /vagrant/mysql_backup.sh and this is run on a cron every 6 hours. You can change that to your own setting or manually execute the file to export them.

#!/bin/sh

DATE=`date +%Y-%m-%d_%H:%M:%S`

cd "/vagrant/backups/"

for i in `mysql -u root -proot -e "show databases;" | grep -Ev "^(Database|mysql|performance_schema|information_schema)$"`; do
        sudo mysqldump -c -u root -proot ${i} > ${i}-$DATE.sql
done

Daily at 12am

@daily /vagrant/mysql_backup.sh >/dev/null

Every 6 hours (12am and 6pm)

0 */6 * * * /vagrant/mysql_backup.sh >/dev/null

You may need to allow execution of the file so chmod +x

chmod +x /vagrant/mysql_backup.sh

About the Author

Ben Sampson

Ben Sampson

Vegan • PHP Developer • PC Gamer • Love to code • Debian / macOS user

Comments