Using puppet to import database on client side

3,052 views
Skip to first unread message

newguy

unread,
Jul 14, 2011, 8:32:38 PM7/14/11
to Puppet Users
Hi guys
I have database dump available on pastebin/exzxzxz

I want the puppet server to download the dump from pastebin and store
it at a fixed location(suppose /home/dump) and then open mysql on the
client side(mysql is installed on the client) and run the import
command so that the downloaded dump is used to make a database on the
client .

Please help guys.

Both client and server are Ubuntu 10.04.

Thanks

newguy

unread,
Jul 15, 2011, 12:10:15 AM7/15/11
to Puppet Users
Guys somebody please help!!!!

Al @ Lab42

unread,
Jul 15, 2011, 2:31:47 AM7/15/11
to puppet...@googlegroups.com
The quick way is to apply a pair of exec resources on the client machine:
- One that downloads the sql file and the other one (that requires the first one) that applies it.
Something like:
    exec {
        "Retrieve $url":
            cwd => "$work_dir",
            command => "wget $url",
            creates => "$work_dir/$sql_filename",
            timeout => 3600,
    }

    exec {
        "Extract $sql_filename":
            command => "mysql $mysql_options < $work_dir/$sql_filename",
            unless => "command that checks if the queryfile has been processed",
            require => Exec["Retrieve $url"],
    }


