Check

The Go Code for dump and restore

The entire Go code with fake variables.

1. The Code


package main

import (
	"fmt"
	"os"
	"os/exec"
)

// Declare variables
var (
    pg_admin_path = "/Applications/pgAdmin4.app/Contents/SharedSupport/"
    pg_dump       = pg_admin_path + "pg_dump"
    pg_restore    = pg_admin_path + "pg_restore"
    pg_psql       = pg_admin_path + "psql"
                
    source_ip  = "94.237.x.1"
    source_db  = "tenant1"
    source_usr = "t1"
    source_pw  = "password"
                
    target_ip  = "94.237.x.2"
    target_db  = "tenant2"
    target_usr = "t2"
    target_pw  = "password"
                
    postgres_pw = "super_password"
)

func main() {
	create_file()
	create_user()
	create_db()
	dump_source()
	restore_target()
	switch_owner()
	if source_usr != target_usr && source_ip != target_ip {
		fmt.Println("New database - New owner for everyhing")
		switch_owner_everything()
	}
}

// Create a dump file.
func create_file() {
	fmt.Println("1. create file " + "dump.custom")
	dump, err := os.Create("dump.custom")

	if err != nil {
		fmt.Println("ERROR")
		fmt.Println(err.Error())
		return
	}
	defer dump.Close()

	err = dump.Chmod(0600)

	if err != nil {
		fmt.Println("## Error setting file permissions:", err.Error())
	}
}

// Create the target database user.

func create_user() {
	fmt.Println("2. create user " + target_usr)
	cmd := exec.Command(pg_psql,
		"--host", target_ip,
		"--port", "5432",
		"--username", "postgres",
		"--command", fmt.Sprintf("CREATE USER %s WITH PASSWORD '%s';", target_usr, target_pw))

	cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", postgres_pw))
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println(err.Error())
	}
}

// Create the target database.
func create_db() {
	fmt.Println("3. create database " + target_db)
	cmd := exec.Command(
		pg_psql,
		"--host", target_ip,
		"--port", "5432",
		"--username", "postgres",
		"--command", fmt.Sprintf("CREATE DATABASE \"%s\"", target_db))

	cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", postgres_pw))
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println(err.Error())
	}
}

// Command to execute pg_dump
func dump_source() {
	fmt.Println("4. Dump the old database (It may take VERY long time. Stay tuned...) " + source_db)
	cmd := exec.Command(
		pg_dump,
		"--host", source_ip,
		"--port", "5432",
		"--username", source_usr,
		"--dbname", source_db,
		"--format", "custom",
		"--verbose",
		"--file", "dump.custom",
	)

	cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", source_pw))
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("## Backup completed successfully")
	}
}

// Command to execute pg_restore to restore the dump file
func restore_target() {

	fmt.Println("5. Restore the database (It may take VERY long time. Stay tuned...) " + target_db)
	cmd := exec.Command(
		pg_restore,
		"--host", target_ip,
		"--port", "5432",
		"--username", "postgres",
		"--dbname", target_db,
		"--verbose",
		"/Users/mats/go/dump_orig/main/dump.custom",
	)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", postgres_pw))

	// Execute the command
	err := cmd.Run()
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("Restoration completed successfully")
	}
}

// Switch back to the original owner of the restored database.
func switch_owner() {
	fmt.Println("6. Change to original owner of database " + target_db)
	cmd := exec.Command(pg_psql,
		"--host", target_ip,
		"--port", "5432",
		"--username", "postgres",
		"--command", fmt.Sprintf("ALTER DATABASE \"%s\" OWNER TO %s;", target_db, target_usr))

	cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", postgres_pw))
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("Switch to original database owner successfully")
	}
}

// Change user for tables, views, functions, etc...
func switch_owner_everything() {
	fmt.Printf("7. Change owner of the entire database (tables, views, functions, etc")

	cmd := exec.Command(
		"sh", "-c",
		fmt.Sprintf(
			"%s --host %s --port 5432 --dbname %s --username postgres --command 'REASSIGN OWNED BY %s TO %s;'",
			pg_psql, target_ip, target_db, source_usr, target_usr,
		),
	)

	cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", postgres_pw))
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout

	err := cmd.Run()
	if err != nil {
		fmt.Println("Error executing command:", err)
	} else {
		fmt.Println("Ownership everywhere reassigned successfully")
	}
}