66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package backup
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
|
|
"git.nyne.dev/x/backup/config"
|
|
)
|
|
|
|
// BackupMariaDB backs up a MariaDB database using mariadb-dump
|
|
func BackupMariaDB(item config.Item, tempDir string) error {
|
|
if item.Type != "mariadb" {
|
|
return fmt.Errorf("item type is not mariadb")
|
|
}
|
|
|
|
// Create destination directory
|
|
destPath := filepath.Join(tempDir, item.Name)
|
|
if err := os.MkdirAll(destPath, 0755); err != nil {
|
|
return fmt.Errorf("failed to create destination directory: %w", err)
|
|
}
|
|
|
|
// Output file
|
|
outputFile := filepath.Join(destPath, fmt.Sprintf("%s.sql", item.Database))
|
|
|
|
// Build mariadb-dump command (MariaDB native dump tool)
|
|
args := []string{
|
|
fmt.Sprintf("--host=%s", item.Host),
|
|
fmt.Sprintf("--user=%s", item.User),
|
|
}
|
|
|
|
if item.Port > 0 {
|
|
args = append(args, fmt.Sprintf("--port=%d", item.Port))
|
|
}
|
|
|
|
if item.Password != "" {
|
|
args = append(args, fmt.Sprintf("--password=%s", item.Password))
|
|
}
|
|
|
|
args = append(args,
|
|
"--single-transaction",
|
|
"--quick",
|
|
"--lock-tables=false",
|
|
item.Database,
|
|
)
|
|
|
|
// Execute mariadb-dump (MariaDB native dump tool)
|
|
cmd := exec.Command("mariadb-dump", args...)
|
|
|
|
outFile, err := os.Create(outputFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create output file: %w", err)
|
|
}
|
|
defer outFile.Close()
|
|
|
|
cmd.Stdout = outFile
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return fmt.Errorf("mariadb-dump failed: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|