db

package
v0.0.0-...-493bf59 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2021 License: Apache-2.0 Imports: 28 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BackupSubCmd = models.Command{
	Name:      "backup",
	ShortHelp: "Create a new backup",
	LongHelp: "<code>db backup</code> creates a new backup for the given database service. " +
		"The backup is started and unless <code>-s</code> is specified, the CLI will poll every few seconds until it finishes. " +
		"Regardless of a successful backup or not, the logs for the backup will be printed to the console when the backup is finished. " +
		"If an error occurs and the logs are not printed, you can use the db logs command to print out historical backup job logs. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db backup db01\n</pre>",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database service to create a backup for (e.g. 'db01')")
			skipPoll := subCmd.BoolOpt("s skip-poll", false, "Whether or not to wait for the backup to finish")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdBackup(*databaseName, *skipPoll, New(settings, crypto.New(), compress.New(), jobs.New(settings)), services.New(settings), jobs.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME [-s]"
		}
	},
}
View Source
var Cmd = models.Command{
	Name:      "db",
	ShortHelp: "Tasks for databases",
	LongHelp:  "The <code>db</code> command gives access to backup, import, and export services for databases. The db command can not be run directly but has subcommands.",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(cmd *cli.Cmd) {
			cmd.CommandLong(BackupSubCmd.Name, BackupSubCmd.ShortHelp, BackupSubCmd.LongHelp, BackupSubCmd.CmdFunc(settings))
			cmd.CommandLong(RestoreSubCmd.Name, RestoreSubCmd.ShortHelp, RestoreSubCmd.LongHelp, RestoreSubCmd.CmdFunc(settings))
			cmd.CommandLong(DownloadSubCmd.Name, DownloadSubCmd.ShortHelp, DownloadSubCmd.LongHelp, DownloadSubCmd.CmdFunc(settings))
			cmd.CommandLong(ExportSubCmd.Name, ExportSubCmd.ShortHelp, ExportSubCmd.LongHelp, ExportSubCmd.CmdFunc(settings))
			cmd.CommandLong(ImportSubCmd.Name, ImportSubCmd.ShortHelp, ImportSubCmd.LongHelp, ImportSubCmd.CmdFunc(settings))
			cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings))
			cmd.CommandLong(LogsSubCmd.Name, LogsSubCmd.ShortHelp, LogsSubCmd.LongHelp, LogsSubCmd.CmdFunc(settings))
		}
	},
}

Cmd is the contract between the user and the CLI. This specifies the command name, arguments, and required/optional arguments and flags for the command.

