Skip to content

Commit

Permalink
Merge pull request #7 from woblerr/history-clean
Browse files Browse the repository at this point in the history
Add history-clean command.
  • Loading branch information
woblerr authored Feb 8, 2024
2 parents b5ca7db + 448519e commit d4e1ea2
Show file tree
Hide file tree
Showing 23 changed files with 917 additions and 100 deletions.
113 changes: 98 additions & 15 deletions COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
- [Display information about backups (`backup-info`)](#display-information-about-backups-backup-info)
- [Examples](#examples-2)
- [Using container](#using-container-2)
- [Migrate history database (`history-migrate`)](#migrate-history-database-history-migrate)
- [Clean failed and deleted backups from the history database (`history-clean`)](#clean-failed-and-deleted-backups-from-the-history-database-history-clean)
- [Examples](#examples-3)
- [Delete information about failed and deleted backups from history database older than n days](#delete-information-about-failed-and-deleted-backups-from-history-database-older-than-n-days)
- [Delete all backups using storage plugin older than timestamp](#delete-all-backups-using-storage-plugin-older-than-timestamp-1)
- [Using container](#using-container-3)
- [Display the report for a specific backup (`report-info`)](#display-the-report-for-a-specific-backup-report-info)
- [Migrate history database (`history-migrate`)](#migrate-history-database-history-migrate)
- [Examples](#examples-4)
- [Using container](#using-container-4)
- [Display the report for a specific backup (`report-info`)](#display-the-report-for-a-specific-backup-report-info)
- [Examples](#examples-5)
- [Display the backup report from local storage](#display-the-backup-report-from-local-storage)
- [Display the backup report using storage plugin](#display-the-backup-report-using-storage-plugin)
- [Using container](#using-container-4)
- [Using container](#using-container-5)

# Delete all existing backups older than the specified time condition (`backup-clean`)

Expand Down Expand Up @@ -82,7 +87,7 @@ gpBackMan returns a message:
Delete all backups older than 7 days and all dependent backups:
```bash
./gpbackman backup-clean \
--older-than-day 7 \
--older-than-days 7 \
--plugin-config /tmp/gpbackup_plugin_config.yaml \
--cascade
```
Expand All @@ -98,7 +103,7 @@ Delete all backups older than timestamp `20240101100000` and all dependent backu
## Using container
Delete all backups using `gpbackup_s3_plugin` storage plugin: older than 7 days:
Delete all backups using `gpbackup_s3_plugin` storage plugin older than 7 days:
```bash
docker run \
--name gpbackman \
Expand All @@ -109,7 +114,7 @@ docker run \
-v /path/to/gpbackup_plugin_config.yaml:/tmp/gpbackup_plugin_config.yaml \
gpbackman \
gpbackman backup-clean \
--older-than-day 7 \
--older-than-days 7 \
--history-db /data/master/gpseg-1/gpbackup_history.db \
--plugin-config /tmp/gpbackup_plugin_config.yaml
```
Expand Down Expand Up @@ -215,9 +220,9 @@ Display information about backups.

By default, only active backups or backups with deletion status "In progress" from gpbackup_history.db are displayed.

To additional display deleted backups, use the --show-deleted option.
To additional display failed backups, use the --show-failed option.
To display all backups, use --show-deleted and --show-failed options together.
To additional display deleted backups, use the --deleted option.
To additional display failed backups, use the --failed option.
To display all backups, use --deleted and --failed options together.

To display backups of a specific type, use the --type option.

Expand All @@ -235,10 +240,10 @@ Usage:
gpbackman backup-info [flags]

Flags:
-h, --help help for backup-info
--show-deleted show deleted backups
--show-failed show failed backups
--type string backup type filter (full, incremental, data-only, metadata-only)
--deleted show deleted backups
--failed show failed backups
-h, --help help for backup-info
--type string backup type filter (full, incremental, data-only, metadata-only)

Global Flags:
--history-db string full path to the gpbackup_history.db file
Expand Down Expand Up @@ -295,8 +300,8 @@ Display info for active backups from `gpbackup_history.db`:
Display info for all backups from `gpbackup_history.yaml`:
```bash
./gpbackman backup-info \
--show-deleted \
--show-failed \
--deleted \
--failed \
--history-file /data/master/gpseg-1/gpbackup_history.yaml
TIMESTAMP | DATE | STATUS | DATABASE | TYPE | OBJECT FILTERING | PLUGIN | DURATION | DATE DELETED
----------------+--------------------------+---------+----------+---------------+------------------+--------------------+----------+--------------
Expand All @@ -321,6 +326,84 @@ docker run \
--history-db /data/master/gpseg-1/gpbackup_history.db
```
# Clean failed and deleted backups from the history database (`history-clean`)
Available options for `history-clean` command and their description:
```bash
./gpbackman history-clean -h
Clean failed and deleted backups from the history database.
Only the database is being cleaned up.
By default, information is deleted only about failed backups from gpbackup_history.db.
To delete information about deleted backups, use the --deleted option.
To delete information about backups older than the given timestamp, use the --before-timestamp option.
To delete information about backups older than the given number of days, use the --older-than-day option.
Only --older-than-days or --before-timestamp option must be specified, not both.
The gpbackup_history.db file location can be set using the --history-db option.
Can be specified only once. The full path to the file is required.
The gpbackup_history.yaml file location can be set using the --history-file option.
Can be specified multiple times. The full path to the file is required.
If no --history-file or --history-db options are specified, the history database will be searched in the current directory.
Only --history-file or --history-db option can be specified, not both.
Usage:
gpbackman history-clean [flags]
Flags:
--before-timestamp string delete information about backups older than the given timestamp
--deleted delete information about deleted backups
-h, --help help for history-clean
--older-than-days uint delete information about backups older than the given number of days
Global Flags:
--history-db string full path to the gpbackup_history.db file
--history-file stringArray full path to the gpbackup_history.yaml file, could be specified multiple times
--log-file string full path to log file directory, if not specified, the log file will be created in the $HOME/gpAdminLogs directory
--log-level-console string level for console logging (error, info, debug, verbose) (default "info")
--log-level-file string level for file logging (error, info, debug, verbose) (default "info")
```
## Examples
### Delete information about failed and deleted backups from history database older than n days
Delete information about failed and deleted backups from history database older than 7 days:
```bash
./gpbackman history-clean \
--older-than-days 7 \
--deleted
```
### Delete all backups using storage plugin older than timestamp
Delete information about failed and deleted backups from history database older than timestamp `20240101100000`:
```bash
./gpbackman history-clean \
--before-timestamp 20240101100000 \
--deleted
```
## Using container
Delete information about failed and deleted backups from history database older than 7 days:
```bash
docker run \
--name gpbackman \
-e GPBACKMAN_UID=$(id -u) \
-e GPBACKMAN_GID=$(id -g) \
-v /data/master/gpseg-1/gpbackup_history.db:/data/master/gpseg-1/gpbackup_history.db \
gpbackman \
gpbackman history-clean \
--older-than-days 7 \
--history-db /data/master/gpseg-1/gpbackup_history.db \
--deleted
```
# Migrate history database (`history-migrate`)
Available options for `history-migrate` command and their description:
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test-e2e:
@make test-e2e_report-info
@make test-e2e_backup-delete
@make test-e2e_history-migrate
@make test-e2e_history-clean

.PHONY: test-e2e_backup-info
test-e2e_backup-info:
Expand All @@ -43,6 +44,13 @@ test-e2e_backup-clean:
$(call run_docker_compose,backup-clean)
$(call down_docker_compose)

.PHONY: test-e2e_history-clean
test-e2e_history-clean:
@echo "Run end-to-end tests for $(APP_NAME) for history-clean command"
$(call down_docker_compose)
$(call run_docker_compose,history-clean)
$(call down_docker_compose)

.PHONY: test-e2e_history-migrate
test-e2e_history-migrate:
@echo "Run end-to-end tests for $(APP_NAME) for history-migrate command"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The utility works with both history database formats: `gpbackup_history.yaml` fi
* display the backup report for existing backups;
* delete existing backups from local storage or using storage plugins (for example, [S3 Storage Plugin](https://github.com/greenplum-db/gpbackup-s3-plugin));
* delete all existing backups from local storage or using storage plugins older than the specified time condition;
* clean failed and deleted backups from the history database;
* migrate history database from `gpbackup_history.yaml` format to `gpbackup_history.db` SQLite format.

## Commands
Expand All @@ -34,6 +35,7 @@ Available Commands:
backup-info Display information about backups
completion Generate the autocompletion script for the specified shell
help Help about any command
history-clean Clean failed and deleted backups from the history database
history-migrate Migrate history database
report-info Display the report for a specific backup

Expand All @@ -55,6 +57,7 @@ Description of each command:
* [Delete all existing backups older than the specified time condition (`backup-clean`)](./COMMANDS.md#delete-all-existing-backups-older-than-the-specified-time-condition-backup-clean)
* [Delete a specific existing backup (`backup-delete`)](./COMMANDS.md#delete-a-specific-existing-backup-backup-delete)
* [Display information about backups (`backup-info`)](./COMMANDS.md#display-information-about-backups-backup-info)
* [Clean failed and deleted backups from the history database (`history-clean`)](./COMMANDS.md#clean-failed-and-deleted-backups-from-the-history-database-history-clean)
* [Migrate history database (`history-migrate`)](./COMMANDS.md#migrate-history-database-history-migrate)
* [Display the report for a specific backup (`report-info`)](./COMMANDS.md#display-the-report-for-a-specific-backup-report-info)
Expand Down
9 changes: 9 additions & 0 deletions cmd/backup_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ func cleanBackup() error {
}
err = backupCleanFilePlugin(backupCleanCascade, beforeTimestamp, backupCleanPluginConfigFile, pluginConfig, &parseHData)
if err != nil {
// In current implementation, there are cases where some backups were deleted, and some were not.
// Foe example, the clean command was executed without --cascade option.
// In this case - metadata backup was deleted, but full + incrementals - weren't.
// We should update the history file even it error occurred.
errUpdateHFile := parseHData.UpdateHistoryFile(hFile)
if errUpdateHFile != nil {
gplog.Error(textmsg.ErrorTextUnableActionHistoryFile("update", errUpdateHFile))
}
// It is enough to return only one error.
return err
}
} else {
Expand Down
20 changes: 3 additions & 17 deletions cmd/backup_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func backupDeleteDBPluginFunc(backupName, pluginConfigPath string, pluginConfig
var err error
dateDeleted := getCurrentTimestamp()
gplog.Info(textmsg.InfoTextBackupDeleteStart(backupName))
err = updateDeleteStatus(backupName, gpbckpconfig.DateDeletedInProgress, hDB)
err = gpbckpconfig.UpdateDeleteStatus(backupName, gpbckpconfig.DateDeletedInProgress, hDB)
if err != nil {
gplog.Error(textmsg.ErrorTextUnableSetBackupStatus(gpbckpconfig.DateDeletedInProgress, backupName, err))
return err
Expand All @@ -256,15 +256,15 @@ func backupDeleteDBPluginFunc(backupName, pluginConfigPath string, pluginConfig
gplog.Error(stderr)
}
if errdel != nil {
err = updateDeleteStatus(backupName, gpbckpconfig.DateDeletedPluginFailed, hDB)
err = gpbckpconfig.UpdateDeleteStatus(backupName, gpbckpconfig.DateDeletedPluginFailed, hDB)
if err != nil {
gplog.Error(textmsg.ErrorTextUnableSetBackupStatus(gpbckpconfig.DateDeletedPluginFailed, backupName, err))
}
gplog.Error(textmsg.ErrorTextUnableDeleteBackup(backupName, errdel))
return errdel
}
gplog.Info(stdout)
err = updateDeleteStatus(backupName, dateDeleted, hDB)
err = gpbckpconfig.UpdateDeleteStatus(backupName, dateDeleted, hDB)
if err != nil {
gplog.Error(textmsg.ErrorTextUnableSetBackupStatus(dateDeleted, backupName, err))
return err
Expand Down Expand Up @@ -384,17 +384,3 @@ func execDeleteBackup(executablePath, deleteBackupPluginCommand, pluginConfigFil
err := cmd.Run()
return stdout.String(), stderr.String(), err
}

func updateDeleteStatus(timestamp, deleteStatus string, historyDB *sql.DB) error {
tx, _ := historyDB.Begin()
_, err := tx.Exec(`UPDATE backups
SET date_deleted = ?
WHERE timestamp = ?;`,
deleteStatus, timestamp)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
return err
}
10 changes: 5 additions & 5 deletions cmd/backup_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var backupInfoCmd = &cobra.Command{
By default, only active backups or backups with deletion status "In progress" from gpbackup_history.db are displayed.
To additional display deleted backups, use the --show-deleted option.
To additional display failed backups, use the --show-failed option.
To display all backups, use --show-deleted and --show-failed options together.
To additional display deleted backups, use the --deleted option.
To additional display failed backups, use the --failed option.
To display all backups, use --deleted and --failed options together.
To display backups of a specific type, use the --type option.
Expand All @@ -54,13 +54,13 @@ func init() {
rootCmd.AddCommand(backupInfoCmd)
backupInfoCmd.Flags().BoolVar(
&backupInfoShowDeleted,
showDeletedFlagName,
deletedFlagName,
false,
"show deleted backups",
)
backupInfoCmd.Flags().BoolVar(
&backupInfoShowFailed,
showFailedFlagName,
failedFlagName,
false,
"show failed backups",
)
Expand Down
8 changes: 6 additions & 2 deletions cmd/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ const (
timestampFlagName = "timestamp"
pluginConfigFileFlagName = "plugin-config"
reportFilePluginPathFlagName = "plugin-report-file-path"
showDeletedFlagName = "show-deleted"
showFailedFlagName = "show-failed"
deletedFlagName = "deleted"
failedFlagName = "failed"
cascadeFlagName = "cascade"
forceFlagName = "force"
olderThenDaysFlagName = "older-than-days"
beforeTimestampFlagName = "before-timestamp"
typeFlagName = "type"

exitErrorCode = 1

// Batch size for deleting from sqlite3.
// This is to prevent problem with sqlite3.
sqliteDeleteBatchSize = 1000
)

var (
Expand Down
Loading

0 comments on commit d4e1ea2

Please sign in to comment.