Keep database synced between computers in Trellis/Bedrock/Sage setup

As of version 2.1.0, Vagrant no longer needs the vagrant-triggers plugin - to get the trellis-db-sync working after updating - remove the plugin from vagrant.default.yml and replace the trigger in the Vagrantfile to:

# Vagrant Triggers
  trellis_config.wordpress_sites.each do |(name, site)|
    #
    # Get database credentials
    #
    site['env'].merge!(trellis_config.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] do |trigger|
      database_file = "../database/current/#{db_name}.sql"
      if File.exists?(database_file)
        trigger.info = "Importing database #{db_name}"
        trigger.run_remote = {inline: "mysql -u #{db_user} -p#{db_pass} #{db_name} < /srv/database/current/#{db_name}.sql"}
      else
        fail_with_message "#{database_file} was not found."
      end
    end
    #
    # Exporting database
    #
    config.trigger.before [:halt, :suspend] do |trigger|
      trigger.info = "Dumping database #{db_name}"
      trigger.run_remote = {inline: "mysqldump -u #{db_user} -p#{db_pass} #{db_name} > /srv/database/current/#{db_name}.sql"}
    end
  end

The rest of the setup remains as before.

I had to remove the ~/.vagrant.d directory to delete vagrant-triggers plugin completely - you may want to backup before removing this directory but the user data is automatically regenerated on running vagrant.

The trigger seems to run a little slower than before but at least it works again.

1 Like