dbbackups3 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/dbbackups3.rb +68 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZWQ1MGQ1OGE4MWQ4NjRkMDk2NmFkNjk5MzIyNzBjYzcyNWU2ZTM4OA==
5
+ data.tar.gz: !binary |-
6
+ YTRjOWU2YmM3NDk4ZGU5YWIzZmJiZTMwMzNkMTcyYWJkY2MzN2Y1NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YmI2NTZjN2YwN2U3NTg4NTdhMDAyNDRhZDk3YjhhNTI0ODlhZjFiZTZmNjJj
10
+ OTQ2ZjY4ZTliZjY0MjZmN2Q0NmY5ZTEwMWQ1OWYxMzI3MzM5ZDRlOWYwYmQy
11
+ YzIzNmFlNzcwODE4YmYwYTE5NDc3ZjhlZTY4YTdlOTNkYmRkMTg=
12
+ data.tar.gz: !binary |-
13
+ ZWFhNDdlNTYzNTg4MDQzYWVlNmYxOTk2NmQwYzI2ZGM3YzUwOTIzMGJhOWQz
14
+ NGFhNzRmZjU4ZTAwZDBlMGNjMzY0NjZhZWZmNGEyODcxODAyMWQ3NTNmMDYy
15
+ OWFlOTQyYmQyMTVmY2E2YWRjMzk4YzMzZTI3ODhmNTMwMDZmNTc=
data/lib/dbbackups3.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'aws/s3'
2
+
3
+ class DbBackup
4
+ attr_accessor :username, :password, :backup_path, :db_name, :aws_access_key, :aws_secret_key, :s3_bucket, :s3_region
5
+ attr_writer :host, :port
6
+
7
+ def host
8
+ @host || 'localhost'
9
+ end
10
+
11
+ def port
12
+ @port || 3306
13
+ end
14
+
15
+ def take_local_backup()
16
+ begin
17
+ if @password && @db_name
18
+ if @backup_path.strip.empty?
19
+ system "mkdir -p ~/backup/mysql_backup/#{Time.now.strftime("%d_%m_%Y_%H_%M")}"
20
+ @backup_path = "~/backup/mysql_backup/#{Time.now.strftime("%d_%m_%Y_%H_%M")}/"
21
+ else
22
+ unless File.directory?(@backup_path.strip)
23
+ puts "This path doesn't exist."
24
+ puts "Try to use absolute path."
25
+ return nil
26
+ else
27
+ path = @backup_path.strip
28
+ unless path[-1] == "/"
29
+ path += "/"
30
+ @backup_path = path
31
+ end
32
+ end
33
+ end
34
+ system "mysqldump -P #{@port} -h #{@host} -u #{@username} -p#{@password} --databases #{@db_name} | gzip > #{@backup_path}#{Time.now.strftime("%d_%m_%Y_%H_%M")}_mysql_db.sql.gz"
35
+ puts "Database backed up successfully."
36
+ else
37
+ puts "Some fields are missing. Check all the attributes."
38
+ end
39
+ rescue
40
+ puts "Check the database credentails or the name of the database which you want to backup."
41
+ end
42
+ end
43
+
44
+ def backup_on_s3()
45
+ begin
46
+ if @s3_region && @s3_bucket && @aws_secret_key && @aws_access_key && @db_name
47
+ backup_filename = "#{Time.now.strftime("%d_%m_%Y_%H_%M")}_mysql_db.sql.gz"
48
+ backup_filename_path = "/tmp/#{backup_filename}"
49
+
50
+ system "mysqldump -P #{@port} -h #{@host} -u #{@username} -p#{@password} --databases #{@db_name} | gzip > #{backup_filename_path}"
51
+
52
+ # save to aws-s3
53
+ AWS::S3::DEFAULT_HOST.replace "s3-#{@s3_region}.amazonaws.com"
54
+ AWS::S3::Base.establish_connection!(:access_key_id => @aws_access_key, :secret_access_key => @aws_secret_key)
55
+ AWS::S3::S3Object.store(backup_filename, backup_filename_path, @s3_bucket)
56
+ puts "Database backed up successfully."
57
+ # remove local backup file
58
+ system "rm -f #{backup_filename_path}"
59
+ else
60
+ puts "Some aws credentails are missing."
61
+ end
62
+ rescue
63
+ puts "Check the aws credentails."
64
+ end
65
+ end
66
+
67
+ end
68
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbbackups3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nitesh Mishra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-s3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.3
27
+ description:
28
+ email: nitesh.mishra143@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/dbbackups3.rb
34
+ homepage: http://rubygems.org/gems/dbbackups3
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.6.7
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: This gem helps you to easily create backups of the MYSQL Database and save
58
+ it to the local system or on AWS S3.
59
+ test_files: []