owncast/utils/backup.go
Gabe Kangas c6c6f0233d
Expanded linting + fix warnings (#1396)
* Expand the linters and types of warnings to improve consistency and safety

* Fail lint workflow if there are errors

* golint has been replaced by revive

* Hand-pick some of the default exclude list

* Ignore error when trying to delete preview gif

* Ignore linter warning opening playlist path

* Rename user field Id -> ID

* A bunch of renames to address linter warnings

* Rename ChatClient -> Client per linter suggestion best practice

* Rename ChatServer -> Server per linter suggestion best practice

* More linter warning fixes

* Add missing comments to all exported functions and properties
2021-09-12 00:18:15 -07:00

110 lines
2.4 KiB
Go

package utils
import (
"bufio"
"bytes"
"compress/gzip"
"database/sql"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/schollz/sqlite3dump"
log "github.com/sirupsen/logrus"
)
// Restore will attempt to restore the database using a specified backup file.
func Restore(backupFile string, databaseFile string) error {
log.Printf("Restoring database backup %s to %s", backupFile, databaseFile)
data, err := ioutil.ReadFile(backupFile) // nolint
if err != nil {
return fmt.Errorf("Unable to read backup file %s", err)
}
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("Unable to read backup file %s", err)
}
defer gz.Close()
var b bytes.Buffer
if _, err := io.Copy(&b, gz); err != nil { // nolint
return fmt.Errorf("Unable to read backup file %s", err)
}
defer gz.Close()
rawSQL := b.String()
if _, err := os.Create(databaseFile); err != nil {
return errors.New("Unable to write restored database")
}
// Create a new database by executing the raw SQL
db, err := sql.Open("sqlite3", databaseFile)
if err != nil {
return err
}
if _, err := db.Exec(rawSQL); err != nil {
return err
}
return nil
}
// Backup will backup the provided instance of the database to the specified file.
func Backup(db *sql.DB, backupFile string) {
log.Traceln("Backing up database to", backupFile)
backupDirectory := filepath.Dir(backupFile)
if !DoesFileExists(backupDirectory) {
err := os.MkdirAll(backupDirectory, 0700)
if err != nil {
log.Errorln("unable to create backup directory. check permissions and ownership.", backupDirectory, err)
return
}
}
// Dump the entire database as plain text sql
var b bytes.Buffer
out := bufio.NewWriter(&b)
if err := sqlite3dump.DumpDB(db, out); err != nil {
handleError(err)
return
}
_ = out.Flush()
// Create a new backup file
f, err := os.OpenFile(backupFile, os.O_WRONLY|os.O_CREATE, 0600) // nolint
if err != nil {
handleError(err)
return
}
// Create a gzip compression writer
w, err := gzip.NewWriterLevel(f, gzip.BestCompression)
if err != nil {
handleError(err)
return
}
// Write compressed data
if _, err := w.Write(b.Bytes()); err != nil {
handleError(err)
return
}
if err := w.Close(); err != nil {
handleError(err)
return
}
}
func handleError(err error) {
log.Errorln("unable to backup owncast database to file", err)
}