Fix log , refactoring of code
This commit is contained in:
20
.github/workflows/release.yml
vendored
20
.github/workflows/release.yml
vendored
@@ -2,7 +2,7 @@ name: Release
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- v**
|
- v1.**
|
||||||
env:
|
env:
|
||||||
BUILDKIT_IMAGE: jkaninda/pg-bkup
|
BUILDKIT_IMAGE: jkaninda/pg-bkup
|
||||||
jobs:
|
jobs:
|
||||||
@@ -12,13 +12,13 @@ jobs:
|
|||||||
packages: write
|
packages: write
|
||||||
contents: read
|
contents: read
|
||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Set up QEMU
|
name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v3
|
||||||
-
|
-
|
||||||
name: Set up Docker Buildx
|
name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
-
|
-
|
||||||
name: Login to DockerHub
|
name: Login to DockerHub
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
@@ -30,6 +30,10 @@ jobs:
|
|||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
-
|
||||||
|
name: Get the tag name
|
||||||
|
id: get_tag_name
|
||||||
|
run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
|
||||||
-
|
-
|
||||||
name: Build and push
|
name: Build and push
|
||||||
uses: docker/build-push-action@v3
|
uses: docker/build-push-action@v3
|
||||||
@@ -38,8 +42,8 @@ jobs:
|
|||||||
file: "./docker/Dockerfile"
|
file: "./docker/Dockerfile"
|
||||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||||
tags: |
|
tags: |
|
||||||
"${{env.BUILDKIT_IMAGE}}:${{env.GITHUB_REF_NAME}}"
|
"${{env.BUILDKIT_IMAGE}}:${{ env.TAG_NAME }}"
|
||||||
"${{env.BUILDKIT_IMAGE}}:latest"
|
"${{env.BUILDKIT_IMAGE}}:latest"
|
||||||
"ghcr.io/${{env.BUILDKIT_IMAGE}}:${{env.GITHUB_REF_NAME}}"
|
"ghcr.io/${{env.BUILDKIT_IMAGE}}:${{ env.TAG_NAME }}"
|
||||||
"ghcr.io/${{env.BUILDKIT_IMAGE}}:latest"
|
"ghcr.io/${{env.BUILDKIT_IMAGE}}:latest"
|
||||||
|
|
||||||
|
|||||||
20
Makefile
20
Makefile
@@ -1,4 +1,6 @@
|
|||||||
BINARY_NAME=pg-bkup
|
BINARY_NAME=pg-bkup
|
||||||
|
IMAGE_NAME=jkaninda/pg-bkup
|
||||||
|
#IMAGE_NAME=ghcr.io/jkaninda/pg-bkup:v1.0
|
||||||
include .env
|
include .env
|
||||||
export
|
export
|
||||||
run:
|
run:
|
||||||
@@ -14,33 +16,33 @@ compile:
|
|||||||
GOOS=linux GOARCH=amd64 go build -o bin/${BINARY_NAME}-linux-amd64 .
|
GOOS=linux GOARCH=amd64 go build -o bin/${BINARY_NAME}-linux-amd64 .
|
||||||
|
|
||||||
docker-build:
|
docker-build:
|
||||||
docker build -f docker/Dockerfile -t jkaninda/pg-bkup:latest .
|
docker build -f docker/Dockerfile -t ${IMAGE_NAME}:latest .
|
||||||
|
|
||||||
docker-run: docker-build
|
docker-run: docker-build
|
||||||
docker run --rm --network internal --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup backup --prune --keep-last 2
|
docker run --rm --network web --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup backup --prune --keep-last 2
|
||||||
docker-restore: docker-build
|
docker-restore: docker-build
|
||||||
docker run --rm --network internal --user 1000:1000 --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup bkup restore -f ${FILE_NAME}
|
docker run --rm --network web --user 1000:1000 --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup restore -f ${FILE_NAME}
|
||||||
|
|
||||||
|
|
||||||
docker-run-scheduled: docker-build
|
docker-run-scheduled: docker-build
|
||||||
docker run --rm --network internal --user 1000:1000 --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup bkup backup --mode scheduled --period "* * * * *"
|
docker run --rm --network web --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup backup --mode scheduled --period "* * * * *"
|
||||||
|
|
||||||
|
|
||||||
docker-run-scheduled-s3: docker-build
|
docker-run-scheduled-s3: docker-build
|
||||||
docker run --rm --network internal --user 1000:1000 --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "ACCESS_KEY=${ACCESS_KEY}" -e "SECRET_KEY=${SECRET_KEY}" -e "BUCKET_NAME=${BUCKET_NAME}" -e "S3_ENDPOINT=${S3_ENDPOINT}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup bkup backup --storage s3 --mode scheduled --path /custom-path --period "* * * * *"
|
docker run --rm --network web --user 1000:1000 --name pg-bkup -v "./backup:/backup" -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "ACCESS_KEY=${ACCESS_KEY}" -e "SECRET_KEY=${SECRET_KEY}" -e "BUCKET_NAME=${BUCKET_NAME}" -e "S3_ENDPOINT=${S3_ENDPOINT}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup backup --storage s3 --mode scheduled --path /custom-path --period "* * * * *"
|
||||||
|
|
||||||
docker-run-s3: docker-build
|
docker-run-s3: docker-build
|
||||||
docker run --rm --network internal --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "ACCESS_KEY=${ACCESS_KEY}" -e "SECRET_KEY=${SECRET_KEY}" -e "AWS_S3_BUCKET_NAME=${AWS_S3_BUCKET_NAME}" -e "AWS_S3_ENDPOINT=${AWS_S3_ENDPOINT}" -e "AWS_REGION=eu2" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup bkup backup --storage s3 #--path /custom-path
|
docker run --rm --network web --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "ACCESS_KEY=${ACCESS_KEY}" -e "SECRET_KEY=${SECRET_KEY}" -e "AWS_S3_BUCKET_NAME=${AWS_S3_BUCKET_NAME}" -e "AWS_S3_ENDPOINT=${AWS_S3_ENDPOINT}" -e "AWS_REGION=eu2" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup backup --storage s3 #--path /custom-path
|
||||||
|
|
||||||
|
|
||||||
docker-restore-s3: docker-build
|
docker-restore-s3: docker-build
|
||||||
docker run --rm --network internal --privileged --device /dev/fuse --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "ACCESS_KEY=${ACCESS_KEY}" -e "SECRET_KEY=${SECRET_KEY}" -e "BUCKET_NAME=${AWS_S3_BUCKET_NAME}" -e "S3_ENDPOINT=${S3_ENDPOINT}" -e "AWS_REGION=eu2" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup bkup restore --storage s3 -f ${FILE_NAME} #--path /custom-path
|
docker run --rm --network web --privileged --device /dev/fuse --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "ACCESS_KEY=${ACCESS_KEY}" -e "SECRET_KEY=${SECRET_KEY}" -e "BUCKET_NAME=${AWS_S3_BUCKET_NAME}" -e "S3_ENDPOINT=${S3_ENDPOINT}" -e "AWS_REGION=eu2" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup restore --storage s3 -f ${FILE_NAME} #--path /custom-path
|
||||||
|
|
||||||
docker-run-ssh: docker-build
|
docker-run-ssh: docker-build
|
||||||
docker run --rm --network internal --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "SSH_USER=${SSH_USER}" -e "SSH_HOST_NAME=${SSH_HOST_NAME}" -e "SSH_REMOTE_PATH=${SSH_REMOTE_PATH}" -e "SSH_PASSWORD=${SSH_PASSWORD}" -e "SSH_PORT=${SSH_PORT}" -e "SSH_IDENTIFY_FILE=${SSH_IDENTIFY_FILE}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" jkaninda/pg-bkup bkup backup --storage ssh
|
docker run --rm --network web --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "SSH_USER=${SSH_USER}" -e "SSH_HOST_NAME=${SSH_HOST_NAME}" -e "SSH_REMOTE_PATH=${SSH_REMOTE_PATH}" -e "SSH_PASSWORD=${SSH_PASSWORD}" -e "SSH_PORT=${SSH_PORT}" -e "SSH_IDENTIFY_FILE=${SSH_IDENTIFY_FILE}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" ${IMAGE_NAME} bkup backup --storage ssh
|
||||||
|
|
||||||
docker-restore-ssh: docker-build
|
docker-restore-ssh: docker-build
|
||||||
docker run --rm --network internal --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "SSH_USER=${SSH_USER}" -e "SSH_HOST_NAME=${SSH_HOST_NAME}" -e "SSH_REMOTE_PATH=${SSH_REMOTE_PATH}" -e "SSH_PASSWORD=${SSH_PASSWORD}" -e "SSH_PORT=${SSH_PORT}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" -e "SSH_IDENTIFY_FILE=${SSH_IDENTIFY_FILE}" jkaninda/pg-bkup bkup restore --storage ssh -f data_20240731_200104.sql.gz.gpg
|
docker run --rm --network web --name pg-bkup -e "DB_HOST=${DB_HOST}" -e "DB_NAME=${DB_NAME}" -e "DB_USERNAME=${DB_USERNAME}" -e "DB_PASSWORD=${DB_PASSWORD}" -e "SSH_USER=${SSH_USER}" -e "SSH_HOST_NAME=${SSH_HOST_NAME}" -e "SSH_REMOTE_PATH=${SSH_REMOTE_PATH}" -e "SSH_PASSWORD=${SSH_PASSWORD}" -e "SSH_PORT=${SSH_PORT}" -e "GPG_PASSPHRASE=${GPG_PASSPHRASE}" -e "SSH_IDENTIFY_FILE=${SSH_IDENTIFY_FILE}" ${IMAGE_NAME} bkup restore --storage ssh -f data_20240731_200104.sql.gz.gpg
|
||||||
|
|
||||||
run-docs:
|
run-docs:
|
||||||
cd docs && bundle exec jekyll serve -H 0.0.0.0 -t
|
cd docs && bundle exec jekyll serve -H 0.0.0.0 -t
|
||||||
2
go.mod
2
go.mod
@@ -13,8 +13,8 @@ require (
|
|||||||
github.com/hpcloud/tail v1.0.0 // indirect
|
github.com/hpcloud/tail v1.0.0 // indirect
|
||||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
golang.org/x/crypto v0.18.0 // indirect
|
golang.org/x/crypto v0.18.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
|
|
||||||
golang.org/x/sys v0.22.0 // indirect
|
golang.org/x/sys v0.22.0 // indirect
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
150
pkg/backup.go
150
pkg/backup.go
@@ -55,7 +55,7 @@ func StartBackup(cmd *cobra.Command) {
|
|||||||
case "ssh", "remote":
|
case "ssh", "remote":
|
||||||
sshBackup(backupFileName, remotePath, disableCompression, prune, backupRetention, encryption)
|
sshBackup(backupFileName, remotePath, disableCompression, prune, backupRetention, encryption)
|
||||||
case "ftp":
|
case "ftp":
|
||||||
utils.Fatalf("Not supported storage type: %s", storage)
|
utils.Fatal("Not supported storage type: %s", storage)
|
||||||
default:
|
default:
|
||||||
localBackup(backupFileName, disableCompression, prune, backupRetention, encryption)
|
localBackup(backupFileName, disableCompression, prune, backupRetention, encryption)
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ func scheduledMode() {
|
|||||||
fmt.Println(" Starting PostgreSQL Bkup... ")
|
fmt.Println(" Starting PostgreSQL Bkup... ")
|
||||||
fmt.Println("***********************************")
|
fmt.Println("***********************************")
|
||||||
utils.Info("Running in Scheduled mode")
|
utils.Info("Running in Scheduled mode")
|
||||||
utils.Info("Execution period ", os.Getenv("SCHEDULE_PERIOD"))
|
utils.Info("Execution period %s ", os.Getenv("SCHEDULE_PERIOD"))
|
||||||
|
|
||||||
//Test database connexion
|
//Test database connexion
|
||||||
utils.TestDatabaseConnection()
|
utils.TestDatabaseConnection()
|
||||||
@@ -93,6 +93,7 @@ func scheduledMode() {
|
|||||||
utils.Fatal("Failed to start supervisord: %v", err)
|
utils.Fatal("Failed to start supervisord: %v", err)
|
||||||
}
|
}
|
||||||
utils.Info("Backup job started")
|
utils.Info("Backup job started")
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := cmd.Process.Kill(); err != nil {
|
if err := cmd.Process.Kill(); err != nil {
|
||||||
utils.Info("Failed to kill supervisord process: %v", err)
|
utils.Info("Failed to kill supervisord process: %v", err)
|
||||||
@@ -105,13 +106,14 @@ func scheduledMode() {
|
|||||||
}
|
}
|
||||||
t, err := tail.TailFile(cronLogFile, tail.Config{Follow: true})
|
t, err := tail.TailFile(cronLogFile, tail.Config{Follow: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Failed to tail file: %v", err)
|
utils.Fatal("Failed to tail file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and print new lines from the log file
|
// Read and print new lines from the log file
|
||||||
for line := range t.Lines {
|
for line := range t.Lines {
|
||||||
fmt.Println(line.Text)
|
fmt.Println(line.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackupDatabase backup database
|
// BackupDatabase backup database
|
||||||
@@ -124,73 +126,75 @@ func BackupDatabase(backupFileName string, disableCompression bool) {
|
|||||||
storagePath = os.Getenv("STORAGE_PATH")
|
storagePath = os.Getenv("STORAGE_PATH")
|
||||||
|
|
||||||
utils.Info("Starting database backup...")
|
utils.Info("Starting database backup...")
|
||||||
if os.Getenv("DB_HOST") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_USERNAME") == "" || os.Getenv("DB_PASSWORD") == "" {
|
|
||||||
utils.Fatal("Please make sure all required environment variables for database are set")
|
err := utils.CheckEnvVars(dbHVars)
|
||||||
} else {
|
if err != nil {
|
||||||
err := os.Setenv("PGPASSWORD", dbPassword)
|
utils.Error("Please make sure all required environment variables for database are set")
|
||||||
|
utils.Fatal("Error checking environment variables: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Setenv("PGPASSWORD", dbPassword)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
utils.TestDatabaseConnection()
|
||||||
|
// Backup Database database
|
||||||
|
utils.Info("Backing up database...")
|
||||||
|
|
||||||
|
// Verify is compression is disabled
|
||||||
|
if disableCompression {
|
||||||
|
// Execute pg_dump
|
||||||
|
cmd := exec.Command("pg_dump",
|
||||||
|
"-h", dbHost,
|
||||||
|
"-p", dbPort,
|
||||||
|
"-U", dbUserName,
|
||||||
|
"-d", dbName,
|
||||||
|
)
|
||||||
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
utils.TestDatabaseConnection()
|
// save output
|
||||||
// Backup Database database
|
file, err := os.Create(fmt.Sprintf("%s/%s", tmpPath, backupFileName))
|
||||||
utils.Info("Backing up database...")
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
// Verify is compression is disabled
|
}
|
||||||
if disableCompression {
|
defer file.Close()
|
||||||
// Execute pg_dump
|
|
||||||
cmd := exec.Command("pg_dump",
|
_, err = file.Write(output)
|
||||||
"-h", dbHost,
|
if err != nil {
|
||||||
"-p", dbPort,
|
log.Fatal(err)
|
||||||
"-U", dbUserName,
|
}
|
||||||
"-d", dbName,
|
|
||||||
)
|
} else {
|
||||||
output, err := cmd.Output()
|
// Execute pg_dump
|
||||||
if err != nil {
|
cmd := exec.Command("pg_dump",
|
||||||
log.Fatal(err)
|
"-h", dbHost,
|
||||||
}
|
"-p", dbPort,
|
||||||
// save output
|
"-U", dbUserName,
|
||||||
file, err := os.Create(fmt.Sprintf("%s/%s", tmpPath, backupFileName))
|
"-d", dbName,
|
||||||
if err != nil {
|
)
|
||||||
log.Fatal(err)
|
stdout, err := cmd.StdoutPipe()
|
||||||
}
|
if err != nil {
|
||||||
defer file.Close()
|
log.Fatal(err)
|
||||||
|
}
|
||||||
_, err = file.Write(output)
|
gzipCmd := exec.Command("gzip")
|
||||||
if err != nil {
|
gzipCmd.Stdin = stdout
|
||||||
log.Fatal(err)
|
// save output
|
||||||
}
|
gzipCmd.Stdout, err = os.Create(fmt.Sprintf("%s/%s", tmpPath, backupFileName))
|
||||||
|
gzipCmd.Start()
|
||||||
} else {
|
if err != nil {
|
||||||
// Execute pg_dump
|
log.Fatal(err)
|
||||||
cmd := exec.Command("pg_dump",
|
}
|
||||||
"-h", dbHost,
|
if err := cmd.Run(); err != nil {
|
||||||
"-p", dbPort,
|
log.Fatal(err)
|
||||||
"-U", dbUserName,
|
}
|
||||||
"-d", dbName,
|
if err := gzipCmd.Wait(); err != nil {
|
||||||
)
|
log.Fatal(err)
|
||||||
stdout, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
gzipCmd := exec.Command("gzip")
|
|
||||||
gzipCmd.Stdin = stdout
|
|
||||||
// save output
|
|
||||||
gzipCmd.Stdout, err = os.Create(fmt.Sprintf("%s/%s", tmpPath, backupFileName))
|
|
||||||
gzipCmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := gzipCmd.Wait(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
utils.Info("Database has been backed up")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
utils.Info("Database has been backed up")
|
||||||
|
|
||||||
}
|
}
|
||||||
func localBackup(backupFileName string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
|
func localBackup(backupFileName string, disableCompression bool, prune bool, backupRetention int, encrypt bool) {
|
||||||
@@ -201,7 +205,7 @@ func localBackup(backupFileName string, disableCompression bool, prune bool, bac
|
|||||||
encryptBackup(backupFileName)
|
encryptBackup(backupFileName)
|
||||||
finalFileName = fmt.Sprintf("%s.%s", backupFileName, gpgExtension)
|
finalFileName = fmt.Sprintf("%s.%s", backupFileName, gpgExtension)
|
||||||
}
|
}
|
||||||
utils.Info("Backup name is ", finalFileName)
|
utils.Info("Backup name is %s", finalFileName)
|
||||||
moveToBackup(finalFileName, storagePath)
|
moveToBackup(finalFileName, storagePath)
|
||||||
//Delete old backup
|
//Delete old backup
|
||||||
if prune {
|
if prune {
|
||||||
@@ -220,24 +224,24 @@ func s3Backup(backupFileName string, s3Path string, disableCompression bool, pru
|
|||||||
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
|
finalFileName = fmt.Sprintf("%s.%s", backupFileName, "gpg")
|
||||||
}
|
}
|
||||||
utils.Info("Uploading backup file to S3 storage...")
|
utils.Info("Uploading backup file to S3 storage...")
|
||||||
utils.Info("Backup name is ", finalFileName)
|
utils.Info("Backup name is %s", finalFileName)
|
||||||
err := utils.UploadFileToS3(tmpPath, finalFileName, bucket, s3Path)
|
err := utils.UploadFileToS3(tmpPath, finalFileName, bucket, s3Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Error uploading file to S3: %s ", err)
|
utils.Fatal("Error uploading file to S3: %s ", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Delete backup file from tmp folder
|
//Delete backup file from tmp folder
|
||||||
err = utils.DeleteFile(filepath.Join(tmpPath, backupFileName))
|
err = utils.DeleteFile(filepath.Join(tmpPath, backupFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error deleting file:", err)
|
fmt.Println("Error deleting file: ", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
// Delete old backup
|
// Delete old backup
|
||||||
if prune {
|
if prune {
|
||||||
err := utils.DeleteOldBackup(bucket, s3Path, backupRetention)
|
err := utils.DeleteOldBackup(bucket, s3Path, backupRetention)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Error deleting old backup from S3: %s ", err)
|
utils.Fatal("Error deleting old backup from S3: %s ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
utils.Done("Database has been backed up and uploaded to s3 ")
|
utils.Done("Database has been backed up and uploaded to s3 ")
|
||||||
@@ -255,14 +259,14 @@ func sshBackup(backupFileName, remotePath string, disableCompression bool, prune
|
|||||||
utils.Info("Backup name is ", backupFileName)
|
utils.Info("Backup name is ", backupFileName)
|
||||||
err := CopyToRemote(finalFileName, remotePath)
|
err := CopyToRemote(finalFileName, remotePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Error uploading file to the remote server: %s ", err)
|
utils.Fatal("Error uploading file to the remote server: %s ", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Delete backup file from tmp folder
|
//Delete backup file from tmp folder
|
||||||
err = utils.DeleteFile(filepath.Join(tmpPath, finalFileName))
|
err = utils.DeleteFile(filepath.Join(tmpPath, finalFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error deleting file:", err)
|
utils.Error("Error deleting file:", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
if prune {
|
if prune {
|
||||||
@@ -278,7 +282,7 @@ func encryptBackup(backupFileName string) {
|
|||||||
gpgPassphrase := os.Getenv("GPG_PASSPHRASE")
|
gpgPassphrase := os.Getenv("GPG_PASSPHRASE")
|
||||||
err := Encrypt(filepath.Join(tmpPath, backupFileName), gpgPassphrase)
|
err := Encrypt(filepath.Join(tmpPath, backupFileName), gpgPassphrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Error during encrypting backup %s", err)
|
utils.Fatal("Error during encrypting backup %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/jkaninda/pg-bkup/utils"
|
"github.com/jkaninda/pg-bkup/utils"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -9,14 +8,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Decrypt(inputFile string, passphrase string) error {
|
func Decrypt(inputFile string, passphrase string) error {
|
||||||
utils.Info("Decrypting backup file: " + inputFile + " ...")
|
utils.Info("Decrypting backup file: %s...", inputFile)
|
||||||
cmd := exec.Command("gpg", "--batch", "--passphrase", passphrase, "--output", RemoveLastExtension(inputFile), "--decrypt", inputFile)
|
cmd := exec.Command("gpg", "--batch", "--passphrase", passphrase, "--output", RemoveLastExtension(inputFile), "--decrypt", inputFile)
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +30,6 @@ func Encrypt(inputFile string, passphrase string) error {
|
|||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/jkaninda/pg-bkup/utils"
|
"github.com/jkaninda/pg-bkup/utils"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -12,7 +11,7 @@ func copyToTmp(sourcePath string, backupFileName string) {
|
|||||||
//Copy backup from storage to /tmp
|
//Copy backup from storage to /tmp
|
||||||
err := utils.CopyFile(filepath.Join(sourcePath, backupFileName), filepath.Join(tmpPath, backupFileName))
|
err := utils.CopyFile(filepath.Join(sourcePath, backupFileName), filepath.Join(tmpPath, backupFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error copying file ", backupFileName, err)
|
utils.Fatal("Error copying file %s %v", backupFileName, err)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,16 +19,16 @@ func moveToBackup(backupFileName string, destinationPath string) {
|
|||||||
//Copy backup from tmp folder to storage destination
|
//Copy backup from tmp folder to storage destination
|
||||||
err := utils.CopyFile(filepath.Join(tmpPath, backupFileName), filepath.Join(destinationPath, backupFileName))
|
err := utils.CopyFile(filepath.Join(tmpPath, backupFileName), filepath.Join(destinationPath, backupFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error copying file ", backupFileName, err)
|
utils.Fatal("Error copying file %s %v", backupFileName, err)
|
||||||
|
|
||||||
}
|
}
|
||||||
//Delete backup file from tmp folder
|
//Delete backup file from tmp folder
|
||||||
err = utils.DeleteFile(filepath.Join(tmpPath, backupFileName))
|
err = utils.DeleteFile(filepath.Join(tmpPath, backupFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error deleting file:", err)
|
utils.Error("Error deleting file: %s", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
utils.Done("Database has been backed up and copied to ", filepath.Join(destinationPath, backupFileName))
|
utils.Done("Database has been backed up and copied to %s", filepath.Join(destinationPath, backupFileName))
|
||||||
}
|
}
|
||||||
func deleteOldBackup(retentionDays int) {
|
func deleteOldBackup(retentionDays int) {
|
||||||
utils.Info("Deleting old backups...")
|
utils.Info("Deleting old backups...")
|
||||||
@@ -44,7 +43,7 @@ func deleteOldBackup(retentionDays int) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error:", err)
|
utils.Fatal("Error:", err)
|
||||||
} else {
|
} else {
|
||||||
utils.Done("File ", filePath, " deleted successfully")
|
utils.Done("File %s deleted successfully", filePath)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func StartRestore(cmd *cobra.Command) {
|
|||||||
case "ssh":
|
case "ssh":
|
||||||
restoreFromRemote(file, remotePath)
|
restoreFromRemote(file, remotePath)
|
||||||
case "ftp":
|
case "ftp":
|
||||||
utils.Fatalf("Restore from FTP is not yet supported")
|
utils.Fatal("Restore from FTP is not yet supported")
|
||||||
default:
|
default:
|
||||||
utils.Info("Restore database from local")
|
utils.Info("Restore database from local")
|
||||||
RestoreDatabase(file)
|
RestoreDatabase(file)
|
||||||
@@ -44,7 +44,7 @@ func restoreFromS3(file, bucket, s3Path string) {
|
|||||||
utils.Info("Restore database from s3")
|
utils.Info("Restore database from s3")
|
||||||
err := utils.DownloadFile(tmpPath, file, bucket, s3Path)
|
err := utils.DownloadFile(tmpPath, file, bucket, s3Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error download file from s3 ", file, err)
|
utils.Fatal("Error download file from s3 %s %v ", file, err)
|
||||||
}
|
}
|
||||||
RestoreDatabase(file)
|
RestoreDatabase(file)
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ func restoreFromRemote(file, remotePath string) {
|
|||||||
utils.Info("Restore database from remote server")
|
utils.Info("Restore database from remote server")
|
||||||
err := CopyFromRemote(file, remotePath)
|
err := CopyFromRemote(file, remotePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error download file from remote server: ", filepath.Join(remotePath, file), err)
|
utils.Fatal("Error download file from remote server: %s %v", filepath.Join(remotePath, file), err)
|
||||||
}
|
}
|
||||||
RestoreDatabase(file)
|
RestoreDatabase(file)
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func RestoreDatabase(file string) {
|
|||||||
//Decrypt file
|
//Decrypt file
|
||||||
err := Decrypt(filepath.Join(tmpPath, file), gpgPassphrase)
|
err := Decrypt(filepath.Join(tmpPath, file), gpgPassphrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatal("Error decrypting file ", file, err)
|
utils.Fatal("Error decrypting file %s %v", file, err)
|
||||||
}
|
}
|
||||||
//Update file name
|
//Update file name
|
||||||
file = RemoveLastExtension(file)
|
file = RemoveLastExtension(file)
|
||||||
@@ -85,42 +85,43 @@ func RestoreDatabase(file string) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.Getenv("DB_HOST") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_USERNAME") == "" || os.Getenv("DB_PASSWORD") == "" || file == "" {
|
err := utils.CheckEnvVars(dbHVars)
|
||||||
utils.Fatal("Please make sure all required environment variables are set")
|
if err != nil {
|
||||||
} else {
|
utils.Error("Please make sure all required environment variables for database are set")
|
||||||
|
utils.Fatal("Error checking environment variables: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if utils.FileExists(fmt.Sprintf("%s/%s", tmpPath, file)) {
|
if utils.FileExists(fmt.Sprintf("%s/%s", tmpPath, file)) {
|
||||||
|
|
||||||
err := os.Setenv("PGPASSWORD", dbPassword)
|
err := os.Setenv("PGPASSWORD", dbPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
|
||||||
utils.TestDatabaseConnection()
|
|
||||||
|
|
||||||
extension := filepath.Ext(fmt.Sprintf("%s/%s", tmpPath, file))
|
|
||||||
// Restore from compressed file / .sql.gz
|
|
||||||
if extension == ".gz" {
|
|
||||||
str := "zcat " + fmt.Sprintf("%s/%s", tmpPath, file) + " | psql -h " + os.Getenv("DB_HOST") + " -p " + os.Getenv("DB_PORT") + " -U " + os.Getenv("DB_USERNAME") + " -v -d " + os.Getenv("DB_NAME")
|
|
||||||
_, err := exec.Command("bash", "-c", str).Output()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error, in restoring the database ", err)
|
|
||||||
}
|
|
||||||
utils.Done("Database has been restored")
|
|
||||||
|
|
||||||
} else if extension == ".sql" {
|
|
||||||
//Restore from sql file
|
|
||||||
str := "cat " + fmt.Sprintf("%s/%s", tmpPath, file) + " | psql -h " + os.Getenv("DB_HOST") + " -p " + os.Getenv("DB_PORT") + " -U " + os.Getenv("DB_USERNAME") + " -v -d " + os.Getenv("DB_NAME")
|
|
||||||
_, err := exec.Command("bash", "-c", str).Output()
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatal("Error in restoring the database", err)
|
|
||||||
}
|
|
||||||
utils.Done("Database has been restored")
|
|
||||||
} else {
|
|
||||||
utils.Fatal("Unknown file extension ", extension)
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
utils.Fatal("File not found in ", fmt.Sprintf("%s/%s", tmpPath, file))
|
|
||||||
}
|
}
|
||||||
|
utils.TestDatabaseConnection()
|
||||||
|
|
||||||
|
extension := filepath.Ext(fmt.Sprintf("%s/%s", tmpPath, file))
|
||||||
|
// Restore from compressed file / .sql.gz
|
||||||
|
if extension == ".gz" {
|
||||||
|
str := "zcat " + fmt.Sprintf("%s/%s", tmpPath, file) + " | psql -h " + os.Getenv("DB_HOST") + " -p " + os.Getenv("DB_PORT") + " -U " + os.Getenv("DB_USERNAME") + " -v -d " + os.Getenv("DB_NAME")
|
||||||
|
_, err := exec.Command("bash", "-c", str).Output()
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal("Error, in restoring the database %v", err)
|
||||||
|
}
|
||||||
|
utils.Done("Database has been restored")
|
||||||
|
|
||||||
|
} else if extension == ".sql" {
|
||||||
|
//Restore from sql file
|
||||||
|
str := "cat " + fmt.Sprintf("%s/%s", tmpPath, file) + " | psql -h " + os.Getenv("DB_HOST") + " -p " + os.Getenv("DB_PORT") + " -U " + os.Getenv("DB_USERNAME") + " -v -d " + os.Getenv("DB_NAME")
|
||||||
|
_, err := exec.Command("bash", "-c", str).Output()
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal("Error in restoring the database %v", err)
|
||||||
|
}
|
||||||
|
utils.Done("Database has been restored")
|
||||||
|
} else {
|
||||||
|
utils.Fatal("Unknown file extension: %s", extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
utils.Fatal("File not found in %s", fmt.Sprintf("%s/%s", tmpPath, file))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
25
pkg/scp.go
25
pkg/scp.go
@@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/bramvdbogaerde/go-scp/auth"
|
"github.com/bramvdbogaerde/go-scp/auth"
|
||||||
"github.com/jkaninda/pg-bkup/utils"
|
"github.com/jkaninda/pg-bkup/utils"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"golang.org/x/exp/slog"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@@ -20,15 +19,20 @@ func CopyToRemote(fileName, remotePath string) error {
|
|||||||
sshPort := os.Getenv("SSH_PORT")
|
sshPort := os.Getenv("SSH_PORT")
|
||||||
sshIdentifyFile := os.Getenv("SSH_IDENTIFY_FILE")
|
sshIdentifyFile := os.Getenv("SSH_IDENTIFY_FILE")
|
||||||
|
|
||||||
|
err := utils.CheckEnvVars(sshVars)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal(fmt.Sprintf("Error checking environment variables\n: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
clientConfig, _ := auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
clientConfig, _ := auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
||||||
if sshIdentifyFile != "" && utils.FileExists(sshIdentifyFile) {
|
if sshIdentifyFile != "" && utils.FileExists(sshIdentifyFile) {
|
||||||
clientConfig, _ = auth.PrivateKey(sshUser, sshIdentifyFile, ssh.InsecureIgnoreHostKey())
|
clientConfig, _ = auth.PrivateKey(sshUser, sshIdentifyFile, ssh.InsecureIgnoreHostKey())
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if sshPassword == "" {
|
if sshPassword == "" {
|
||||||
return errors.New("SSH_PASSWORD environment variable is required if SSH_IDENTIFY_FILE is empty\n")
|
return errors.New("SSH_PASSWORD environment variable is required if SSH_IDENTIFY_FILE is empty")
|
||||||
}
|
}
|
||||||
slog.Warn("Accessing the remote server using password, password is not recommended\n")
|
utils.Warn("Accessing the remote server using password, password is not recommended")
|
||||||
clientConfig, _ = auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
clientConfig, _ = auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -36,9 +40,9 @@ func CopyToRemote(fileName, remotePath string) error {
|
|||||||
client := scp.NewClient(fmt.Sprintf("%s:%s", sshHostName, sshPort), &clientConfig)
|
client := scp.NewClient(fmt.Sprintf("%s:%s", sshHostName, sshPort), &clientConfig)
|
||||||
|
|
||||||
// Connect to the remote server
|
// Connect to the remote server
|
||||||
err := client.Connect()
|
err = client.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Couldn't establish a connection to the remote server\n")
|
return errors.New("couldn't establish a connection to the remote server")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open a file
|
// Open a file
|
||||||
@@ -64,6 +68,11 @@ func CopyFromRemote(fileName, remotePath string) error {
|
|||||||
sshPort := os.Getenv("SSH_PORT")
|
sshPort := os.Getenv("SSH_PORT")
|
||||||
sshIdentifyFile := os.Getenv("SSH_IDENTIFY_FILE")
|
sshIdentifyFile := os.Getenv("SSH_IDENTIFY_FILE")
|
||||||
|
|
||||||
|
err := utils.CheckEnvVars(sshVars)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatal("Error checking environment variables\n: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
clientConfig, _ := auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
clientConfig, _ := auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
||||||
if sshIdentifyFile != "" && utils.FileExists(sshIdentifyFile) {
|
if sshIdentifyFile != "" && utils.FileExists(sshIdentifyFile) {
|
||||||
clientConfig, _ = auth.PrivateKey(sshUser, sshIdentifyFile, ssh.InsecureIgnoreHostKey())
|
clientConfig, _ = auth.PrivateKey(sshUser, sshIdentifyFile, ssh.InsecureIgnoreHostKey())
|
||||||
@@ -72,7 +81,7 @@ func CopyFromRemote(fileName, remotePath string) error {
|
|||||||
if sshPassword == "" {
|
if sshPassword == "" {
|
||||||
return errors.New("SSH_PASSWORD environment variable is required if SSH_IDENTIFY_FILE is empty\n")
|
return errors.New("SSH_PASSWORD environment variable is required if SSH_IDENTIFY_FILE is empty\n")
|
||||||
}
|
}
|
||||||
slog.Warn("Accessing the remote server using password, password is not recommended\n")
|
utils.Warn("Accessing the remote server using password, password is not recommended\n")
|
||||||
clientConfig, _ = auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
clientConfig, _ = auth.PasswordKey(sshUser, sshPassword, ssh.InsecureIgnoreHostKey())
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -80,7 +89,7 @@ func CopyFromRemote(fileName, remotePath string) error {
|
|||||||
client := scp.NewClient(fmt.Sprintf("%s:%s", sshHostName, sshPort), &clientConfig)
|
client := scp.NewClient(fmt.Sprintf("%s:%s", sshHostName, sshPort), &clientConfig)
|
||||||
|
|
||||||
// Connect to the remote server
|
// Connect to the remote server
|
||||||
err := client.Connect()
|
err = client.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Couldn't establish a connection to the remote server\n")
|
return errors.New("Couldn't establish a connection to the remote server\n")
|
||||||
}
|
}
|
||||||
@@ -96,7 +105,7 @@ func CopyFromRemote(fileName, remotePath string) error {
|
|||||||
err = client.CopyFromRemote(context.Background(), file, filepath.Join(remotePath, fileName))
|
err = client.CopyFromRemote(context.Background(), file, filepath.Join(remotePath, fileName))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error while copying file ", err)
|
utils.Error("Error while copying file %s ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func CreateCrontabScript(disableCompression bool, storage string) {
|
|||||||
//task := "/usr/local/bin/backup_cron.sh"
|
//task := "/usr/local/bin/backup_cron.sh"
|
||||||
touchCmd := exec.Command("touch", backupCronFile)
|
touchCmd := exec.Command("touch", backupCronFile)
|
||||||
if err := touchCmd.Run(); err != nil {
|
if err := touchCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error creating file %s: %v\n", backupCronFile, err)
|
utils.Fatal("Error creating file %s: %v\n", backupCronFile, err)
|
||||||
}
|
}
|
||||||
var disableC = ""
|
var disableC = ""
|
||||||
if disableCompression {
|
if disableCompression {
|
||||||
@@ -37,36 +37,36 @@ bkup backup --dbname %s --port %s %v
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.WriteToFile(backupCronFile, scriptContent); err != nil {
|
if err := utils.WriteToFile(backupCronFile, scriptContent); err != nil {
|
||||||
utils.Fatalf("Error writing to %s: %v\n", backupCronFile, err)
|
utils.Fatal("Error writing to %s: %v\n", backupCronFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
chmodCmd := exec.Command("chmod", "+x", "/usr/local/bin/backup_cron.sh")
|
chmodCmd := exec.Command("chmod", "+x", "/usr/local/bin/backup_cron.sh")
|
||||||
if err := chmodCmd.Run(); err != nil {
|
if err := chmodCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error changing permissions of %s: %v\n", backupCronFile, err)
|
utils.Fatal("Error changing permissions of %s: %v\n", backupCronFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lnCmd := exec.Command("ln", "-s", "/usr/local/bin/backup_cron.sh", "/usr/local/bin/backup_cron")
|
lnCmd := exec.Command("ln", "-s", "/usr/local/bin/backup_cron.sh", "/usr/local/bin/backup_cron")
|
||||||
if err := lnCmd.Run(); err != nil {
|
if err := lnCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error creating symbolic link: %v\n", err)
|
utils.Fatal("Error creating symbolic link: %v\n", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
touchLogCmd := exec.Command("touch", cronLogFile)
|
touchLogCmd := exec.Command("touch", cronLogFile)
|
||||||
if err := touchLogCmd.Run(); err != nil {
|
if err := touchLogCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error creating file %s: %v\n", cronLogFile, err)
|
utils.Fatal("Error creating file %s: %v\n", cronLogFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cronJob := "/etc/cron.d/backup_cron"
|
cronJob := "/etc/cron.d/backup_cron"
|
||||||
touchCronCmd := exec.Command("touch", cronJob)
|
touchCronCmd := exec.Command("touch", cronJob)
|
||||||
if err := touchCronCmd.Run(); err != nil {
|
if err := touchCronCmd.Run(); err != nil {
|
||||||
utils.Fatalf("Error creating file %s: %v\n", cronJob, err)
|
utils.Fatal("Error creating file %s: %v\n", cronJob, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> %s"
|
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> %s"
|
||||||
`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile)
|
`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile)
|
||||||
|
|
||||||
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
|
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
|
||||||
utils.Fatalf("Error writing to %s: %v\n", cronJob, err)
|
utils.Fatal("Error writing to %s: %v\n", cronJob, err)
|
||||||
}
|
}
|
||||||
utils.ChangePermission("/etc/cron.d/backup_cron", 0644)
|
utils.ChangePermission("/etc/cron.d/backup_cron", 0644)
|
||||||
|
|
||||||
|
|||||||
16
pkg/var.go
16
pkg/var.go
@@ -21,3 +21,19 @@ var (
|
|||||||
disableCompression = false
|
disableCompression = false
|
||||||
encryption = false
|
encryption = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// dbHVars Required environment variables for database
|
||||||
|
var dbHVars = []string{
|
||||||
|
"DB_HOST",
|
||||||
|
"DB_PASSWORD",
|
||||||
|
"DB_USERNAME",
|
||||||
|
"DB_NAME",
|
||||||
|
}
|
||||||
|
|
||||||
|
// sshVars Required environment variables for SSH remote server storage
|
||||||
|
var sshVars = []string{
|
||||||
|
"SSH_USER",
|
||||||
|
"SSH_REMOTE_PATH",
|
||||||
|
"SSH_HOST_NAME",
|
||||||
|
"SSH_PORT",
|
||||||
|
}
|
||||||
|
|||||||
58
utils/logger.go
Normal file
58
utils/logger.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var currentTime = time.Now().Format("2006/01/02 15:04:05")
|
||||||
|
|
||||||
|
// Info message
|
||||||
|
func Info(msg string, args ...any) {
|
||||||
|
formattedMessage := fmt.Sprintf(msg, args...)
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Printf("%s INFO: %s\n", currentTime, msg)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s INFO: %s\n", currentTime, formattedMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn Warning message
|
||||||
|
func Warn(msg string, args ...any) {
|
||||||
|
formattedMessage := fmt.Sprintf(msg, args...)
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Printf("%s WARN: %s\n", currentTime, msg)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s WARN: %s\n", currentTime, formattedMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error error message
|
||||||
|
func Error(msg string, args ...any) {
|
||||||
|
formattedMessage := fmt.Sprintf(msg, args...)
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Printf("%s ERROR: %s\n", currentTime, msg)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func Done(msg string, args ...any) {
|
||||||
|
formattedMessage := fmt.Sprintf(msg, args...)
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Printf("%s INFO: %s\n", currentTime, msg)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s INFO: %s\n", currentTime, formattedMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatal(msg string, args ...any) {
|
||||||
|
// Fatal logs an error message and exits the program.
|
||||||
|
formattedMessage := fmt.Sprintf(msg, args...)
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Printf("%s ERROR: %s\n", currentTime, msg)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
35
utils/s3.go
35
utils/s3.go
@@ -2,13 +2,11 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/s3"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -19,13 +17,32 @@ import (
|
|||||||
// CreateSession creates a new AWS session
|
// CreateSession creates a new AWS session
|
||||||
func CreateSession() (*session.Session, error) {
|
func CreateSession() (*session.Session, error) {
|
||||||
|
|
||||||
|
// AwsVars Required environment variables for AWS S3 storage
|
||||||
|
var awsVars = []string{
|
||||||
|
"AWS_S3_ENDPOINT",
|
||||||
|
"AWS_S3_BUCKET_NAME",
|
||||||
|
"AWS_ACCESS_KEY",
|
||||||
|
"AWS_SECRET_KEY",
|
||||||
|
"AWS_REGION",
|
||||||
|
"AWS_REGION",
|
||||||
|
"AWS_REGION",
|
||||||
|
}
|
||||||
|
|
||||||
endPoint := GetEnvVariable("AWS_S3_ENDPOINT", "S3_ENDPOINT")
|
endPoint := GetEnvVariable("AWS_S3_ENDPOINT", "S3_ENDPOINT")
|
||||||
accessKey := GetEnvVariable("AWS_ACCESS_KEY", "ACCESS_KEY")
|
accessKey := GetEnvVariable("AWS_ACCESS_KEY", "ACCESS_KEY")
|
||||||
secretKey := GetEnvVariable("AWS_SECRET_KEY", "SECRET_KEY")
|
secretKey := GetEnvVariable("AWS_SECRET_KEY", "SECRET_KEY")
|
||||||
|
_ = GetEnvVariable("AWS_S3_BUCKET_NAME", "BUCKET_NAME")
|
||||||
|
|
||||||
region := os.Getenv("AWS_REGION")
|
region := os.Getenv("AWS_REGION")
|
||||||
awsDisableSsl, err := strconv.ParseBool(os.Getenv("AWS_DISABLE_SSL"))
|
awsDisableSsl, err := strconv.ParseBool(os.Getenv("AWS_DISABLE_SSL"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatalf("Unable to parse AWS_DISABLE_SSL env var: %s", err)
|
Fatal("Unable to parse AWS_DISABLE_SSL env var: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = CheckEnvVars(awsVars)
|
||||||
|
if err != nil {
|
||||||
|
Error("Error checking environment variables\n: %s", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
// Configure to use MinIO Server
|
// Configure to use MinIO Server
|
||||||
s3Config := &aws.Config{
|
s3Config := &aws.Config{
|
||||||
@@ -88,7 +105,7 @@ func DownloadFile(destinationPath, key, bucket, prefix string) error {
|
|||||||
Info("Download backup from S3 storage...")
|
Info("Download backup from S3 storage...")
|
||||||
file, err := os.Create(filepath.Join(destinationPath, key))
|
file, err := os.Create(filepath.Join(destinationPath, key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to create file", err)
|
Error("Failed to create file", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
@@ -102,7 +119,7 @@ func DownloadFile(destinationPath, key, bucket, prefix string) error {
|
|||||||
Key: aws.String(objectKey),
|
Key: aws.String(objectKey),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to download file", err)
|
Error("Failed to download file", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
Info("Backup downloaded: ", file.Name(), " bytes size ", numBytes)
|
Info("Backup downloaded: ", file.Name(), " bytes size ", numBytes)
|
||||||
@@ -135,18 +152,18 @@ func DeleteOldBackup(bucket, prefix string, retention int) error {
|
|||||||
Key: object.Key,
|
Key: object.Key,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to delete object %s: %v", *object.Key, err)
|
Info("Failed to delete object %s: %v", *object.Key, err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Deleted object %s\n", *object.Key)
|
Info("Deleted object %s\n", *object.Key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return !lastPage
|
return !lastPage
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to list objects: %v", err)
|
Error("Failed to list objects: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Finished deleting old files.")
|
Info("Finished deleting old files.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,31 +10,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/exp/slog"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Info(v ...any) {
|
|
||||||
fmt.Println("⒤ ", fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
func Worn(msg string, v ...any) {
|
|
||||||
slog.Warn(fmt.Sprintf(msg, v))
|
|
||||||
}
|
|
||||||
func Done(v ...any) {
|
|
||||||
fmt.Println("✔ ", fmt.Sprint(v...))
|
|
||||||
}
|
|
||||||
func Fatal(v ...any) {
|
|
||||||
fmt.Println("✘ ", fmt.Sprint(v...))
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
func Fatalf(msg string, v ...any) {
|
|
||||||
fmt.Printf("✘ "+msg, v...)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FileExists(filename string) bool {
|
func FileExists(filename string) bool {
|
||||||
info, err := os.Stat(filename)
|
info, err := os.Stat(filename)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -91,7 +72,7 @@ func CopyFile(src, dst string) error {
|
|||||||
}
|
}
|
||||||
func ChangePermission(filePath string, mod int) {
|
func ChangePermission(filePath string, mod int) {
|
||||||
if err := os.Chmod(filePath, fs.FileMode(mod)); err != nil {
|
if err := os.Chmod(filePath, fs.FileMode(mod)); err != nil {
|
||||||
Fatalf("Error changing permissions of %s: %v\n", filePath, err)
|
Fatal("Error changing permissions of %s: %v\n", filePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -145,7 +126,7 @@ func TestDatabaseConnection() {
|
|||||||
// Run the command and capture any errors
|
// Run the command and capture any errors
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error running psql command: %v\nOutput: %s\n", err, out.String())
|
Error("Error running psql command: %v\nOutput: %s\n", err, out.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Info("Successfully connected to database")
|
Info("Successfully connected to database")
|
||||||
@@ -187,11 +168,29 @@ func GetEnvVariable(envName, oldEnvName string) string {
|
|||||||
if value == "" {
|
if value == "" {
|
||||||
value = os.Getenv(oldEnvName)
|
value = os.Getenv(oldEnvName)
|
||||||
if value != "" {
|
if value != "" {
|
||||||
slog.Warn(fmt.Sprintf("%s is deprecated, please use %s instead!\n", oldEnvName, envName))
|
err := os.Setenv(envName, value)
|
||||||
|
if err != nil {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
Warn("%s is deprecated, please use %s instead! ", oldEnvName, envName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
func ShowHistory() {
|
|
||||||
|
// CheckEnvVars checks if all the specified environment variables are set
|
||||||
|
func CheckEnvVars(vars []string) error {
|
||||||
|
missingVars := []string{}
|
||||||
|
|
||||||
|
for _, v := range vars {
|
||||||
|
if os.Getenv(v) == "" {
|
||||||
|
missingVars = append(missingVars, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(missingVars) > 0 {
|
||||||
|
return fmt.Errorf("missing environment variables: %v", missingVars)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user