Ejemplo n.º 1
0
//BackupAllDatabases - Use pg_dumpall on a SOLO cluster to perform a full backup of everything postgres related
func (t *Task) BackupAllDatabases() (err error) {
	b := backupParams{}

	b.databaseName = "postgres"
	b.pgDumpPath, err = config.GetValue(`pgDumpBinaryLocation`)
	if err != nil {
		return err
	}
	b.pgPort, err = config.GetValue(`BackupPort`)
	if err != nil {
		return err
	}
	b.basePath, err = config.GetValue(`BackupsPath`)
	if err != nil {
		return err
	}
	b.node, err = rdpgconsul.GetNode()
	if err != nil {
		return err
	}
	b.baseFileName = getBaseFileName() //Use this to keep schema and data file names the same

	err = createTargetFolder(b.basePath + `/` + b.databaseName)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.BackupAllDatabases() Could not create target folder %s ! %s", b.basePath, err))
		return err
	}

	createDumpAllFileHistory, err := createDumpAllFile(b)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.BackupAllDatabases() Could not create pg_dumpall file for database %s ! %s", b.databaseName, err))
		createDumpAllFileHistory.Status = `error`
	}
	err = history.InsertBackupHistoryDumpAll(createDumpAllFileHistory)

	return
}
Ejemplo n.º 2
0
//BackupDatabase - Perform a schema and database backup of a given database to local disk
func (t *Task) BackupDatabase() (err error) {
	b := backupParams{}

	//Make sure database actually exists first.
	b.databaseName = t.Data
	if b.databaseName != "rdpg" {
		address := `127.0.0.1`
		sq := fmt.Sprintf(`SELECT 1 FROM cfsb.instances WHERE effective_at IS NOT NULL AND decommissioned_at IS NULL AND dbname = '%s';`, b.databaseName)
		databasesWithThatName, err := rdpgpg.GetList(address, sq)
		if err != nil {
			log.Error(fmt.Sprintf("Tasks.BackupDatabase() utils/backup.GetList(%s, %s) Error trying to query for database.", address, b.databaseName))
			return err
		}
		if len(databasesWithThatName) == 0 {
			log.Error(fmt.Sprintf("Task.BackupDatabase() Attempt to back up non-existant or non-commissioned database with name: %s", b.databaseName))
			return errors.New("Database doesn't exist.")
		}
	}
	lockAcquired, sessID := acquireBackupLock(b.databaseName)
	if !lockAcquired {
		log.Warn("Aborting Backup: Unable to acquire database lock. Is another backup already in progress?")
		return errors.New("Unable to acquire database lock")
	}
	defer releaseBackupLock(b.databaseName, sessID)

	b.pgDumpPath, err = config.GetValue(`pgDumpBinaryLocation`)
	if err != nil {
		return err
	}
	b.pgPort, err = config.GetValue(`BackupPort`)
	if err != nil {
		return err
	}
	b.basePath, err = config.GetValue(`BackupsPath`)
	if err != nil {
		return err
	}
	b.node, err = rdpgconsul.GetNode()
	if err != nil {
		return err
	}
	b.baseFileName = getBaseFileName() //Use this to keep schema and data file names the same

	err = createTargetFolder(b.basePath + `/` + b.databaseName)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.BackupDatabase() Could not create target folder %s ! %s", b.basePath, err))
		return err
	}

	schemaDataFileHistory, err := createSchemaAndDataFile(b)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.BackupDatabase() Could not create schema and data file for database %s ! %s", b.databaseName, err))
		schemaDataFileHistory.Status = `error`
	}
	err = history.InsertBackupHistory(schemaDataFileHistory)

	if b.databaseName == `rdpg` {
		globalsFileHistory, err := createGlobalsFile(b)
		if err != nil {
			log.Error(fmt.Sprintf("tasks.BackupDatabase() Could not create globals file for database %s ! %s", b.databaseName, err))
			globalsFileHistory.Status = `error`
		}

		err = history.InsertBackupHistory(globalsFileHistory)

	}
	return
}