Sha256: fe520395301e3bd4e3212b0c68643c2b7acfefc2d1352bd7641edf5822d20fc2

Contents?: true

Size: 1.66 KB

Versions: 11

Compression:

Stored size: 1.66 KB

Contents

module SnapshotArchive
  module Stores
    class BoundBackup
      attr_reader :store, :args
      def initialize(store, args)
        raise ArgumentError.new("double bound backup") if store.is_a?(BoundBackup)

        @store = store
        @args = args
      end

      def backup(dir)
        store.backup(dir, args)
      end

      def restore(metadata)
        store.restore(metadata)
      end
    end

    module Mysql
      class << self
        def backup(dir, args)
          Backup.call(dir, args)
        end

        def restore(metadata)
          Restore.call(metadata)
        end
      end

      class Backup
        def self.call(dir, names)
          new(dir, names).call
        end

        attr_reader :dir, :names
        def initialize(dir, names)
          @dir = dir
          @names = names
        end

        def call
          Cfg.shell.debug("backing up #{names} into #{dir}")

          path = File.join(dir, "mysql.sql.gz")

          Cfg.shell.run(<<~SH)
            mysqldump \
              --add-drop-database \
              --databases #{names.join(" ")} \
              | gzip > #{path}
          SH

          {
            type: "mysql",
            path: path,
            databases: names
          }
        end
      end

      class Restore
        def self.call(metadata)
          new(metadata).call
        end

        attr_reader :metadata
        def initialize(metadata)
          @metadata = metadata
        end

        def call
          dump_path = metadata.dig("path")
          Cfg.shell.debug("restoring #{metadata.to_json}")
          Cfg.shell.run("bash -ec 'zcat < #{dump_path} | mysql -uroot'")
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
snapshot_archive-0.12.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.11.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.10.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.9.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.8.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.7.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.6.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.5.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.3.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.2.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.1.0 lib/snapshot_archive/stores.rb