Skip to content

Instantly share code, notes, and snippets.

@joshuadegier
Created January 20, 2017 11:29
Show Gist options
  • Save joshuadegier/4773e904bd642698136f4d0fe31e7555 to your computer and use it in GitHub Desktop.
Save joshuadegier/4773e904bd642698136f4d0fe31e7555 to your computer and use it in GitHub Desktop.
Trigger to back-up databases before suspend and/or destroy of Vagrant/Homestead
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'json'
require 'yaml'
VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path(File.join(Dir.home, ".homestead"))
homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"
require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')
Vagrant.require_version '>= 1.8.4'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if File.exist? aliasesPath then
config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"
config.vm.provision "shell" do |s|
s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases"
end
end
if File.exist? homesteadYamlPath then
settings = YAML::load(File.read(homesteadYamlPath))
elsif File.exist? homesteadJsonPath then
settings = JSON.parse(File.read(homesteadJsonPath))
end
Homestead.configure(config, settings)
if File.exist? afterScriptPath then
config.vm.provision "shell", path: afterScriptPath, privileged: false
end
if defined? VagrantPlugins::HostsUpdater
config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
end
config.trigger.before :suspend do
info "Dumping all databases befored suspending the VM..."
run_remote "bash /home/vagrant/Projects/_dbdumps/create-backups.sh"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment