Skip to content

Add Export database task #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tasks/export.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"description": "Allows you to backup your database to local file.",
"input_method": "stdin",
"parameters": {
"database": {
"description": "Database to connect to",
"type": "Optional[String[1]]"
},
"user": {
"description": "The user",
"type": "Optional[String[1]]"
},
"password": {
"description": "The password",
"type": "Optional[String[1]]"
},
"sql": {
"description": "Path to file you want backup to",
"type": "String[1]"
}
}
}
30 changes: 30 additions & 0 deletions tasks/export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/opt/puppetlabs/puppet/bin/ruby
require 'json'
require 'open3'
require 'puppet'

def get(file, database, user, password)
cmd_string = "mysqldump"
cmd_string << " --databases #{database}" unless database.nil?
cmd_string << " --user=#{user}" unless user.nil?
cmd_string << " --password=#{password}" unless password.nil?
cmd_string << " > #{file}" unless file.nil?
stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, _("stderr: '#{stderr}'") if status != 0
{ status: stdout.strip }
end

params = JSON.parse(STDIN.read)
database = params['database']
user = params['user']
password = params['password']
file = params['file']

begin
result = get(file, database, user, password)
puts result.to_json
exit 0
rescue Puppet::Error => e
puts({ status: 'failure', error: e.message }.to_json)
exit 1
end