From e991199735a8a842047e2e3b55535c0d54d0873f Mon Sep 17 00:00:00 2001 From: Meisam <39205857+MFTabriz@users.noreply.github.com> Date: Mon, 14 Jun 2021 00:28:59 +0200 Subject: [PATCH] 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 --- config/constants.go | 4 ++-- core/data/data.go | 7 +++++-- main.go | 5 +++++ utils/backup.go | 10 ++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/config/constants.go b/config/constants.go index 6008aeed1..09f0b9954 100644 --- a/config/constants.go +++ b/config/constants.go @@ -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") ) diff --git a/core/data/data.go b/core/data/data.go index 74321dbd2..b2484c30c 100644 --- a/core/data/data.go +++ b/core/data/data.go @@ -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: diff --git a/main.go b/main.go index 1568be5e9..40078bd1c 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/utils/backup.go b/utils/backup.go index b030e3952..19edde1ae 100644 --- a/utils/backup.go +++ b/utils/backup.go @@ -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)