Vagrant triggers backup databases

When you running command Vagrant resume, Vagrant halt and vagrant destroy. Then Vagrant triggers backup databases. For example backup Mysql databases for WordPress. Vagrant the essential for DevOps Roles.

Vagrant triggers backup databases

Installing vagrant triggers

$ vagrant plugin install vagrant-triggers

To configure the vagrant triggers database dump

$ vim Vagrantfile

The content as below:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
#config.vm.box = "digitalquery/wpvagrant"
 config.vm.box = "ubuntu/xenial64"
 config.vm.hostname = "wp"
 config.ssh.insert_key = false
 # Vagrant triggers
 #config.trigger.before :destroy, :stdout => true do
 #config.trigger.before :destroy do
 [:resume, :suspend, :halt, :destroy, :reload].each do |cmd|
 config.trigger.before cmd, stdout: true do
 info "Dumping the database before destroying the VM..."
 run_remote "bash /vagrant/mysql/db_dump.sh"
 #run "vagrant ssh -c 'sh /vagrant/wp-vagrant/mysql/db_dump.sh'"
 end
 end
 end

The databases dump script

$ vim /home/huupv/project/mysql/db_dump.sh

The content db_dump script as below:

#!/bin/bash
 echo "db name is wordpress"
 if [ ! -z "wordpress" ]; then
 now=`date +"%Y_%m_%d-%H-%M-%S"`
 db_dump_file=wordpress"_"$now".sql"
 echo "dumping database before destroy"
 echo "dump file: /vagrant/mysql/db_dumps/$db_dump_file"
 if [ ! -d /vagrant/mysql/db_dumps ]; then
 sudo mkdir /vagrant/mysql/db_dumps
 fi
 mysqldump -u root --password=root wordpress > /vagrant/mysql/db_dumps/$db_dump_file
 if [ ! "$?" -eq 0 ]; then
 echo "DATABASE DUMP FAILED - YOU MAY WISH TO ABORT VAGRANT DESTROY."
 echo "Check /vagrant/VAGRANT_ENV/db_dumps/error.log for more info"
 fi
 fi

Conclusion

How to preserve your MySQL Databases Between Destroy and halt. Vagrant triggers are the plugin. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.