snapshot_archive 0.1.0

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: 4da07dc7efc8fc705adf5c30458fc478990ce8becbb21341929532726bfcf866
4
+ data.tar.gz: 55ab36863f972707168fd6a9f4066dae5f63502a29f88d8ab7e0fc3ca14eb905
5
+ SHA512:
6
+ metadata.gz: 8758443c01473fa6111e37cba9cc4963e9e68470d4b8a352c9df11a4d0e36254cd65ec7b6fe3415ba471b2554da9384211734096fb704d27a25095714bd4611f
7
+ data.tar.gz: 79aca3b6538fe7e57ff2147fbcb0bd734d28ff3bb1b2e4f879c11e8e532f62cf3d8bdc7ac5f3d3f299dc8bd6a7de4c7fff5abd4c1878c9d475ead5cab227c824
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.5
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at pete.kinnecom@appfolio.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in snapshot_archive.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snapshot_archive (0.1.0)
5
+ commander (~> 4.5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ commander (4.6.0)
12
+ highline (~> 2.0.0)
13
+ highline (2.0.3)
14
+ parallel (1.22.1)
15
+ parser (3.1.1.0)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.1.1)
18
+ rake (13.0.6)
19
+ regexp_parser (2.3.0)
20
+ rexml (3.2.5)
21
+ rubocop (1.27.0)
22
+ parallel (~> 1.10)
23
+ parser (>= 3.1.0.0)
24
+ rainbow (>= 2.2.2, < 4.0)
25
+ regexp_parser (>= 1.8, < 3.0)
26
+ rexml
27
+ rubocop-ast (>= 1.16.0, < 2.0)
28
+ ruby-progressbar (~> 1.7)
29
+ unicode-display_width (>= 1.4.0, < 3.0)
30
+ rubocop-ast (1.16.0)
31
+ parser (>= 3.1.1.0)
32
+ ruby-progressbar (1.11.0)
33
+ unicode-display_width (2.1.0)
34
+
35
+ PLATFORMS
36
+ x86_64-darwin-19
37
+ x86_64-darwin-20
38
+
39
+ DEPENDENCIES
40
+ rake (~> 13.0)
41
+ rubocop (~> 1.7)
42
+ snapshot_archive!
43
+
44
+ BUNDLED WITH
45
+ 2.2.17
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Pete Kinnecom
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,156 @@
1
+ # SnapshotArchive
2
+
3
+ Save and restore snapshots of stateful services to a central archive. Have a
4
+ development database that you want to restore to a prior state? Just take a
5
+ snapshot and restore it later! Build your own snapshotting behavior in a
6
+ configuration file so that you can snapshot any stateful service you use during
7
+ development.
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ gem install snapshot_archive
13
+ ```
14
+
15
+ ## Example
16
+
17
+ Snapshot and restore mysql databases named `db_1` and `db_2`:
18
+
19
+ ```bash
20
+ $ snap backup mysql:db_1,db_2 -m "snapshot description"
21
+ #=> Saved snapshot: a4758dbe-6f26-4365-9d3a-6962a96557b5
22
+
23
+ $ snap show a4758dbe-6f26-4365-9d3a-6962a96557b5
24
+ #=> id: a4758dbe-6f26-4365-9d3a-6962a96557b5
25
+ #=> time: 2022-04-15 22:30
26
+ #=> ----------------------
27
+ #=> snapshot description
28
+
29
+ $ snap restore a4758dbe-6f26-4365-9d3a-6962a96557b5
30
+ #=> Restored snapshot: a4758dbe-6f26-4365-9d3a-6962a96557b5
31
+ ```
32
+
33
+ ## Backup
34
+
35
+ Save a snapshot that can be restored later.
36
+
37
+ By default, will use the active stores that have been configured. The
38
+ stores used for the snapshot can be overridden by passing them as args
39
+ to the command. Each store can also be passed arguments for
40
+ snapshotting from the CLI. Examples below:
41
+
42
+ See the "Configure" section for how add a custom store.
43
+
44
+ ```bash
45
+ # Uses your EDITOR to add details for the snapshot.
46
+ # Uses the default configured "stores" for creating the snapshot
47
+ snap backup
48
+
49
+ # Add a message from the command line:
50
+ snap backup --message "Add message from command line"
51
+ snap backup -m "Add message from command line"
52
+
53
+ # Override which stores are used for the snapshot
54
+ snap backup store_1 store_2
55
+
56
+ # Pass arguments to a store
57
+ snap backup mysql:db_1,db_2
58
+ ```
59
+ ## Show
60
+
61
+ ```bash
62
+ # Show the full details for a given snapshot
63
+ snap show {id}
64
+ ```
65
+
66
+ ## Restore
67
+
68
+ ```bash
69
+ # Restore the given snapshot
70
+ snap restore {id}
71
+ ```
72
+
73
+ ## List
74
+
75
+ ```bash
76
+ # list all snapshots (pipe to `less` if you like)
77
+ snap list
78
+ ```
79
+
80
+ ## Delete
81
+
82
+ ```bash
83
+ # Delete the given snapshot
84
+ snap rm {id}
85
+ ```
86
+
87
+ ## Configure
88
+
89
+ A custom configuration can be written at the location `$HOME/.config/snapshot_archive.rb`. It is written in ruby. Here is an example config:
90
+
91
+ ```ruby
92
+
93
+ SnapshotArchive.configure do |config|
94
+
95
+ # Configure where the snapshots are stored. Defaults to `$HOME/.snapshot_archive`
96
+ config.storage_path = "/path/to/snapshot_dir"
97
+
98
+ # Configure which stores are active by default (can be overridden with --store
99
+ # option on command line). The default store is "mysql_rails" which saves a
100
+ # snapshot of all databases for the Rails app found in the CWD.
101
+ config.active_stores = ["store_1", "store_2"]
102
+
103
+ # Creating a custom store:
104
+ #
105
+ # Stores must implement two methods:
106
+ #
107
+ # backup: accepts a directory for data and returns a JSON hash (with string
108
+ # keys) for use in restoring.
109
+ #
110
+ # restore: accepts the metadata JSON data created when making the backup. No
111
+ # return value.
112
+ #
113
+ # Stores can either be defined as objects/classes/modules or a builtin store
114
+ # builder can be used.
115
+
116
+ # Using an object:
117
+
118
+ class MyCustomStore
119
+ def backup(dir, args=[])
120
+ path = File.join(dir, "my_custom_store.txt")
121
+ File.write(path, "received args: #{args}")
122
+
123
+ { "path" => path }
124
+ end
125
+
126
+ def restore(metadata)
127
+ puts(File.read(metadata.fetch("path")))
128
+ end
129
+ end
130
+
131
+ config.register_store("my_custom_store", MyCustomStore.new)
132
+
133
+
134
+ # Using the store builder:
135
+
136
+ config.register_store("my_customer_store") do |store|
137
+ store.backup do |dir, args=[]|
138
+ path = File.join(dir, "my_custom_store.txt")
139
+ File.write(path, "received args: #{args}")
140
+
141
+ { "path" => path }
142
+ end
143
+
144
+ store.restore do |metadata|
145
+ puts(File.read(metadata.fetch("path")))
146
+ end
147
+ end
148
+
149
+ # Making an alias for a store with arguments. This makes the following
150
+ # invocations the same:
151
+ # snap backup my_backup
152
+ # snap backup mysql:db_1,db_2
153
+
154
+ config.register_store("my_backup", "mysql:db_1,db_2")
155
+ end
156
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "snapshot_archive"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/snap ADDED
@@ -0,0 +1,131 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ if $PROGRAM_NAME == __FILE__
6
+ require "bundler/setup"
7
+ end
8
+
9
+ require "commander"
10
+ require "snapshot_archive"
11
+
12
+ module SnapshotArchive
13
+ class CommanderCli
14
+ include Commander::Methods
15
+
16
+ def run
17
+ SnapshotArchive::Cfg.load
18
+
19
+ program(:name, "SnapshotArchive")
20
+ program(:version, SnapshotArchive::VERSION)
21
+ program(:description, SnapshotArchive::DESCRIPTION)
22
+ global_option('--verbose') { Cfg.instance.shell = Shell.new(verbose: true) }
23
+
24
+ command(:backup) do |c|
25
+ c.syntax = "snap backup [store(s)]"
26
+ c.description = <<~TXT
27
+ Save a snapshot that can be restored later.
28
+
29
+ By default, will use the active stores that have been configured. The
30
+ stores used for the snapshot can be overridden by passing them as args
31
+ to the command. Each store can also be passed arguments for
32
+ snapshotting from the CLI. Examples below:
33
+
34
+ # Uses your EDITOR to add details for the snapshot.
35
+ # Uses the default configured "stores" for creating the snapshot
36
+ snap backup
37
+
38
+ # Add a message from the command line:
39
+ snap backup --message "Add message from command line"
40
+ snap backup -m "Add message from command line"
41
+
42
+ # Override which stores are used for the snapshot
43
+ snap backup store_1 store_2
44
+
45
+ # Pass arguments to a store
46
+ snap backup mysql:db_1,db_2
47
+ TXT
48
+
49
+ c.option "-m MESSAGE", "--message MESSAGE", String, "Description"
50
+
51
+ c.action do |args, options|
52
+ message = (
53
+ if options.message
54
+ options.message.strip
55
+ else
56
+ ask_editor(File.read(File.join(__dir__, "../lib/template.txt")))
57
+ .split("\n")
58
+ .reject { |l| l.match(/\A#|\A\n/) }
59
+ .join("\n")
60
+ .strip
61
+ end
62
+ )
63
+
64
+ stores = (
65
+ if args.count > 0
66
+ args.map { |arg| SnapshotArchive::Cfg.parse_store(arg) }.to_h
67
+ else
68
+ SnapshotArchive::Cfg.stores
69
+ end
70
+ )
71
+
72
+ SnapshotArchive::Cli.backup(
73
+ msg: message,
74
+ stores: stores
75
+ )
76
+ end
77
+
78
+ end
79
+
80
+ command(:restore) do |c|
81
+ c.syntax = "snap restore ID"
82
+ c.description = "Restore the state of the databases."
83
+
84
+ c.action do |args, _options|
85
+ raise "exactly one ID required" unless args.count == 1
86
+
87
+ SnapshotArchive::Cli.restore(
88
+ id: args.first
89
+ )
90
+ end
91
+ end
92
+
93
+ command(:list) do |c|
94
+ c.syntax = "snap list"
95
+ c.description = "List available snapshots"
96
+
97
+ c.action do |_args, options|
98
+ SnapshotArchive::Cli.list
99
+ end
100
+ end
101
+
102
+ command(:show) do |c|
103
+ c.syntax = "snap show"
104
+ c.description = "Show full message for snapshot"
105
+
106
+ c.action do |args, options|
107
+ raise "exactly one id is required" unless args.length == 1
108
+
109
+ SnapshotArchive::Cli.show(args.first)
110
+ end
111
+ end
112
+
113
+ command(:rm) do |c|
114
+ c.syntax = "snap rm"
115
+ c.description = "Delete an snapshot"
116
+
117
+ c.action do |args, options|
118
+ raise "exactly one id is required" unless args.length == 1
119
+
120
+ SnapshotArchive::Cli.delete(args.first)
121
+ end
122
+ end
123
+
124
+ default_command(:list)
125
+
126
+ run!
127
+ end
128
+ end
129
+ end
130
+
131
+ SnapshotArchive::CommanderCli.new.run
@@ -0,0 +1,56 @@
1
+ module SnapshotArchive
2
+ module Archives
3
+ class Builder
4
+ def self.call(dir:, stores:)
5
+ new(dir, stores).call
6
+ end
7
+
8
+ attr_reader :dir, :stores
9
+ def initialize(dir, stores)
10
+ @dir = dir
11
+ @stores = stores
12
+ end
13
+
14
+ def call
15
+ stores_metadata = (
16
+ stores
17
+ .map { |name, store| store.backup(dir)&.merge(type: name) }
18
+ .compact
19
+ )
20
+
21
+ {
22
+ stores: stores_metadata
23
+ }
24
+ end
25
+ end
26
+
27
+ class Restore
28
+ def self.call(metadata)
29
+ new(metadata).call
30
+ end
31
+
32
+ attr_reader :metadata
33
+ def initialize(metadata)
34
+ @metadata = metadata
35
+ end
36
+
37
+ def call
38
+ metadata.fetch("stores").each do |store_metadata|
39
+ store = Cfg.instance.store(store_metadata.fetch("type"))
40
+ store.restore(store_metadata)
41
+ end
42
+ end
43
+ end
44
+
45
+ class Presenter
46
+ def self.call(metadata)
47
+ new(metadata).call
48
+ end
49
+
50
+ attr_reader :metadata
51
+ def initialize(metadata)
52
+ @metadata = metadata
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,134 @@
1
+ require "snapshot_archive/shell"
2
+ require "snapshot_archive/repositories"
3
+
4
+ module SnapshotArchive
5
+ class StoreBuilder
6
+ class CustomStore
7
+ def initialize(backup:, restore:)
8
+ @backup = backup
9
+ @restore = restore
10
+ end
11
+
12
+ def backup(dir)
13
+ @backup.call(dir)
14
+ end
15
+
16
+ def restore(metadata)
17
+ @restore.call(metadata)
18
+ end
19
+ end
20
+
21
+ def backup(&block)
22
+ @backup = block
23
+ end
24
+
25
+ def restore(&block)
26
+ @restore = block
27
+ end
28
+
29
+ def to_store
30
+ CustomStore.new(backup: @backup, restore: @restore)
31
+ end
32
+ end
33
+
34
+ class Cfg
35
+ class << self
36
+ def instance
37
+ @instance ||= Cfg.new
38
+ end
39
+
40
+ [
41
+ :bind_backup,
42
+ :load,
43
+ :parse_store,
44
+ :pwd,
45
+ :repository,
46
+ :shell,
47
+ :stores,
48
+ ].each do |meth|
49
+ define_method(meth) do |*args|
50
+ instance.public_send(meth, *args)
51
+ end
52
+ end
53
+ end
54
+
55
+ attr_accessor :pwd, :storage_path, :active_stores, :shell
56
+ def initialize
57
+ self.active_stores = []
58
+ end
59
+
60
+ def load(path = ".config/snapshot_archive.rb")
61
+ config_path = File.join(ENV["HOME"], path)
62
+
63
+ require("snapshot_archive/default_configuration")
64
+ Kernel.load(config_path) if File.exist?(config_path)
65
+ end
66
+
67
+ def register_store(name, klass_or_alias=nil, active_by_default: true, &block)
68
+ store = (
69
+ if klass_or_alias.is_a?(String)
70
+ parse_store(klass_or_alias)[1]
71
+ elsif klass_or_alias
72
+ klass_or_alias
73
+ else
74
+ builder = StoreBuilder.new
75
+ yield(builder)
76
+ builder.to_store
77
+ end
78
+ )
79
+
80
+ store_registry[name] = store
81
+ if active_by_default
82
+ active_stores << name
83
+ end
84
+ end
85
+
86
+ def repository
87
+ Repositories::FileSystem.new(path: storage_path)
88
+ end
89
+
90
+ def parse_store(str)
91
+ name, *store_args = str.split(/[:,]/)
92
+
93
+ store = (
94
+ if store_args.count > 0
95
+ bind_backup(name, store_args)
96
+ else
97
+ store_registry.fetch(name)
98
+ end
99
+ )
100
+
101
+ [name, store]
102
+ rescue KeyError
103
+ raise "Store not found: '#{str}'"
104
+ end
105
+
106
+ def bind_backup(name, args)
107
+ SnapshotArchive::Stores::BoundBackup.new(store_registry.fetch(name), args)
108
+ end
109
+
110
+ def store(name)
111
+ store_registry.fetch(name)
112
+ end
113
+
114
+ def stores
115
+ unknown_keys = active_stores - store_registry.keys
116
+
117
+ if !unknown_keys.empty?
118
+ raise ArgumentError.new("invalid store(s): #{unknown_keys.join(",")}")
119
+ end
120
+
121
+ store_registry.slice(*active_stores)
122
+ end
123
+
124
+ def shell
125
+ @shell ||= Shell.new
126
+ end
127
+
128
+ private
129
+
130
+ def store_registry
131
+ @store_registry ||= {}
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,38 @@
1
+ require "ostruct"
2
+ require "time"
3
+ require "json"
4
+ require "securerandom"
5
+
6
+ require "snapshot_archive/stores"
7
+ require "snapshot_archive/archives"
8
+ require "snapshot_archive/formatters"
9
+
10
+ module SnapshotArchive
11
+ class Cli
12
+ class << self
13
+ def backup(msg:, stores:)
14
+ if msg.empty?
15
+ Cfg.shell.warn("aborting due to empty message")
16
+ else
17
+ Cfg.repository.add(msg: msg, stores: stores)
18
+ end
19
+ end
20
+
21
+ def restore(id:)
22
+ Cfg.repository.restore(id)
23
+ end
24
+
25
+ def list
26
+ Cfg.repository.list
27
+ end
28
+
29
+ def show(id)
30
+ Cfg.repository.show(id)
31
+ end
32
+
33
+ def delete(id)
34
+ Cfg.repository.delete(id)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ SnapshotArchive.configure do |config|
2
+ config.register_store(
3
+ "mysql",
4
+ SnapshotArchive::Stores::Mysql,
5
+ active_by_default: false
6
+ )
7
+
8
+ config.register_store("mysql_rails") do |store|
9
+ store.backup do |dir, _args|
10
+ existing_databases = (
11
+ config
12
+ .shell
13
+ .run("mysql --execute 'show databases' --silent --skip-column-names")
14
+ )
15
+
16
+ db_names = (
17
+ config
18
+ .shell
19
+ .run("sh -c 'cd $(git rev-parse --show-toplevel) && git grep database:.*_development *database.yml || exit 0'")
20
+ .map { |line| line.match(/database:\s+(.*_development)\s*$/) }
21
+ .compact
22
+ .map { |match| match[1] }
23
+ .select { |db| existing_databases.include?(db) }
24
+ .reject { |db| db.match(/_tmp_/)}
25
+ )
26
+
27
+ next if db_names.empty?
28
+
29
+ Cfg.bind_backup("mysql", db_names).backup(dir)
30
+ end
31
+
32
+ store.restore do |metadata|
33
+ Cfg.store("mysql").restore(metadata)
34
+ end
35
+ end
36
+
37
+ config.storage_path = File.join(ENV["HOME"], ".snapshot_archive")
38
+ end
@@ -0,0 +1,49 @@
1
+ module SnapshotArchive
2
+ module Formatters
3
+ class List
4
+ def self.call(metadata)
5
+ new(metadata).call
6
+ end
7
+
8
+ attr_reader :metadata
9
+ def initialize(metadata)
10
+ @metadata = metadata
11
+ end
12
+
13
+ def call
14
+ id = metadata.fetch("id")
15
+ timestamp = Time.parse(metadata.fetch("timestamp")).strftime("%F %H:%M")
16
+ message = metadata.fetch("message")
17
+
18
+ short_message = message.split("\n").first
19
+ Cfg.shell.puts([id, timestamp, short_message].join(" : "))
20
+ end
21
+ end
22
+
23
+ class Show
24
+ def self.call(metadata)
25
+ new(metadata).call
26
+ end
27
+
28
+ attr_reader :metadata
29
+ def initialize(metadata)
30
+ @metadata = metadata
31
+ end
32
+
33
+ def call
34
+ id = metadata.fetch("id")
35
+ timestamp = Time.parse(metadata.fetch("timestamp")).strftime("%F %H:%M")
36
+ message = metadata.fetch("message")
37
+ dir = metadata.fetch("dir")
38
+
39
+ Cfg.shell.puts(<<~TXT)
40
+ id: #{id}
41
+ time: #{timestamp}
42
+ path: #{dir}
43
+ ----------------------
44
+ #{message}
45
+ TXT
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,90 @@
1
+ module SnapshotArchive
2
+ module Repositories
3
+ class FileSystem
4
+ SCHEMA = "0"
5
+ attr_reader :path
6
+ def initialize(path:)
7
+ @path = path
8
+ end
9
+
10
+ def add(msg:, stores:)
11
+ id = SecureRandom.uuid
12
+ timestamp = Time.now
13
+ dir = mkdir(id)
14
+
15
+ archive_metadata = Archives::Builder.call(dir: dir, stores: stores)
16
+
17
+ Cfg.shell.debug("Using stores: #{stores.keys.join(", ")}")
18
+
19
+ if archive_metadata.fetch(:stores).empty?
20
+ Cfg.shell.warn("no data to save")
21
+ else
22
+ metadata = {
23
+ __schema__: SCHEMA,
24
+ id: id,
25
+ message: msg,
26
+ timestamp: timestamp.utc.iso8601,
27
+ dir: dir,
28
+ archive: archive_metadata
29
+ }
30
+
31
+ Cfg.shell.debug("writing metadata: #{metadata.to_json}")
32
+ Cfg.shell.info("Saved snapshot: #{id}")
33
+ File.write(File.join(dir, "metadata.json"), JSON.pretty_generate(metadata))
34
+ end
35
+ end
36
+
37
+ def restore(id)
38
+ metadata = JSON.parse(File.read(File.join(path, id, "metadata.json")))
39
+
40
+ Archives::Restore.call(metadata.dig("archive"))
41
+ Cfg.shell.info("Restored snapshot: #{id}")
42
+ end
43
+
44
+ def list
45
+ snapshots = (
46
+ Dir
47
+ .glob(File.join(path, "**/metadata.json"))
48
+ .map { |metadata| JSON.parse(File.read(metadata)) }
49
+ .sort_by { |metadata| metadata.fetch("timestamp") }
50
+ .reverse
51
+ )
52
+
53
+ if snapshots.count > 0
54
+ snapshots.each do |metadata|
55
+ Formatters::List.call(metadata)
56
+ end
57
+ else
58
+ Cfg.shell.info("No snapshots in archive")
59
+ end
60
+ end
61
+
62
+ def show(id)
63
+ Formatters::Show.call(
64
+ JSON.parse(File.read(File.join(path, id, "metadata.json")))
65
+ )
66
+ end
67
+
68
+ def delete(id)
69
+ dir = File.join(path, id)
70
+
71
+ raise ArgumentError.new("unknown snapshot: #{id}") unless File.exist?(dir)
72
+
73
+ FileUtils.rm_rf(dir)
74
+ Cfg.shell.info("Removed snapshot: #{id}")
75
+ end
76
+
77
+ private
78
+
79
+ def timestamp
80
+ @timestamp ||= Time.now
81
+ end
82
+
83
+ def mkdir(id)
84
+ dir = File.join(path, id)
85
+ FileUtils.mkdir_p(dir)
86
+ dir
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,53 @@
1
+ module SnapshotArchive
2
+ class Shell
3
+ CODES = {
4
+ red: 31,
5
+ green: 32,
6
+ yellow: 33,
7
+ pink: 35,
8
+ }.freeze
9
+
10
+ CommandFailureError = Class.new(StandardError)
11
+
12
+ def initialize(verbose: false)
13
+ @verbose = verbose
14
+ end
15
+
16
+ def run(cmd)
17
+ debug(cmd)
18
+
19
+ `#{cmd}`
20
+ .split("\n")
21
+ .tap { raise CommandFailureError.new(cmd) unless $? == 0 }
22
+ end
23
+
24
+ def warn(msg)
25
+ print "#{colorize(msg, :red)}\n"
26
+ end
27
+
28
+ def notify(msg)
29
+ print "#{colorize(msg, :yellow)}\n"
30
+ end
31
+
32
+ def puts(msg)
33
+ print "#{msg}\n"
34
+ end
35
+
36
+ def info(msg)
37
+ puts(msg)
38
+ end
39
+
40
+ def debug(msg)
41
+ puts(msg) if @verbose
42
+ end
43
+
44
+ private
45
+
46
+ # colorization
47
+ def colorize(msg, color)
48
+ color_code = CODES.fetch(color)
49
+
50
+ "\e[#{color_code}m#{msg}\e[0m"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,81 @@
1
+ module SnapshotArchive
2
+ module Stores
3
+ class BoundBackup
4
+ attr_reader :store, :args
5
+ def initialize(store, args)
6
+ raise ArgumentError.new("double bound backup") if store.is_a?(BoundBackup)
7
+
8
+ @store = store
9
+ @args = args
10
+ end
11
+
12
+ def backup(dir)
13
+ store.backup(dir, args)
14
+ end
15
+
16
+ def restore(metadata)
17
+ store.restore(metadata)
18
+ end
19
+ end
20
+
21
+ module Mysql
22
+ class << self
23
+ def backup(dir, args)
24
+ Backup.call(dir, args)
25
+ end
26
+
27
+ def restore(metadata)
28
+ Restore.call(metadata)
29
+ end
30
+ end
31
+
32
+ class Backup
33
+ def self.call(dir, names)
34
+ new(dir, names).call
35
+ end
36
+
37
+ attr_reader :dir, :names
38
+ def initialize(dir, names)
39
+ @dir = dir
40
+ @names = names
41
+ end
42
+
43
+ def call
44
+ Cfg.shell.debug("backing up #{names} into #{dir}")
45
+
46
+ path = File.join(dir, "mysql.sql.gz")
47
+
48
+ Cfg.shell.run(<<~SH)
49
+ mysqldump \
50
+ --add-drop-database \
51
+ --databases #{names.join(" ")} \
52
+ | gzip > #{path}
53
+ SH
54
+
55
+ {
56
+ type: "mysql",
57
+ path: path,
58
+ databases: names
59
+ }
60
+ end
61
+ end
62
+
63
+ class Restore
64
+ def self.call(metadata)
65
+ new(metadata).call
66
+ end
67
+
68
+ attr_reader :metadata
69
+ def initialize(metadata)
70
+ @metadata = metadata
71
+ end
72
+
73
+ def call
74
+ dump_path = metadata.dig("path")
75
+ Cfg.shell.debug("restoring #{metadata.to_json}")
76
+ Cfg.shell.run("bash -ec 'zcat < #{dump_path} | mysql -uroot'")
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnapshotArchive
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "snapshot_archive/version"
4
+ require_relative "snapshot_archive/cfg"
5
+
6
+ module SnapshotArchive
7
+ class Error < StandardError; end
8
+
9
+ DESCRIPTION = <<~TEXT.chomp
10
+ Save and restore snapshots of stateful services to a central archive. The
11
+ default action is to backup all databases for the current rails apps,
12
+ however, custom actions can easily be configured.
13
+
14
+ See https://github.com/petekinnecom/snapshot_archive for the full README.
15
+ TEXT
16
+
17
+ class << self
18
+ def configure
19
+ yield(Cfg.instance)
20
+ end
21
+ end
22
+ end
23
+
24
+ require_relative "snapshot_archive/cli"
data/lib/template.txt ADDED
@@ -0,0 +1,5 @@
1
+
2
+
3
+ # Follows same rules as a git commit message. First line is summary, followed by
4
+ # more detailed info. Set `export EDITOR="..."` to use a different editor for
5
+ # this message. (eg. `export EDITOR="code --wait"` to use VSCode)
data/script/install ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -ex
4
+
5
+ gemname='snapshot_archive'
6
+
7
+ gem uninstall -Ix ${gemname}
8
+ gem build ${gemname}.gemspec
9
+ gemfile=${gemname}-"$(grep -oEi '\d+\.\d+\.\d+' lib/${gemname}/version.rb)".gem
10
+ gem install "${gemfile}"
11
+ rm "${gemfile}"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/snapshot_archive/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "snapshot_archive"
7
+ spec.version = SnapshotArchive::VERSION
8
+ spec.authors = ["Pete Kinnecom"]
9
+ spec.email = ["git@k7u7.com"]
10
+
11
+ spec.summary = <<~TEXT.chomp
12
+ Save and restore snapshots of stateful services to a central archive. The
13
+ default action is to backup all databases for the current rails apps,
14
+ however, custom actions can easily be configured.
15
+ TEXT
16
+
17
+ spec.homepage = "https://github.com/petekinnecom/snapshot_archive"
18
+ spec.license = "WTFPL"
19
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
20
+
21
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
22
+
23
+ spec.metadata["homepage_uri"] = spec.homepage
24
+ spec.metadata["source_code_uri"] = spec.homepage
25
+
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency("commander", "~> 4.5")
34
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snapshot_archive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pete Kinnecom
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.5'
27
+ description:
28
+ email:
29
+ - git@k7u7.com
30
+ executables:
31
+ - snap
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rubocop.yml"
37
+ - ".ruby-version"
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/setup
46
+ - exe/snap
47
+ - lib/snapshot_archive.rb
48
+ - lib/snapshot_archive/archives.rb
49
+ - lib/snapshot_archive/cfg.rb
50
+ - lib/snapshot_archive/cli.rb
51
+ - lib/snapshot_archive/default_configuration.rb
52
+ - lib/snapshot_archive/formatters.rb
53
+ - lib/snapshot_archive/repositories.rb
54
+ - lib/snapshot_archive/shell.rb
55
+ - lib/snapshot_archive/stores.rb
56
+ - lib/snapshot_archive/version.rb
57
+ - lib/template.txt
58
+ - script/install
59
+ - snapshot_archive.gemspec
60
+ homepage: https://github.com/petekinnecom/snapshot_archive
61
+ licenses:
62
+ - WTFPL
63
+ metadata:
64
+ allowed_push_host: https://rubygems.org
65
+ homepage_uri: https://github.com/petekinnecom/snapshot_archive
66
+ source_code_uri: https://github.com/petekinnecom/snapshot_archive
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.4.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.3.4
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Save and restore snapshots of stateful services to a central archive. The
86
+ default action is to backup all databases for the current rails apps, however, custom
87
+ actions can easily be configured.
88
+ test_files: []