@@ -163,52 +163,50 @@ func (b *PostgresBackup) doPostgresBackup() (string, error) {
163163 args = append (args , b .cfg .PostgresDatabase )
164164 }
165165
166- // Dump to a timestamped file in the temp directory
166+ // Dump to a timestamped gzip file in the temp directory
167167 ts := time .Now ().Format ("20060102-150405" )
168- outfile := fmt .Sprintf ("%s/%s-backup-%s.sql" , b .tempDir , b .cfg .PostgresDatabase , ts )
168+ outfile := fmt .Sprintf ("%s/%s-backup-%s.sql.gz" , b .tempDir , b .cfg .PostgresDatabase , ts )
169+
169170 f , err := os .Create (outfile )
170171 if err != nil {
171172 slog .Error ("Failed to create dump file" , "file" , outfile , "error" , err )
172173 return "" , err
173174 }
174- defer func (f * os.File ) {
175- err = f .Close ()
176- if err != nil {
177- slog .Error ("Failed to close dump file" , "file" , outfile , "error" , err )
175+
176+ // Create gzip writer for compression (pg_dump stdout -> gzip -> file)
177+ gzipWriter := gzip .NewWriter (f )
178+ defer func () {
179+ // Close gzip first to flush its footer/trailer into the underlying file
180+ if cerr := gzipWriter .Close (); cerr != nil {
181+ slog .Error ("Failed to close gzip writer" , "file" , outfile , "error" , cerr )
178182 }
179- }(f )
180- args = append (args , "-f" , outfile )
183+ if cerr := f .Close (); cerr != nil {
184+ slog .Error ("Failed to close dump file" , "file" , outfile , "error" , cerr )
185+ }
186+ }()
181187
182188 // Prepare command
183189 cmd := exec .Command ("pg_dump" , args ... )
184190 // Pass password via environment variable
185191 cmd .Env = append (os .Environ (), fmt .Sprintf ("PGPASSWORD=%s" , b .cfg .PostgresPassword ))
186192
187- // Create gzip writer for compression
188- gzipWriter := gzip .NewWriter (f )
189- defer func (gzipWriter * gzip.Writer ) {
190- err = gzipWriter .Close ()
191- if err != nil {
192- slog .Error ("Failed to close gzip writer" , "file" , outfile , "error" , err )
193- }
194- }(gzipWriter )
193+ // Stream pg_dump directly into gzip (Solution A)
194+ cmd .Stdout = gzipWriter
195195
196- var stdout bytes.Buffer
197- cmd .Stdout = & stdout
198196 var stderr bytes.Buffer
199197 cmd .Stderr = & stderr
200198
201199 // Redact sensitive data in logs
202200 logArgs := strings .Join (args , " " )
203- slog .Info ("Executing pg_dump with compression" , "args" , logArgs , "output" , outfile )
201+ slog .Info ("Executing pg_dump with compression" , "args" , logArgs , "output" , filepath . Base ( outfile ) )
204202
205203 if err = cmd .Run (); err != nil {
206204 // Remove partial file on failure
207205 _ = os .Remove (outfile )
208- slog .Error ("Failed to execute pg_dump" , "error" , err , "stderr" , stderr .String (), "stdout" , stdout .String ())
209- err = fmt .Errorf ("pg_dump failed %w" , err )
210- return "" , err
206+ slog .Error ("Failed to execute pg_dump" , "error" , err , "stderr" , stderr .String ())
207+ return "" , fmt .Errorf ("pg_dump failed %w" , err )
211208 }
209+
212210 return outfile , nil
213211}
214212
0 commit comments