Sha256: 7bf31e395046da352c0302ecf759aa1dfce905377f41846134c5bf0adddb2f47

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 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(**opts)
        store.backup(args: args, **opts)
      end

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

      def delete(...)
        store.delete(...)
      end
    end

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

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

      class Backup
        def self.call(...)
          new(...).call
        end

        attr_reader :dir, :names, :id
        def initialize(dir:, id:, args:, **)
          @dir = dir
          @id = id
          @names = args
        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(...)
          new(...).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

4 entries across 4 versions & 1 rubygems

Version Path
snapshot_archive-0.25.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.24.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.23.0 lib/snapshot_archive/stores.rb
snapshot_archive-0.22.0 lib/snapshot_archive/stores.rb