An alternative approach would be to use puppi to "deploy" the sql file using this define:
http://github.com/example42/puppi/blob/master/manifests/project/mysql.pp
In order to do this you should:
- Include puppi in your modules ( http://github.com/example42/puppi )
- Write a define like for your client node
    puppi::project::mysql { "mysite_sql":
        source           => "http://url_to/mysite.sql",
        mysql_user       => "$mysql_user",
        mysql_host       => "$mysql_host",
        mysql_database   => "$mysql_database",
        mysql_password   => "$mysql_password",
        report_email     => "my@mail",
        enable           => "true",
    }
- MANUALLY TYPE "puppi deploy mysite_sql" on the client node (or trigger it via Puppet with an exec resource like the ones at the beginning).

Note that this Puppi approach might be a bit overkill for your needs: if you have to apply the sql file statements only once the first approach is quicker, if you plan to manage more or less continuous application deployments where developers leave on http://url_to/mysite.sql the changes on the database they require, the puppi approach saves time in the long term (and gives a lot of extra features).

My2c
Al

Marek Dohojda

unread,
Jul 15, 2011, 2:43:29 AM7/15/11
to Puppet Users
Personally I don't think using puppet for this would not be the best
solution. However here is what I would do. Write a shell script to do what
you need, and than use puppet's EXEC statement to launch this command as
needed.

You could also use custom Fact for this.

For instance have you custom script do the export, while within puppet class
you have an "if" that looks if a particular fact is true. if it is set to
true than launch that exec command to do copy and import on your other host.

Does that make sense?

Guys somebody please help!!!!

--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to
puppet-users...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.

newguy

unread,
Jul 15, 2011, 1:03:13 PM7/15/11
to Puppet Users
Yes Marek, it does make sense and thanks for this help, lemme try what
Al @ Lab42 has suggested first as I have been told to do things by
Puppet. In case I fail then I will look in to your suggestion.

Thanks anyways

On Jul 14, 11:43 pm, "Marek Dohojda" <chro...@gmail.com> wrote:
> Personally I don't think using puppet for this would not be the best
> solution.  However here is what I would do.  Write a shell script to do what
> you need, and than use puppet's EXEC statement to launch this command as
> needed.
>
> You could also use custom Fact for this.
>
> For instance have you custom script do the export, while within puppet class
> you have an "if" that looks if a particular fact is true.  if it is set to
> true than launch that exec command to do copy and import on your other host.
>
> Does that make sense?
>
>
>
>
>
>
>
> -----Original Message-----
> From:newguy
> Sent: Thursday, July 14, 2011 10:10 PM
> To: Puppet Users
> Subject: [Puppet Users] Re: Using puppet to import database on client side
>
> Guys somebody please help!!!!
>

newguy

unread,
Jul 15, 2011, 1:37:43 PM7/15/11
to Puppet Users
Hey
Thanks for the help, I was able to download the dump file successfully
but when I try to execute the mysql command I get the following error:

Could not run Puppet configuration client: 'mysql -uroot -pring

parasol < download.php?i=hV0wsTfa' is both unqualifed and specified no

search path at /etc/puppet/environments/abcd/modules/vim/manifests/
init.pp:25



here is my exec where am trying to run mysql command:

exec {"Get db":
command => "mysql -uroot -proot papa < dumpfile.sql",
require => exec["Retrieve dump"],
}
Thanks for your efforts

Nan Liu

unread,
Jul 15, 2011, 1:41:42 PM7/15/11
to puppet...@googlegroups.com
On Fri, Jul 15, 2011 at 10:37 AM, newguy <aimanp...@gmail.com> wrote:
> Hey
> Thanks for the help, I was able to download the dump file successfully
> but when I try to execute the mysql command I get the following error:
>
> Could not run Puppet configuration client: 'mysql -uroot -pring
>
> parasol < download.php?i=hV0wsTfa' is both unqualifed and specified no
>
> search path at /etc/puppet/environments/abcd/modules/vim/manifests/
> init.pp:25
>
>
>
> here is my exec where am trying to run mysql command:
>
> exec {"Get db":
>                command => "mysql -uroot -proot papa < dumpfile.sql",
>                require => exec["Retrieve dump"],
>        }

You need to provide the full path to mysql such as /usr/bin/mysql, or
supply the path.

Nan

Message has been deleted
Message has been deleted

newguy

unread,
Jul 15, 2011, 2:47:44 PM7/15/11
to Puppet Users
I tried the path /usr/bin but it showed the following error:

change from notrun to 0 failed: /usr/bin/mysql -uroot -proot papa <
filedump.sql returned 1 instead of one of [0] at /etc/puppet/ping/
modules/vim/manifests/init.pp:29
exec {"Get db":
command => "/usr/bin/mysql -uroot -proot papa <
dumpfile.sql",
require => exec["Retrieve dump"],
}
Thanks for your efforts
On Jul 15, 10:41 am, Nan Liu <n...@puppetlabs.com> wrote:

Al @ Lab42

unread,
Jul 16, 2011, 12:03:19 AM7/16/11
to puppet...@googlegroups.com
Seems like the command /usr/bin/mysql -uroot -proot papa < filedump.sql  returns an error when executed.
Try to run it directly to understand why and note that you have to eecute it only once and not at every Puppet runs, that is achieved with
refreshonly => true,
or
unless => "a command that checks if the exec has been done and returns true",

Al

Gabriel Filion

unread,
Jul 16, 2011, 4:33:38 AM7/16/11
to puppet...@googlegroups.com, newguy
On 11-07-15 02:47 PM, newguy wrote:
> exec {"Get db":
> command => "/usr/bin/mysql -uroot -proot papa <
> dumpfile.sql",
> require => exec["Retrieve dump"],
> }

if your .sql file name is written in a similar fashion in your manifests
it probably errors out because it doesn't find it. try using an absolute
path to your dump file.

e.g.:

exec {"Get db":
command => "/usr/bin/mysql -uroot -proot papa <

/var/backups/mysql/dumpfile.sql",


require => exec["Retrieve dump"],
}

--
Gabriel Filion

newguy

unread,
Jul 16, 2011, 11:20:34 AM7/16/11
to Puppet Users
It worked thanks forth help guys. Problem was not of dump file its
that on client machine password was misspelt.
Thanks anyways.

On Jul 16, 1:33 am, Gabriel Filion <lelu...@gmail.com> wrote:

Stefan Schulte

unread,
Jul 15, 2011, 2:32:11 PM7/15/11
to Puppet Users
On Fri, Jul 15, 2011 at 11:17:40AM -0700, newguy wrote:
> Yes I did that and got the following error:
> change from notrun to 0 failed: /usr/bin/mysql -uroot -pring parasol <
> download.php?i=hV0wsTfa returned 1 instead of one of [0] at /etc/
> puppet/environments/ss/modules/vim/manifests/init.pp:29
>
> This is my exec:
>
> exec {"Get db":
> command => "/usr/bin/mysql -uroot -pring parasol < download.php?
> i=hV0wsTfa",

> require => exec["Retrieve dump"],
> }
>
>
> Please help

Well it meanst that you command exited with an exit code of 1 instead of
0. So the easiest way to debug this is to run the command by hand in a
shell

# /usr/bin/mysql -uroot -pring parasol < download.php?i=hV0wsTf
# echo $?

Is download.php?i=hV0wsTf really the filename? I would also suggest to
provide the full path to the inputfile because you never know where
someone runs puppet (or provide the cwd parameter to switch to the
desired directory first)

-Stefan

Reply all
Reply to author
Forward
0 new messages