View Source
var DownloadSubCmd = models.Command{
	Name:      "download",
	ShortHelp: "Download a previously created backup",
	LongHelp: "<code>db download</code> downloads a previously created backup to your local hard drive. " +
		"Be careful using this command as it could download PHI. " +
		"Be sure that all hard drive encryption and necessary precautions have been taken before performing a download. " +
		"The ID of the backup is found by first running the db list command. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db download db01 cd2b4bce-2727-42d1-89e0-027bf3f1a203 ./db.sql\n</pre>\n\n" +
		"This assumes you are downloading a MySQL or PostgreSQL backup which takes the <code>.sql</code> file format. If you are downloading a mongo backup, the command might look like this\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db download db01 cd2b4bce-2727-42d1-89e0-027bf3f1a203 ./db.tar.gz\n</pre>",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database service which was backed up (e.g. 'db01')")
			backupID := subCmd.StringArg("BACKUP_ID", "", "The ID of the backup to download (found from \"datica backup list\")")
			filePath := subCmd.StringArg("FILEPATH", "", "The location to save the downloaded backup to. This location must NOT already exist unless -f is specified")
			force := subCmd.BoolOpt("f force", false, "If a file previously exists at \"filepath\", overwrite it and download the backup")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdDownload(*databaseName, *backupID, *filePath, *force, New(settings, crypto.New(), compress.New(), jobs.New(settings)), prompts.New(), services.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME BACKUP_ID FILEPATH [-f]"
		}
	},
}
View Source
var ExportSubCmd = models.Command{
	Name:      "export",
	ShortHelp: "Export data from a database",
	LongHelp: "<code>db export</code> is a simple wrapper around the <code>db backup</code> and <code>db download</code> commands. " +
		"When you request an export, a backup is created that will be added to the list of backups shown when you perform the db list command. " +
		"Then that backup is immediately downloaded. Regardless of a successful export or not, the logs for the backup will be printed to the console when the export is finished. " +
		"If an error occurs and the logs are not printed, you can use the db logs command to print out historical backup job logs. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db export db01 ./dbexport.sql\n</pre>\n\n" +
		"This assumes you are exporting a MySQL or PostgreSQL database which takes the <code>.sql</code> file format. If you are exporting a mongo database, the command might look like this\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db export db01 ./dbexport.tar.gz\n</pre>",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database to export data from (e.g. 'db01')")
			filePath := subCmd.StringArg("FILEPATH", "", "The location to save the exported data. This location must NOT already exist unless -f is specified")
			force := subCmd.BoolOpt("f force", false, "If a file previously exists at <code>filepath</code>, overwrite it and export data")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdExport(*databaseName, *filePath, *force, New(settings, crypto.New(), compress.New(), jobs.New(settings)), prompts.New(), services.New(settings), jobs.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME FILEPATH [-f]"
		}
	},
}
View Source
var ImportSubCmd = models.Command{
	Name:      "import",
	ShortHelp: "Import data into a database",
	LongHelp: "<code>db import</code> allows you to inject new data into your database service. For example, if you wrote a simple SQL file\n\n" +
		"<pre>\nCREATE TABLE mytable (\n" +
		"id TEXT PRIMARY KEY,\n" +
		"val TEXT\n" +
		");\n\n" +
		"INSERT INTO mytable (id, val) values ('1', 'test');\n</pre>\n\n" +
		"and stored it at <code>./db.sql</code> you could import this into your database service. " +
		"When importing data into mongo, you may specify the database and collection to import into using the <code>-d</code> and <code>-c</code> flags respectively. " +
		"Regardless of a successful import or not, the logs for the import will be printed to the console when the import is finished. " +
		"Before an import takes place, your database is backed up automatically in case any issues arise. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db import db01 ./db.sql\n</pre>\n\n" +
		"When importing data into postgres, import cannot DROP DATABASE \"catalyzeDB\". " +
		"Ensure your import individually removes any neccessary \"catalyzeDB\" objects, or import only into newly created postgres services where the \"catalyzeDB\" database is already empty.\n",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database to import data to (e.g. 'db01')")
			filePath := subCmd.StringArg("FILEPATH", "", "The location of the file to import to the database")
			mongoCollection := subCmd.StringOpt("c mongo-collection", "", "If importing into a mongo service, the name of the collection to import into")
			mongoDatabase := subCmd.StringOpt("d mongo-database", "", "If importing into a mongo service, the name of the database to import into")
			skipBackup := subCmd.BoolOpt("s skip-backup", false, "Skip backing up database. Useful for large databases, which can have long backup times.")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdImport(*databaseName, *filePath, *mongoCollection, *mongoDatabase, *skipBackup, New(settings, crypto.New(), compress.New(), jobs.New(settings)), prompts.New(), services.New(settings), jobs.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME FILEPATH [-s][-d][-c]"
		}
	},
}
View Source
var ListSubCmd = models.Command{
	Name:      "list",
	ShortHelp: "List created backups",
	LongHelp: "<code>db list</code> lists all previously created backups. " +
		"After listing backups you can copy the backup ID and use it to download that backup or view the logs from that backup. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db list db01\n</pre>",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database service to list backups for (e.g. 'db01')")
			page := subCmd.IntOpt("p page", 1, "The page to view")
			pageSize := subCmd.IntOpt("n page-size", 10, "The number of items to show per page")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdList(*databaseName, *page, *pageSize, New(settings, crypto.New(), compress.New(), jobs.New(settings)), services.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME [-p] [-n]"
		}
	},
}
View Source
var LogsSubCmd = models.Command{
	Name:      "logs",
	ShortHelp: "Print out the logs from a previous database backup job",
	LongHelp: "<code>db logs</code> allows you to view backup logs from historical backup jobs. " +
		"You can find the backup ID from using the <code>db list</code> command. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db logs db01 cd2b4bce-2727-42d1-89e0-027bf3f1a203\n</pre>",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database service (e.g. 'db01')")
			backupID := subCmd.StringArg("BACKUP_ID", "", "The ID of the backup to download logs from (found from \"datica backup list\")")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdLogs(*databaseName, *backupID, New(settings, crypto.New(), compress.New(), jobs.New(settings)), services.New(settings), jobs.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME BACKUP_ID"
		}
	},
}
View Source
var RestoreSubCmd = models.Command{
	Name:      "restore",
	ShortHelp: "Restore a previously created backup",
	LongHelp: "<code>db restore</code> restores a backup to your database. " +
		"The restore job does not back up data before performing the database restore. Perform a backup prior to the restore if you have concerns about overwriting data. " +
		"The restore command will confirm that you do not need to perform a backup. " +
		"When restoring data to postgres, please close all open consoles and database connections before proceeding. " +
		"If any users are connected to the database, postgres restore will fail. " +
		"Once the restore job is started, the CLI will poll every few seconds until it finishes. " +
		"Regardless of a successful restore or not, the logs for the restore will be printed to the console when the restore is finished. " +
		"If an error occurs and the logs are not printed, you can use the db logs command to print out historical backup job logs. Here is a sample command\n\n" +
		"<pre>\ndatica -E \"<your_env_name>\" db restore db01 00000000-0000-0000-0000-000000000000\n</pre>",
	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
		return func(subCmd *cli.Cmd) {
			databaseName := subCmd.StringArg("DATABASE_NAME", "", "The name of the database service which was backed up (e.g. 'db01')")
			backupID := subCmd.StringArg("BACKUP_ID", "", "The ID of the backup to download (found from \"datica backup list\")")
			mongoDatabase := subCmd.StringOpt("d mongo-database", "", "If restoring a mongo service, the name of the database to restore")
			skipConfirm := subCmd.BoolOpt("y yes", false, "Skip warning and confirmation")
			subCmd.Action = func() {
				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
					logrus.Fatal(err.Error())
				}
				if err := config.CheckRequiredAssociation(settings); err != nil {
					logrus.Fatal(err.Error())
				}
				err := CmdRestore(*databaseName, *backupID, *mongoDatabase, *skipConfirm, New(settings, crypto.New(), compress.New(), jobs.New(settings)), prompts.New(), services.New(settings))
				if err != nil {
					logrus.Fatal(err.Error())
				}
			}
			subCmd.Spec = "DATABASE_NAME BACKUP_ID [-d][-y]"
		}
	},
}

