1

Totally new to shell script's and I have a script that execute the following command in order to export my database from Vagrant:

export_folder="/var/www/projects/DatabaseBackup/local"

schema="wp"

tables="wp_8_options"

export_data="mysqldump -uroot -pmy-secret-password --opt $schema $tables > $file_name.sql"

sudo vagrant ssh --command "cd $export_folder && $export_data"

$export_folder : Is the path inside Vagrant, that corresponding on my host computer folder

$scema : The database to export from Vagrant mySql server

$tables : The tables to export from the selected schema

$export_data : The actual mysqldump command that exporting the data

sudo vagrant ssh --command "..." : The command that instruct the Vagrant to use the SSH in order to execute another command inside the Vagrant VM.

All the code above, run's from a shell script and until now it works properly. The reason I have write this script is to automate the database exporting when ever I commit something on my Git by using the Git hooks.

The problem with the above script, is that I cannot run it via the Git hooks, because it is using sudo.

Unfortunatelly the Vagrant requires sudo on my computer in order to get run. Also, you should know, that I am not a Vagrant expert, and the Vagrant got installed on my computer by another coworker, so I don't know how to modify it in order to run without sudo.

Additionally, I don't really care about this script security because will only run from inside my computer.

So the question is, how to run this script, without using the sudo command. What are the options for that ?

4
  • 1
    Why does Vagrant require sudo? I am using it on Ubuntu and it worked without sudo out of the box. Did you install it in some weird way? Or perhaps initially create the Vagrant VM as root? Commented Dec 23, 2014 at 16:16
  • I don't really know !! :) I have not install it. It got installed by another coworker. I am totally new on Vagrant. The only think that I know is that requires always sudo to run anything. I use sudo for vagrant up, vagrant halt, vagrant ssh, .... Commented Dec 23, 2014 at 16:17
  • Maybe exists a group for using "Vagrant". This is just an hypothesis, I just checked what Vagrant is, looks like a VM (am I right?). Check with cat /etc/group if there's a vagrant group. If so add your user to the group. Commented Dec 23, 2014 at 16:22
  • No there is not exists. The only that exists is vboxusers:x:126: that maybe is for the VirtualBox Commented Dec 23, 2014 at 16:25

1 Answer 1

2

Just give yourself power to sudo vagrant without a password:

Your /etc/sudoers file will have to include the line

#includedir /etc/sudoers.d

you must use the visudo command to edit /etc/sudoers -- do not edit it manually.

Then, sudo visudo -f /etc/sudoers.d/<username> and add to that file

<username> <hostname> = NOPASSWD: /usr/bin/vagrant
Sign up to request clarification or add additional context in comments.

1 Comment

This is a workaround, not a solution. It's probably still a good idea to figure out what's wrong with Vagrant and fix the cause, not the symptom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.