Skip to content

Commit e62f81a

Browse files
author
Bohdan Astapov
committed
Add Export database task
1 parent c408022 commit e62f81a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tasks/export.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"description": "Allows you to backup your database to local file.",
3+
"input_method": "stdin",
4+
"parameters": {
5+
"database": {
6+
"description": "Database to connect to",
7+
"type": "Optional[String[1]]"
8+
},
9+
"user": {
10+
"description": "The user",
11+
"type": "Optional[String[1]]"
12+
},
13+
"password": {
14+
"description": "The password",
15+
"type": "Optional[String[1]]"
16+
},
17+
"sql": {
18+
"description": "Path to file you want backup to",
19+
"type": "String[1]"
20+
}
21+
}
22+
}

tasks/export.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
require 'json'
3+
require 'open3'
4+
require 'puppet'
5+
6+
def get(file, database, user, password)
7+
cmd_string = "mysqldump"
8+
cmd_string << " --databases #{database}" unless database.nil?
9+
cmd_string << " --user=#{user}" unless user.nil?
10+
cmd_string << " --password=#{password}" unless password.nil?
11+
cmd_string << " > #{file}" unless file.nil?
12+
stdout, stderr, status = Open3.capture3(cmd_string)
13+
raise Puppet::Error, _("stderr: '#{stderr}'") if status != 0
14+
{ status: stdout.strip }
15+
end
16+
17+
params = JSON.parse(STDIN.read)
18+
database = params['database']
19+
user = params['user']
20+
password = params['password']
21+
file = params['file']
22+
23+
begin
24+
result = get(file, database, user, password)
25+
puts result.to_json
26+
exit 0
27+
rescue Puppet::Error => e
28+
puts({ status: 'failure', error: e.message }.to_json)
29+
exit 1
30+
end

0 commit comments

Comments
 (0)