Functions

func CmdBackup

func CmdBackup(databaseName string, skipPoll bool, id IDb, is services.IServices, ij jobs.IJobs) error

func CmdDownload

func CmdDownload(databaseName, backupID, filePath string, force bool, id IDb, ip prompts.IPrompts, is services.IServices) error

func CmdExport

func CmdExport(databaseName, filePath string, force bool, id IDb, ip prompts.IPrompts, is services.IServices, ij jobs.IJobs) error

func CmdImport

func CmdImport(databaseName, filePath, mongoCollection, mongoDatabase string, skipBackup bool, id IDb, ip prompts.IPrompts, is services.IServices, ij jobs.IJobs) error

func CmdList

func CmdList(databaseName string, page, pageSize int, id IDb, is services.IServices) error

func CmdLogs

func CmdLogs(databaseName, backupID string, id IDb, is services.IServices, ij jobs.IJobs) error

func CmdRestore

func CmdRestore(databaseName, backupID, mongoDatabase string, skipConfirm bool, id IDb, ip prompts.IPrompts, is services.IServices) error

Types

type IDb

type IDb interface {
	Backup(service *models.Service) (*models.Job, error)
	Restore(backupID string, service *models.Service, mongoDatabase string) error
	Download(backupID, filePath string, service *models.Service) error
	Export(filePath string, job *models.Job, service *models.Service) error
	Import(rt *transfer.ReaderTransfer, key, iv []byte, mongoCollection, mongoDatabase string, service *models.Service, singleUploadMode bool) (*models.Job, error)
	List(page, pageSize int, service *models.Service) (*[]models.Job, error)
	TempDownloadURL(jobID string, service *models.Service) (*models.TempURL, error)
	TempLogsURL(jobID string, serviceID string) (*models.TempURL, error)
	DumpLogs(taskType string, job *models.Job, service *models.Service) error
	NewEncryptReader(reader io.Reader, key, iv []byte) (*gcm.EncryptReader, error)
	RetrievePodApiVersion() (*models.VersionInfo, error)
}

