From 2d56d4430bfa9ed46a4c1aa0d28a0dd6ffa5f4bb Mon Sep 17 00:00:00 2001 From: dortal94 Date: Sat, 25 Apr 2020 19:14:25 +0100 Subject: [PATCH] Updated Vagrantfile Added triggers to Vagrantfile for up and destroy commands. After vagrant up is executed an attempt to restore db data accures. Before vagrant destroy is executed a save to db data accures. --- Vagrantfile | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Vagrantfile b/Vagrantfile index 61a4b3c..d9056c3 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,4 +1,20 @@ +db_file_exists = "test -f /vagrant/edison/db.sql" +restore_db = "sudo -u postgres psql edison < /vagrant/edison/db.sql" +db_restored_msg = "echo \"Database restored.\"" +db_not_exists_msg = "echo \"db.sql not exists.\"" +try_restore_db = "bash -c '#{db_file_exists} && #{restore_db} && #{db_restored_msg} || #{db_not_exists_msg}'" +save_db_data = "sudo -u postgres pg_dump edison > /vagrant/edison/db.sql" + Vagrant.configure("2") do |config| + config.trigger.before :destroy do |trigger| + trigger.info = "Saving database data inside synced folder..." + trigger.run_remote = {inline: "#{save_db_data}"} + end + + config.trigger.after :up do |trigger| + trigger.info = "Trying to restore database from /vagrant/edison/db.sql..." + trigger.run_remote = {inline: "#{try_restore_db}"} + end config.vm.box = "ubuntu/bionic64" config.vm.provision :shell, path: "setup.sh"