Sobes.tech
Junior — Middle

What mechanisms and tools are used for creating database backups?

sobes.tech AI

Answer from AI

The following mechanisms and tools are commonly used in Go for creating database backups:

  1. Database-level backup — using built-in commands or DBMS utilities (e.g., pg_dump for PostgreSQL, mysqldump for MySQL). In Go, these utilities can be executed via the os/exec package.

  2. File copying of the database — if the database is stored in files (e.g., SQLite), simply copying the database file as an archive.

  3. Using transactions and data export — programmatically exporting data from tables into JSON, CSV formats and saving them.

  4. Backup libraries and packages — there are third-party libraries that facilitate creating backups.

Example of running pg_dump from Go:

cmd := exec.Command("pg_dump", "-U", "user", "-F", "c", "-f", "backup.dump", "dbname")
cmd.Env = append(os.Environ(), "PGPASSWORD=yourpassword")
err := cmd.Run()
if err != nil {
    log.Fatal(err)
}

Thus, the choice of tool depends on the type of database and backup requirements.