My database is exported correctly and synced in my local directory database/backups!
But I need to insert this command into the run command of the vagrant trigger instead of using the external command (config/homebin/db_backup) like VVV does within a wordpress_sites loop like this:
if Vagrant.has_plugin? 'vagrant-triggers'
config.trigger.before :halt, :stdout => true do
wordpress_sites.each do |(name, site)|
db_name = site['env']['db_name']
db_user = site['env']['db_user']
db_pass = site['env']['db_password']
info "Backing up #{db_name}"
run "vagrant ssh -c echo #{db_name} && mysqldump -u#{db_user} -p#{db_pass} #{db_name} > /srv/database/backups/#{db_name}.sql && echo 'Database #{db_name} backed up';"
end
end
else
puts 'vagrant-triggers missing, please install the plugin:'
puts 'vagrant plugin install vagrant-triggers'
end
But I keep on getting this error:
The command "vagrant ssh mysql -u username -p password
mysqldump -uusername -ppassword example_dev > /srv/database/backups/example_dev.sql" returned a failed exit code. The
error output is shown below:
An invalid option was specified. The help for this command
is available below.
Usage: vagrant ssh [options] [name] [-- extra ssh args]
Options:
-c, --command COMMAND Execute an SSH command directly
-p, --plain Plain mode, leaves authentication up to user
-h, --help Print this help
I already tried it without the -c flag but that doesn’t make a difference.
What exactly is the invalid option?
I’m excited that you’re figuring out cool stuff here. I cheer you on, while also saying that I probably won’t engage on it much more given that it is outside of regular Trellis and not a personal need of my own.
I couldn’t help wondering though, have you already tried vagrant ssh -c 'command in quotes'
When you omit quotes, maybe vagrant is thinking that the parts of the COMMAND are supposed to be options, and they look like invalid options.
# Vagrant Triggers
if Vagrant.has_plugin? 'vagrant-triggers'
# Importing Database
wordpress_sites.each do |(name, site)|
db_name = site['env']['db_name']
db_user = site['env']['db_user']
db_pass = site['env']['db_password']
{
[:up, :resume, :reload] => "vagrant ssh -c 'cd /srv/database/backups/
sql_count=`ls -1 #{db_name}.sql 2>/dev/null | wc -l`
if [ $sql_count != 0 ]
then
echo Database file exists, so start importing
mysql -u #{db_user} -p#{db_pass} #{db_name} < /srv/database/backups/#{db_name}.sql
else
echo Database file does not exist
fi'",
}.each do |command, trigger|
config.trigger.after command, :stdout => true do
info "Importing database #{db_name}"
run "#{trigger}"
end
end
end
# Exporting Database
wordpress_sites.each do |(name, site)|
db_name = site['env']['db_name']
db_user = site['env']['db_user']
db_pass = site['env']['db_password']
{
[:halt, :suspend, :destroy] => "vagrant ssh -c 'mysqldump -u #{db_user} -p#{db_pass} #{db_name} > /srv/database/backups/#{db_name}.sql'",
}.each do |command, trigger|
config.trigger.before command, :stdout => true do
info "Backing up #{db_name}"
run "#{trigger}"
end
end
end
else
puts 'vagrant-triggers missing, please install the plugin:'
puts 'vagrant plugin install vagrant-triggers'
end
On vagrant up, resume and reload my database file gets imported correctly if it exists and on vagrant halt, suspend and destroy it gets exported correctly!
If anyone sees any potential bugs or problems, please let me know!
Thanks for all your help @fullyint!
Oh by the way, in order to get this up and running;
On all the computers you want to sync, your MacOs user id should be the same (and probably your user name too). Here’s how you can change this: https://support.apple.com/en-us/HT201548
Make sure to create a second Administrator user first and log in with that new account first before changing the User id and/or name. After changing the home folder name and id and logging back in, my account was all messed up. I had to change the file permissions on my home folder and reinstall Dropbox, Vagrant and Virtualbox in order to get things working again!
But it was worth it, because it’s working now on 2 computers!
I am a newbie in Vagrant and I have made a script which install LAMP but what I missed is the sync of the database can you please share the entire script on how you sync the database?
hey @Twansparant, this looks fantastic, I’m attempting to see if I can get this rolling as well. It looks like the Trellis Vagrantfile is pretty drastically different. It looks like most of your implementation is just related to Vagrant Triggers right? Ideally I could use the upstream version of that Vagrantfile and install your triggers modification to that. Swapping the entire Vagrantfile leads to some odd changes in the VM.
Yeah and you need an extra synched folder for the database: config.vm.synced_folder "../database/", "/srv/database"[quote=“qbunt, post:30, topic:4478”]
Swapping the entire Vagrantfile leads to some odd changes in the VM.
[/quote]
Hmm that shouldn’t happen, I’m using this exact same Vagrantfile without any problems?
What kind of changes?
@Twansparant the modification actually could have been caused by another dev unbeknownst to me. However, from what I can tell, it looks like this SSH command:
vagrant ssh -c 'cd /srv/database/backups/
sql_count=`ls -1 #{db_name}.sql 2>/dev/null | wc -l`
if [ $sql_count != 0 ]
then
echo Database file exists, so start importing
mysql -u #{db_user} -p#{db_pass} #{db_name} < /srv/database/backups/#{db_name}.sql
else
echo Database file does not exist
fi'"
…is either not getting executed by the vagrant machine or the host machine isn’t sending it through correctly. I’m using zsh as my shell, so that could be adding unnecessary troubles. The reason I asked about the exact modifications is that the upstream Vagrantfile and the one in your Gist have a fair amount of differences seemingly unrelated to migrating databases.
Going to try just running this directly in bash and hopefully remove zsh from the equation, hopefully that helps.
Ok I just tried my custom vagrant-triggers and can confirm it doesn’t work anymore with Trellis 0.9.5 and up.
The problem is, the trigger commands need to read the database credentials in the wordpress_sites loop, but since the db_password variable is moved to the (encrypted) vault.yml, the database dump or import won’t succeed because there is no password set in the command. So I’m trying something like this now:
config_file = File.join(ANSIBLE_PATH, 'group_vars', 'development', 'wordpress_sites.yml')
vault_file = File.join(ANSIBLE_PATH, 'group_vars', 'development', 'vault.yml')
if File.exists?(config_file)
wordpress_sites = YAML.load_file(config_file)['wordpress_sites']
vaults = YAML.load_file(vault_file)['vaults'] --vault-password-file .vault_pass
#vaults = ansible-vault YAML.load_file(vault_file)
fail_with_message "No sites found in #{config_file}." if wordpress_sites.to_h.empty?
else
fail_with_message "#{config_file} was not found. Please set `ANSIBLE_PATH` in your Vagrantfile."
end
# Vagrant Triggers
#
# If the vagrant-triggers plugin is installed, we can run various scripts on Vagrant
# state changes like `vagrant up`, `vagrant halt`, `vagrant suspend`, and `vagrant destroy`
#
# These scripts are run on the host machine, so we use `vagrant ssh` to tunnel back
# into the VM and execute things.
#
if Vagrant.has_plugin? 'vagrant-triggers'
vaults.each_pair do |vault|
#
# Get database password
#
db_pass = vault['env']['db_password']
end
wordpress_sites.each_pair do |site|
#
# Get database credentials
#
db_name = site['env']['db_name']
db_user = site['env']['db_user']
#
# Importing database
#
config.trigger.after [:up, :resume, :reload], :force => true do
info "Importing databases"
run_remote "cd /srv/database/backups/ mysql -u #{db_user} -p#{db_pass} #{db_name} < /srv/database/backups/#{db_name}.sql"
end
#
# Exporting database
#
config.trigger.before [:halt, :suspend, :destroy], :force => true do
info "Dumping databases"
run_remote "mysqldump -u #{db_user} -p#{db_pass} #{db_name} > /srv/database/backups/#{db_name}.sql"
end
end
else
puts 'vagrant-triggers missing, please install the plugin:'
puts 'vagrant plugin install vagrant-triggers'
end
Ok, for now I’m not going to encrypt my groups_vars/development/vault.yml file to be able to read the db_password variables. However, since I’m not a Ruby expert;
At the top of my Vagrantfile I set the vault_wordpress_sites variable like this:
vault_file = File.join(ANSIBLE_PATH, 'group_vars', 'development', 'vault.yml')
if File.exists?(vault_file)
vault_wordpress_sites = YAML.load_file(vault_file)['vault_wordpress_sites']
fail_with_message "No sites found in #{vault_file}." if vault_wordpress_sites.to_h.empty?
else
fail_with_message "#{vault_file} was not found. Please set `ANSIBLE_PATH` in your Vagrantfile."
end
Somehow I need to read the db_password variable from this vault_wordpress_sites array inside the existing wordpress_sites loop, something like this (but with correct syntax):
wordpress_sites.each_pair do |name, site|
#
# Get database credentials
#
db_name = site['env']['db_name']
db_user = site['env']['db_user']
#db_pass = vault_wordpress_sites.#{index}.env.db_password
end
But I have no clue how to approach this… Anyone familiar with Ruby loops?
Thanks!
Yup, that works great, thanks!
My updated triggers look like this now:
vault_file = File.join(ANSIBLE_PATH, 'group_vars', 'development', 'vault.yml')
vault_sites = YAML.load_file(vault_file)['vault_wordpress_sites']
# Vagrant Triggers
#
# If the vagrant-triggers plugin is installed, we can run various scripts on Vagrant
# state changes like `vagrant up`, `vagrant halt`, `vagrant suspend`, and `vagrant destroy`
#
# These scripts are run on the host machine, so we use `vagrant ssh` to tunnel back
# into the VM and execute things.
#
if Vagrant.has_plugin? 'vagrant-triggers'
wordpress_sites.each do |(name, site)|
#
# Get database credentials
#
site['env'].merge!(vault_sites[name]['env'])
db_name = site['env']['db_name']
db_user = site['env']['db_user']
db_pass = site['env']['db_password']
#
# Importing database
#
config.trigger.after [:up, :resume, :reload], :force => true do
info "Importing databases"
run_remote "mysql -u #{db_user} -p#{db_pass} #{db_name} < /srv/database/backups/#{db_name}.sql"
end
#
# Exporting database
#
config.trigger.before [:halt, :suspend, :destroy], :force => true do
info "Dumping databases"
run_remote "mysqldump -u #{db_user} -p#{db_pass} #{db_name} > /srv/database/backups/#{db_name}.sql"
end
end
else
puts 'vagrant-triggers missing, please install the plugin:'
puts 'vagrant plugin install vagrant-triggers'
end