IDb

func New

func New(settings *models.Settings, crypto crypto.ICrypto, compress compress.ICompress, jobs jobs.IJobs) IDb

New returns an instance of IDb

type SDb

type SDb struct {
	Settings *models.Settings
	Crypto   crypto.ICrypto
	Compress compress.ICompress
	Jobs     jobs.IJobs
}

SDb is a concrete implementation of IDb

func (*SDb) Backup

func (d *SDb) Backup(service *models.Service) (*models.Job, error)

Backup creates a new backup

func (*SDb) CompleteMultiPartUpload

func (d *SDb) CompleteMultiPartUpload(service *models.Service, fileName string, uploadID string, parts []map[string]interface{}) (*models.Location, error)

func (*SDb) Download

func (d *SDb) Download(backupID, filePath string, service *models.Service) error

Download an existing backup to the local machine. The backup is encrypted throughout the entire journey and then decrypted once it is stored locally.

func (*SDb) DumpLogs

func (d *SDb) DumpLogs(taskType string, job *models.Job, service *models.Service) error

DumpLogs dumps logs from a Backup/Restore/Import/Export job to the console

func (*SDb) Export

func (d *SDb) Export(filePath string, job *models.Job, service *models.Service) error

Export dumps all data from a database service and downloads the encrypted data to the local machine. The export is accomplished by first creating a backup. Once finished, the CLI asks where the file can be downloaded from. The file is downloaded, decrypted, decompressed, and saved locally.

func (*SDb) Import

func (d *SDb) Import(rt *transfer.ReaderTransfer, key, iv []byte, mongoCollection, mongoDatabase string, service *models.Service, singleUploadMode bool) (*models.Job, error)

Import imports data into a database service. The import is accomplished by encrypting the file locally, requesting a location that it can be uploaded to, then uploads the file. Once uploaded an automated service processes the file and acts according to the given parameters.

The type of file that should be imported depends on the database. For PostgreSQL and MySQL, this should be a single `.sql` file. For Mongo, this should be a single tar'ed, gzipped archive (`.tar.gz`) of the database dump that you want to import.

func (*SDb) InitiateMultiPartUpload

func (d *SDb) InitiateMultiPartUpload(service *models.Service) (*models.MultipartUploadInfo, error)

func (*SDb) List

func (d *SDb) List(page, pageSize int, service *models.Service) (*[]models.Job, error)

List lists the created backups for the service sorted from oldest to newest

func (*SDb) NewEncryptReader

func (db *SDb) NewEncryptReader(reader io.Reader, key, iv []byte) (*gcm.EncryptReader, error)

func (*SDb) Restore

func (d *SDb) Restore(backupID string, service *models.Service, mongoDatabase string) error

Restore a backup to the database.

func (*SDb) RetrievePodApiVersion

func (d *SDb) RetrievePodApiVersion() (*models.VersionInfo, error)

func (*SDb) TempDownloadURL

func (d *SDb) TempDownloadURL(jobID string, service *models.Service) (*models.TempURL, error)

func (*SDb) TempLogsURL

func (d *SDb) TempLogsURL(jobID string, serviceID string) (*models.TempURL, error)

func (*SDb) TempUploadURL

func (d *SDb) TempUploadURL(service *models.Service, fileName string, partNumber string, uploadID string) (*models.TempURL, error)

func (*SDb) TempUploadURLSingleUploadMode

func (d *SDb) TempUploadURLSingleUploadMode(service *models.Service) (*models.TempURL, error)

type SortedJobs

type SortedJobs []models.Job

SortedJobs is a wrapper for Jobs array in order to sort them by CreatedAt for the ListBackups command

func (SortedJobs) Len

func (jobs SortedJobs) Len() int

func (SortedJobs) Less

func (jobs SortedJobs) Less(i, j int) bool

func (SortedJobs) Swap

func (jobs SortedJobs) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL