p2b-backup 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de162570feb13aa0e5cc913160a12dceeebee89eaf9892e4730620c0ee9a68a9
4
+ data.tar.gz: 2ddd2e1faba8f6af9b58ef7ce7a604f5ad32834be82b1aa4bacc49583a34019d
5
+ SHA512:
6
+ metadata.gz: 2ef0af368c87a81623e4a67679bd10d89d63b23cc93728b25df2a7b51d3ee073e8caae13f0d1a69dad8d0250c088ece8f45b2be58ac6501daa7eee221e5255b8
7
+ data.tar.gz: b32868e417622690edfdc70178bf9ba388982529b5ded208d5b7c3617c7115222af1f204d93f5d33c97792aceb80864d52606cdae30daeb64f1d41f6815bbad3
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'rubygems'
data/bin/p2bbackup ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'yaml'
4
+ require 'optparse'
5
+ require 'p2b-backup'
6
+
7
+ options = { command: :new_backup }
8
+
9
+ options[:config] = '/etc/.postgresql.backups.yml' if File.exist?('/etc/.postgresql.backups.yml')
10
+
11
+ # Build a parser for the command line arguments
12
+ opts = OptionParser.new do |opts|
13
+ opts.version = '0.0.4'
14
+
15
+ opts.banner = 'Usage: p2bbackup [-flag] [argument]'
16
+ opts.define_head 'p2bbackup: backing up your shit since way back when...'
17
+ opts.separator '*' * 80
18
+
19
+ opts.on('-l', '--list-backup DATABASE', 'List mysql backups for DATABASE') do |db|
20
+ options[:db] = (db || 'all')
21
+ options[:command] = :list
22
+ end
23
+
24
+ opts.on('-n', '--new-backup', 'Create new mysql backup') do
25
+ options[:command] = :new_backup
26
+ end
27
+
28
+ opts.on('-c', '--config CONFIG', 'Use config file.') do |config|
29
+ options[:config] = config
30
+ end
31
+
32
+ opts.on('-d', '--download BACKUP_INDEX',
33
+ 'download the backup specified by index. Run p2bbackup -l to get the index.') do |index|
34
+ options[:command] = :download
35
+ options[:index] = index
36
+ end
37
+
38
+ opts.on('-e', '--engine DATABASE_ENGINE', 'The database engine. ex: mysql, postgresql.') do |engine|
39
+ options[:engine] = engine || if File.exist?('/etc/.mysql.backups.yml')
40
+ 'mysql'
41
+ elsif File.exist?('/etc/.postgresql.backups.yml')
42
+ 'postgresql'
43
+ end
44
+
45
+ options[:config] ||= "/etc/.#{options[:engine]}.backups.yml"
46
+ end
47
+
48
+ opts.on('-r', '--restore BACKUP_INDEX',
49
+ 'Download and apply the backup specified by index WARNING! will overwrite the current db with the backup. Run eybackup -l to get the index.') do |index|
50
+ options[:command] = :restore
51
+ options[:index] = index
52
+ end
53
+ end
54
+
55
+ opts.parse!
56
+ options[:config] ||= (File.exist?('/etc/.postgresql.backups.yml') ? '/etc/.postgresql.backups.yml' : '/etc/.mysql.backups.yml')
57
+ options[:config] = File.expand_path(options[:config])
58
+
59
+ options[:engine] ||= if File.exist?('/etc/.mysql.backups.yml')
60
+ 'mysql'
61
+ elsif File.exist?('/etc/.postgresql.backups.yml')
62
+ 'postgresql'
63
+ end
64
+
65
+ backup_klass = nil
66
+ if File.exist?(options[:config])
67
+ backup_klass = case options[:engine]
68
+ when 'postgresql', 'postgres' then P2bBackup::PostgresqlBackup.new(YAML.load(IO.read(options[:config]),
69
+ symbolize_names: true))
70
+ when 'mysql', NilClass then P2bBackup::MysqlBackup.new(YAML.load(IO.read(options[:config]),
71
+ symbolize_names: true))
72
+ else raise "Invalid engine: #{options[:engine]}"
73
+ end
74
+ end
75
+
76
+ case options[:command]
77
+ when :list
78
+ backup_klass.list options[:db], true
79
+ when :new_backup
80
+ backup_klass.new_backup
81
+ when :download
82
+ backup_klass.download(options[:index])
83
+ when :restore
84
+ backup_klass.restore(options[:index])
85
+ end
86
+ # backup_klass.cleanup
@@ -0,0 +1,52 @@
1
+ # require 'aws/s3'
2
+ require 'date'
3
+ require 'digest'
4
+ require 'net/http'
5
+ require 'tmpdir'
6
+ require 'fileutils'
7
+ require 'p2b-backup/providers/s3'
8
+ require 'p2b-backup/providers/sftp'
9
+
10
+ module P2bBackup
11
+ class Backup
12
+ def initialize(opts = {})
13
+ @dbuser = opts[:dbuser]
14
+ @dbpass = opts[:dbpass]
15
+ @databases = opts[:databases]
16
+ @keep = opts[:keep]
17
+ @tmpname = "#{Time.now.strftime('%Y-%m-%dT%H:%M:%S').gsub(/:/, '-')}.sql.gz"
18
+
19
+ @provider = case opts[:provider]
20
+ when 's3' then P2bBackup::Providers::S3.new(opts[:provider_opts])
21
+ when 'sftp' then P2bBackup::Providers::Sftp.new(opts[:provider_opts])
22
+ else raise "Unknown provider #{opts[:provider]}"
23
+ end
24
+ end
25
+
26
+ def backup_database(database)
27
+ if system(backup_command(database))
28
+ @provider.upload("#{Dir.tmpdir}/#{database}.#{@tmpname}", filename: @tmpname)
29
+ FileUtils.rm "#{Dir.tmpdir}/#{database}.#{@tmpname}"
30
+ puts "successful backup: #{Dir.tmpdir}/#{database}.#{@tmpname}"
31
+ else
32
+ raise "Unable to dump database '#{database}' wtf?"
33
+ end
34
+ end
35
+
36
+ def new_backup
37
+ @databases.each do |db|
38
+ backup_database(db)
39
+ end
40
+ end
41
+
42
+ def normalize_name(obj)
43
+ obj.key.gsub(%r{^.*?/}, '')
44
+ end
45
+
46
+ private
47
+
48
+ def backup_command(*)
49
+ raise NotImplementedError
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+ require 'p2b-backup/providers/s3'
4
+
5
+ module P2bBackup
6
+ class MysqlBackup < Backup
7
+ private
8
+
9
+ def backup_command(database)
10
+ "mysqldump -u #{@dbuser} -p'#{@dbpass}' #{database} | gzip - > #{Dir.tmpdir}/#{database}.#{@tmpname}"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+ require 'p2b-backup/providers/s3'
4
+
5
+ module P2bBackup
6
+ class PostgresqlBackup < Backup
7
+ private
8
+
9
+ def backup_command(database)
10
+ "PGPASSWORD='#{@dbpass}' pg_dump --clean --no-owner --no-privileges -U#{@dbuser} #{database} | gzip - > #{Dir.tmpdir}/#{database}.#{@tmpname}"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ require 'aws-sdk-core'
2
+ require 'aws-sdk-s3'
3
+ module P2bBackup
4
+ module Providers
5
+ class S3
6
+ def initialize(opts = {})
7
+ @client = Aws::S3::Client.new(
8
+ region: opts[:region],
9
+ access_key_id: opts[:access_key_id],
10
+ secret_access_key: opts[:secret_access_key]
11
+ )
12
+ @bucket = opts[:bucket]
13
+ end
14
+
15
+ def upload(file_or_io)
16
+ @client.put_object(
17
+ {
18
+ body: file_or_io,
19
+ bucket: @bucket,
20
+ key: 'HappyFace.jpg'
21
+ }
22
+ )
23
+ puts "Uploaded to #{@bucket}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'net/sftp'
2
+
3
+ module P2bBackup
4
+ module Providers
5
+ class Sftp
6
+ def initialize(opts = {})
7
+ @opts = opts
8
+ end
9
+
10
+ def upload(path, filename: 'file.sql.gz')
11
+ Net::SFTP.start(@opts[:host], @opts[:username], password: @opts[:password]) do |sftp|
12
+ # upload a file or directory to the remote host
13
+ sftp.upload!(path, "#{@opts[:path]}/#{filename}")
14
+ end
15
+ puts "Uploaded to #{@bucket}"
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/p2b-backup.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'p2b-backup/backup'
2
+ require 'p2b-backup/backups/mysql_backup'
3
+ require 'p2b-backup/backups/postgresql_backup'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: p2b-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Frederik Spang Thomsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-s3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-sftp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: hello
56
+ email: fst@place2book.com
57
+ executables:
58
+ - p2bbackup
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Rakefile
63
+ - bin/p2bbackup
64
+ - lib/p2b-backup.rb
65
+ - lib/p2b-backup/backup.rb
66
+ - lib/p2b-backup/backups/mysql_backup.rb
67
+ - lib/p2b-backup/backups/postgresql_backup.rb
68
+ - lib/p2b-backup/providers/s3.rb
69
+ - lib/p2b-backup/providers/sftp.rb
70
+ homepage: http://place2book.com
71
+ licenses: []
72
+ metadata:
73
+ allowed_push_host: https://rubygems.org
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.2.22
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: hello
93
+ test_files: []