updates to backups directory (#1099)

* read BackupDirectory from command line flag

* Change the default backup directory

* mkdir BackupDirectory

* use config for backup file path

* migrateDatabase to the backup directory

* use DoesFileExists

change permission on the directory to 0700

* declare err

* generate backupFile  where needed

* style fix

* more style fixes

* more style fixes
This commit is contained in:
Meisam 2021-06-14 00:28:59 +02:00 committed by GitHub
parent d9a6be2ac6
commit e991199735
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 4 deletions

View file

@ -11,8 +11,6 @@ const (
PrivateHLSStoragePath = "hls"
// FfmpegSuggestedVersion is the version of ffmpeg we suggest.
FfmpegSuggestedVersion = "v4.1.5" // Requires the v
// BackupDirectory is the directory we write backup files to.
BackupDirectory = "backup"
// DataDirectory is the directory we save data to.
DataDirectory = "data"
)
@ -20,4 +18,6 @@ const (
var (
// PublicHLSStoragePath is the directory we write public HLS files to for distribution.
PublicHLSStoragePath = filepath.Join(WebRoot, "hls")
// BackupDirectory is the directory we write backup files to.
BackupDirectory = filepath.Join(DataDirectory, "backup")
)

View file

@ -8,15 +8,16 @@ import (
"database/sql"
"fmt"
"os"
"path/filepath"
"time"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/utils"
log "github.com/sirupsen/logrus"
)
const (
schemaVersion = 0
backupFile = "backup/owncastdb.bak"
)
var _db *sql.DB
@ -95,6 +96,7 @@ func SetupPersistence(file string) error {
dbBackupTicker := time.NewTicker(1 * time.Hour)
go func() {
backupFile := filepath.Join(config.BackupDirectory, "owncastdb.bak")
for range dbBackupTicker.C {
utils.Backup(_db, backupFile)
}
@ -105,7 +107,8 @@ func SetupPersistence(file string) error {
func migrateDatabase(db *sql.DB, from, to int) error {
log.Printf("Migrating database from version %d to %d\n", from, to)
utils.Backup(db, fmt.Sprintf("backup/owncast-v%d.bak", from))
dbBackupFile := filepath.Join(config.BackupDirectory, fmt.Sprintf("owncast-v%d.bak", from))
utils.Backup(db, dbBackupFile)
for v := from; v < to; v++ {
switch v {
case 0:

View file

@ -35,6 +35,7 @@ func main() {
dbFile := flag.String("database", "", "Path to the database file.")
logDirectory := flag.String("logdir", "", "Directory where logs will be written to")
backupDirectory := flag.String("backupdir", "", "Directory where backups will be written to")
enableDebugOptions := flag.Bool("enableDebugFeatures", false, "Enable additional debugging options.")
enableVerboseLogging := flag.Bool("enableVerboseLogging", false, "Enable additional logging.")
restoreDatabaseFile := flag.String("restoreDatabase", "", "Restore an Owncast database backup")
@ -62,6 +63,10 @@ func main() {
configureLogging(*enableDebugOptions, *enableVerboseLogging)
log.Infoln(config.GetReleaseString())
if *backupDirectory != "" {
config.BackupDirectory = *backupDirectory
}
// Create the data directory if needed
if !utils.DoesFileExists("data") {
os.Mkdir("./data", 0700)

View file

@ -10,6 +10,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/schollz/sqlite3dump"
@ -59,6 +60,15 @@ func Restore(backupFile string, databaseFile string) error {
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.Fatalln(err)
}
}
// Dump the entire database as plain text sql
var b bytes.Buffer
out := bufio.NewWriter(&b)