Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c9e5f04ca | |||
| 86a28b6ec5 | |||
| 63b35414d2 | |||
| db46770e7f | |||
| 51764a677a | |||
| bdbbb59e51 | |||
| 1a6ea13222 |
@ -11,6 +11,7 @@ import (
|
||||
|
||||
"dbbackup/internal/database"
|
||||
"dbbackup/internal/engine/native"
|
||||
"dbbackup/internal/metadata"
|
||||
"dbbackup/internal/notify"
|
||||
|
||||
"github.com/klauspost/pgzip"
|
||||
@ -163,6 +164,54 @@ func runNativeBackup(ctx context.Context, db database.Database, databaseName, ba
|
||||
"duration", backupDuration,
|
||||
"engine", result.EngineUsed)
|
||||
|
||||
// Get actual file size from disk
|
||||
fileInfo, err := os.Stat(outputFile)
|
||||
var actualSize int64
|
||||
if err == nil {
|
||||
actualSize = fileInfo.Size()
|
||||
} else {
|
||||
actualSize = result.BytesProcessed
|
||||
}
|
||||
|
||||
// Calculate SHA256 checksum
|
||||
sha256sum, err := metadata.CalculateSHA256(outputFile)
|
||||
if err != nil {
|
||||
log.Warn("Failed to calculate SHA256", "error", err)
|
||||
sha256sum = ""
|
||||
}
|
||||
|
||||
// Create and save metadata file
|
||||
meta := &metadata.BackupMetadata{
|
||||
Version: "1.0",
|
||||
Timestamp: backupStartTime,
|
||||
Database: databaseName,
|
||||
DatabaseType: dbType,
|
||||
Host: cfg.Host,
|
||||
Port: cfg.Port,
|
||||
User: cfg.User,
|
||||
BackupFile: filepath.Base(outputFile),
|
||||
SizeBytes: actualSize,
|
||||
SHA256: sha256sum,
|
||||
Compression: "gzip",
|
||||
BackupType: backupType,
|
||||
Duration: backupDuration.Seconds(),
|
||||
ExtraInfo: map[string]string{
|
||||
"engine": result.EngineUsed,
|
||||
"objects_processed": fmt.Sprintf("%d", result.ObjectsProcessed),
|
||||
},
|
||||
}
|
||||
|
||||
if cfg.CompressionLevel == 0 {
|
||||
meta.Compression = "none"
|
||||
}
|
||||
|
||||
metaPath := outputFile + ".meta.json"
|
||||
if err := metadata.Save(metaPath, meta); err != nil {
|
||||
log.Warn("Failed to save metadata", "error", err)
|
||||
} else {
|
||||
log.Debug("Metadata saved", "path", metaPath)
|
||||
}
|
||||
|
||||
// Audit log: backup completed
|
||||
auditLogger.LogBackupComplete(user, databaseName, cfg.BackupDir, result.BytesProcessed)
|
||||
|
||||
|
||||
23
cmd/root.go
23
cmd/root.go
@ -15,11 +15,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
log logger.Logger
|
||||
auditLogger *security.AuditLogger
|
||||
rateLimiter *security.RateLimiter
|
||||
notifyManager *notify.Manager
|
||||
cfg *config.Config
|
||||
log logger.Logger
|
||||
auditLogger *security.AuditLogger
|
||||
rateLimiter *security.RateLimiter
|
||||
notifyManager *notify.Manager
|
||||
deprecatedPassword string
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
@ -47,6 +48,11 @@ For help with specific commands, use: dbbackup [command] --help`,
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check for deprecated password flag
|
||||
if deprecatedPassword != "" {
|
||||
return fmt.Errorf("--password flag is not supported for security reasons. Use environment variables instead:\n - MySQL/MariaDB: export MYSQL_PWD='your_password'\n - PostgreSQL: export PGPASSWORD='your_password' or use .pgpass file")
|
||||
}
|
||||
|
||||
// Store which flags were explicitly set by user
|
||||
flagsSet := make(map[string]bool)
|
||||
cmd.Flags().Visit(func(f *pflag.Flag) {
|
||||
@ -171,15 +177,8 @@ func Execute(ctx context.Context, config *config.Config, logger logger.Logger) e
|
||||
rootCmd.PersistentFlags().StringVar(&cfg.Database, "database", cfg.Database, "Database name")
|
||||
// SECURITY: Password flag removed - use PGPASSWORD/MYSQL_PWD environment variable or .pgpass file
|
||||
// Provide helpful error message for users expecting --password flag
|
||||
var deprecatedPassword string
|
||||
rootCmd.PersistentFlags().StringVar(&deprecatedPassword, "password", "", "DEPRECATED: Use MYSQL_PWD or PGPASSWORD environment variable instead")
|
||||
rootCmd.PersistentFlags().MarkHidden("password")
|
||||
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
||||
if deprecatedPassword != "" {
|
||||
return fmt.Errorf("--password flag is not supported for security reasons. Use environment variables instead:\n - MySQL/MariaDB: export MYSQL_PWD='your_password'\n - PostgreSQL: export PGPASSWORD='your_password' or use .pgpass file")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
rootCmd.PersistentFlags().StringVarP(&cfg.DatabaseType, "db-type", "d", cfg.DatabaseType, "Database type (postgres|mysql|mariadb)")
|
||||
rootCmd.PersistentFlags().StringVar(&cfg.BackupDir, "backup-dir", cfg.BackupDir, "Backup directory")
|
||||
rootCmd.PersistentFlags().BoolVar(&cfg.NoColor, "no-color", cfg.NoColor, "Disable colored output")
|
||||
|
||||
@ -70,11 +70,11 @@ func (p *PostgreSQL) Connect(ctx context.Context) error {
|
||||
maxConns = 5 // minimum pool size
|
||||
}
|
||||
}
|
||||
config.MaxConns = maxConns // Max concurrent connections based on --jobs
|
||||
config.MinConns = 2 // Keep minimum connections ready
|
||||
config.MaxConnLifetime = 0 // No limit on connection lifetime
|
||||
config.MaxConnIdleTime = 0 // No idle timeout
|
||||
config.HealthCheckPeriod = 1 * time.Minute // Health check every minute
|
||||
config.MaxConns = maxConns // Max concurrent connections based on --jobs
|
||||
config.MinConns = 2 // Keep minimum connections ready
|
||||
config.MaxConnLifetime = 0 // No limit on connection lifetime
|
||||
config.MaxConnIdleTime = 0 // No idle timeout
|
||||
config.HealthCheckPeriod = 5 * time.Second // Faster health check for quicker shutdown on Ctrl+C
|
||||
|
||||
// Optimize for large query results (BLOB data)
|
||||
config.ConnConfig.RuntimeParams["work_mem"] = "64MB"
|
||||
@ -97,6 +97,16 @@ func (p *PostgreSQL) Connect(ctx context.Context) error {
|
||||
|
||||
p.pool = pool
|
||||
p.db = db
|
||||
|
||||
// CRITICAL: Start a goroutine to close the pool when context is cancelled
|
||||
// This ensures the background health check goroutine is stopped on Ctrl+C
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
if p.pool != nil {
|
||||
p.pool.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
p.log.Info("Connected to PostgreSQL successfully", "driver", "pgx", "max_conns", config.MaxConns)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -71,7 +71,14 @@ const (
|
||||
)
|
||||
|
||||
// NewParallelRestoreEngine creates a new parallel restore engine
|
||||
// NOTE: Pass a cancellable context to ensure the pool is properly closed on Ctrl+C
|
||||
func NewParallelRestoreEngine(config *PostgreSQLNativeConfig, log logger.Logger, workers int) (*ParallelRestoreEngine, error) {
|
||||
return NewParallelRestoreEngineWithContext(context.Background(), config, log, workers)
|
||||
}
|
||||
|
||||
// NewParallelRestoreEngineWithContext creates a new parallel restore engine with context support
|
||||
// This ensures the connection pool is properly closed when the context is cancelled
|
||||
func NewParallelRestoreEngineWithContext(ctx context.Context, config *PostgreSQLNativeConfig, log logger.Logger, workers int) (*ParallelRestoreEngine, error) {
|
||||
if workers < 1 {
|
||||
workers = 4 // Default to 4 parallel workers
|
||||
}
|
||||
@ -94,17 +101,33 @@ func NewParallelRestoreEngine(config *PostgreSQLNativeConfig, log logger.Logger,
|
||||
poolConfig.MaxConns = int32(workers + 2)
|
||||
poolConfig.MinConns = int32(workers)
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig)
|
||||
// CRITICAL: Reduce health check period to allow faster shutdown
|
||||
// Default is 1 minute which causes hangs on Ctrl+C
|
||||
poolConfig.HealthCheckPeriod = 5 * time.Second
|
||||
|
||||
// Use the provided context so pool health checks stop when context is cancelled
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create connection pool: %w", err)
|
||||
}
|
||||
|
||||
return &ParallelRestoreEngine{
|
||||
engine := &ParallelRestoreEngine{
|
||||
config: config,
|
||||
pool: pool,
|
||||
log: log,
|
||||
parallelWorkers: workers,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CRITICAL: Start a goroutine to close the pool when context is cancelled
|
||||
// This ensures the background health check goroutine is stopped on Ctrl+C
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
if pool != nil {
|
||||
pool.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
return engine, nil
|
||||
}
|
||||
|
||||
// RestoreFile restores from a SQL file with parallel execution
|
||||
@ -215,17 +238,38 @@ func (e *ParallelRestoreEngine) RestoreFile(ctx context.Context, filePath string
|
||||
semaphore := make(chan struct{}, options.Workers)
|
||||
var completedCopies int64
|
||||
var totalRows int64
|
||||
var cancelled int32 // Atomic flag to signal cancellation
|
||||
|
||||
for _, stmt := range copyStmts {
|
||||
// Check for context cancellation before starting new work
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
semaphore <- struct{}{} // Acquire worker slot
|
||||
select {
|
||||
case semaphore <- struct{}{}: // Acquire worker slot
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
atomic.StoreInt32(&cancelled, 1)
|
||||
break
|
||||
}
|
||||
|
||||
go func(s *SQLStatement) {
|
||||
defer wg.Done()
|
||||
defer func() { <-semaphore }() // Release worker slot
|
||||
|
||||
// Check cancellation before executing
|
||||
if ctx.Err() != nil || atomic.LoadInt32(&cancelled) == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := e.executeCopy(ctx, s)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
// Context cancelled, don't log as error
|
||||
return
|
||||
}
|
||||
if options.ContinueOnError {
|
||||
e.log.Warn("COPY failed", "table", s.TableName, "error", err)
|
||||
} else {
|
||||
@ -243,6 +287,12 @@ func (e *ParallelRestoreEngine) RestoreFile(ctx context.Context, filePath string
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Check if cancelled
|
||||
if ctx.Err() != nil {
|
||||
return result, ctx.Err()
|
||||
}
|
||||
|
||||
result.TablesRestored = completedCopies
|
||||
result.RowsRestored = totalRows
|
||||
|
||||
@ -264,15 +314,35 @@ func (e *ParallelRestoreEngine) RestoreFile(ctx context.Context, filePath string
|
||||
|
||||
// Execute post-data in parallel
|
||||
var completedPostData int64
|
||||
cancelled = 0 // Reset for phase 4
|
||||
for _, sql := range postDataStmts {
|
||||
// Check for context cancellation before starting new work
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
semaphore <- struct{}{}
|
||||
select {
|
||||
case semaphore <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
atomic.StoreInt32(&cancelled, 1)
|
||||
break
|
||||
}
|
||||
|
||||
go func(stmt string) {
|
||||
defer wg.Done()
|
||||
defer func() { <-semaphore }()
|
||||
|
||||
// Check cancellation before executing
|
||||
if ctx.Err() != nil || atomic.LoadInt32(&cancelled) == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
if err := e.executeStatement(ctx, stmt); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return // Context cancelled
|
||||
}
|
||||
if options.ContinueOnError {
|
||||
e.log.Warn("Post-data statement failed", "error", err)
|
||||
}
|
||||
@ -289,6 +359,11 @@ func (e *ParallelRestoreEngine) RestoreFile(ctx context.Context, filePath string
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Check if cancelled
|
||||
if ctx.Err() != nil {
|
||||
return result, ctx.Err()
|
||||
}
|
||||
|
||||
result.Duration = time.Since(startTime)
|
||||
e.log.Info("Parallel restore completed",
|
||||
"duration", result.Duration,
|
||||
|
||||
@ -635,7 +635,8 @@ func (e *Engine) restoreWithNativeEngine(ctx context.Context, archivePath, targe
|
||||
"database", targetDB,
|
||||
"archive", archivePath)
|
||||
|
||||
parallelEngine, err := native.NewParallelRestoreEngine(nativeCfg, e.log, parallelWorkers)
|
||||
// Pass context to ensure pool is properly closed on Ctrl+C cancellation
|
||||
parallelEngine, err := native.NewParallelRestoreEngineWithContext(ctx, nativeCfg, e.log, parallelWorkers)
|
||||
if err != nil {
|
||||
e.log.Warn("Failed to create parallel restore engine, falling back to sequential", "error", err)
|
||||
// Fall back to sequential restore
|
||||
@ -1342,9 +1343,14 @@ func (e *Engine) RestoreCluster(ctx context.Context, archivePath string, preExtr
|
||||
}
|
||||
|
||||
format := DetectArchiveFormat(archivePath)
|
||||
if format != FormatClusterTarGz {
|
||||
if !format.CanBeClusterRestore() {
|
||||
operation.Fail("Invalid cluster archive format")
|
||||
return fmt.Errorf("not a cluster archive: %s (detected format: %s)", archivePath, format)
|
||||
return fmt.Errorf("not a valid cluster restore format: %s (detected format: %s). Supported: .tar.gz, .sql, .sql.gz", archivePath, format)
|
||||
}
|
||||
|
||||
// For SQL-based cluster restores, use a different restore path
|
||||
if format == FormatPostgreSQLSQL || format == FormatPostgreSQLSQLGz {
|
||||
return e.restoreClusterFromSQL(ctx, archivePath, operation)
|
||||
}
|
||||
|
||||
// Check if we have a pre-extracted directory (optimization to avoid double extraction)
|
||||
@ -2177,6 +2183,45 @@ func (e *Engine) RestoreCluster(ctx context.Context, archivePath string, preExtr
|
||||
return nil
|
||||
}
|
||||
|
||||
// restoreClusterFromSQL restores a pg_dumpall SQL file using the native engine
|
||||
// This handles .sql and .sql.gz files containing full cluster dumps
|
||||
func (e *Engine) restoreClusterFromSQL(ctx context.Context, archivePath string, operation logger.OperationLogger) error {
|
||||
e.log.Info("Restoring cluster from SQL file (pg_dumpall format)",
|
||||
"file", filepath.Base(archivePath),
|
||||
"native_engine", true)
|
||||
|
||||
clusterStartTime := time.Now()
|
||||
|
||||
// Determine if compressed
|
||||
compressed := strings.HasSuffix(strings.ToLower(archivePath), ".gz")
|
||||
|
||||
// Use native engine to restore directly to postgres database (globals + all databases)
|
||||
e.log.Info("Restoring SQL dump using native engine...",
|
||||
"compressed", compressed,
|
||||
"size", FormatBytes(getFileSize(archivePath)))
|
||||
|
||||
e.progress.Start("Restoring cluster from SQL dump...")
|
||||
|
||||
// For pg_dumpall, we restore to the 'postgres' database which then creates other databases
|
||||
targetDB := "postgres"
|
||||
|
||||
err := e.restoreWithNativeEngine(ctx, archivePath, targetDB, compressed)
|
||||
if err != nil {
|
||||
operation.Fail(fmt.Sprintf("SQL cluster restore failed: %v", err))
|
||||
e.recordClusterRestoreMetrics(clusterStartTime, archivePath, 0, 0, false, err.Error())
|
||||
return fmt.Errorf("SQL cluster restore failed: %w", err)
|
||||
}
|
||||
|
||||
duration := time.Since(clusterStartTime)
|
||||
e.progress.Complete(fmt.Sprintf("Cluster restored successfully from SQL in %s", duration.Round(time.Second)))
|
||||
operation.Complete("SQL cluster restore completed")
|
||||
|
||||
// Record metrics
|
||||
e.recordClusterRestoreMetrics(clusterStartTime, archivePath, 1, 1, true, "")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// recordClusterRestoreMetrics records metrics for cluster restore operations
|
||||
func (e *Engine) recordClusterRestoreMetrics(startTime time.Time, archivePath string, totalDBs, successCount int, success bool, errorMsg string) {
|
||||
duration := time.Since(startTime)
|
||||
@ -2924,6 +2969,15 @@ func (e *Engine) isIgnorableError(errorMsg string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// getFileSize returns the size of a file, or 0 if it can't be read
|
||||
func getFileSize(path string) int64 {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return info.Size()
|
||||
}
|
||||
|
||||
// FormatBytes formats bytes to human readable format
|
||||
func FormatBytes(bytes int64) string {
|
||||
const unit = 1024
|
||||
|
||||
@ -168,11 +168,19 @@ func (f ArchiveFormat) IsCompressed() bool {
|
||||
f == FormatClusterTarGz
|
||||
}
|
||||
|
||||
// IsClusterBackup returns true if the archive is a cluster backup
|
||||
// IsClusterBackup returns true if the archive is a cluster backup (.tar.gz format created by dbbackup)
|
||||
func (f ArchiveFormat) IsClusterBackup() bool {
|
||||
return f == FormatClusterTarGz
|
||||
}
|
||||
|
||||
// CanBeClusterRestore returns true if the format can be used for cluster restore
|
||||
// This includes .tar.gz (dbbackup format) and .sql/.sql.gz (pg_dumpall format for native engine)
|
||||
func (f ArchiveFormat) CanBeClusterRestore() bool {
|
||||
return f == FormatClusterTarGz ||
|
||||
f == FormatPostgreSQLSQL ||
|
||||
f == FormatPostgreSQLSQLGz
|
||||
}
|
||||
|
||||
// IsPostgreSQL returns true if the archive is PostgreSQL format
|
||||
func (f ArchiveFormat) IsPostgreSQL() bool {
|
||||
return f == FormatPostgreSQLDump ||
|
||||
|
||||
@ -205,11 +205,20 @@ func (m ArchiveBrowserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return diagnoseView, diagnoseView.Init()
|
||||
}
|
||||
|
||||
// For restore-cluster mode:
|
||||
// - .tar.gz cluster archives → full cluster restore
|
||||
// - .sql/.sql.gz files → single database restore (Native Engine supports these)
|
||||
// - .dump/.dump.gz → single database restore (pg_restore)
|
||||
// ALL formats are now allowed for restore operations!
|
||||
// For restore-cluster mode: check if format can be used for cluster restore
|
||||
// - .tar.gz: dbbackup cluster format (works with pg_restore)
|
||||
// - .sql/.sql.gz: pg_dumpall format (works with native engine or psql)
|
||||
if m.mode == "restore-cluster" && !selected.Format.CanBeClusterRestore() {
|
||||
m.message = errorStyle.Render(fmt.Sprintf("⚠️ %s cannot be used for cluster restore.\n\n Supported formats: .tar.gz (dbbackup), .sql, .sql.gz (pg_dumpall)",
|
||||
selected.Name))
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// For SQL-based cluster restore, enable native engine automatically
|
||||
if m.mode == "restore-cluster" && !selected.Format.IsClusterBackup() {
|
||||
// This is a .sql or .sql.gz file - use native engine
|
||||
m.config.UseNativeEngine = true
|
||||
}
|
||||
|
||||
// For single restore mode with cluster backup selected - offer to select individual database
|
||||
if m.mode == "restore-single" && selected.Format.IsClusterBackup() {
|
||||
@ -217,7 +226,7 @@ func (m ArchiveBrowserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return clusterSelector, clusterSelector.Init()
|
||||
}
|
||||
|
||||
// Open restore preview for any valid format
|
||||
// Open restore preview for valid format
|
||||
preview := NewRestorePreview(m.config, m.logger, m.parent, m.ctx, selected, m.mode)
|
||||
return preview, preview.Init()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user