diff --git a/app.go b/app.go index dd05c95..e942577 100644 --- a/app.go +++ b/app.go @@ -11,6 +11,7 @@ package writefreely import ( + "bytes" "crypto/tls" "database/sql" "fmt" @@ -30,6 +31,7 @@ import ( "github.com/gorilla/schema" "github.com/gorilla/sessions" "github.com/manifoldco/promptui" + "github.com/writeas/go-mysqldump" stripmd "github.com/writeas/go-strip-markdown" "github.com/writeas/impart" "github.com/writeas/web-core/auth" @@ -640,6 +642,17 @@ func CreateSchema(apper Apper) error { return nil } +func Dump(apper Apper) error { + apper.LoadConfig() + connectToDatabase(apper.App()) + defer shutdown(apper.App()) + err := adminDumpDatabase(apper.App()) + if err != nil { + return err + } + return nil +} + // Migrate runs all necessary database migrations. func Migrate(apper Apper) error { apper.LoadConfig() @@ -881,3 +894,22 @@ func adminInitDatabase(app *App) error { log.Info("Done.") return nil } + +func adminDumpDatabase(app *App) error { + if app.db.driverName != driverMySQL { + return fmt.Errorf("database dump only supports %s driver right now", driverMySQL) + } + + out := &bytes.Buffer{} + dumper, err := mysqldump.Register(app.db.DB, out) + if err != nil { + fmt.Println("Error registering database:", err) + return err + } + err = dumper.Dump() + if err != nil { + return err + } + fmt.Printf("%s", out) + return nil +} diff --git a/cmd/writefreely/main.go b/cmd/writefreely/main.go index 7fc2342..3217871 100644 --- a/cmd/writefreely/main.go +++ b/cmd/writefreely/main.go @@ -42,6 +42,7 @@ func main() { deleteUsername := flag.String("delete-user", "", "Delete a user with the given username") resetPassUser := flag.String("reset-pass", "", "Reset the given user's password") outputVersion := flag.Bool("v", false, "Output the current version") + dump := flag.Bool("dump", false, "Dump all database data (MySQL-only)") flag.Parse() app := writefreely.NewApp(*configFile) @@ -49,6 +50,13 @@ func main() { if *outputVersion { writefreely.OutputVersion() os.Exit(0) + } else if *dump { + err := writefreely.Dump(app) + if err != nil { + log.Error(err.Error()) + os.Exit(1) + } + os.Exit(0) } else if *createConfig { err := writefreely.CreateConfig(app) if err != nil { diff --git a/go.mod b/go.mod index 5da3da4..a3be95c 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/stretchr/testify v1.3.0 github.com/writeas/activity v0.1.2 github.com/writeas/activityserve v0.0.0-20191115095800-dd6d19cc8b89 + github.com/writeas/go-mysqldump v0.5.2-0.20200224191309-1bf29d35e962 github.com/writeas/go-strip-markdown v2.0.1+incompatible github.com/writeas/go-webfinger v0.0.0-20190106002315-85cf805c86d2 github.com/writeas/httpsig v1.0.0 diff --git a/go.sum b/go.sum index 2d433ec..90f415e 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ code.as/core/socks v1.0.0 h1:SPQXNp4SbEwjOAP9VzUahLHak8SDqy5n+9cm9tpjZOs= code.as/core/socks v1.0.0/go.mod h1:BAXBy5O9s2gmw6UxLqNJcVbWY7C/UPs+801CcSsfWOY= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/DATA-DOG/go-sqlmock v1.3.0 h1:ljjRxlddjfChBJdFKJs5LuCwCWPLaC1UZLwAo3PBBMk= +github.com/DATA-DOG/go-sqlmock v1.3.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/alecthomas/gometalinter v2.0.11+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk= github.com/alecthomas/gometalinter v3.0.0+incompatible h1:e9Zfvfytsw/e6Kd/PYd75wggK+/kX5Xn8IYDUKyc5fU= github.com/alecthomas/gometalinter v3.0.0+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk= @@ -132,6 +134,8 @@ github.com/writeas/activityserve v0.0.0-20191011072627-3a81f7784d5b h1:rd2wX/bTq github.com/writeas/activityserve v0.0.0-20191011072627-3a81f7784d5b/go.mod h1:Kz62mzYsCnrFTSTSFLXFj3fGYBQOntmBWTDDq57b46A= github.com/writeas/activityserve v0.0.0-20191115095800-dd6d19cc8b89 h1:NJhzq9aTccL3SSSZMrcnYhkD6sObdY9otNZ1X6/ZKNE= github.com/writeas/activityserve v0.0.0-20191115095800-dd6d19cc8b89/go.mod h1:Kz62mzYsCnrFTSTSFLXFj3fGYBQOntmBWTDDq57b46A= +github.com/writeas/go-mysqldump v0.5.2-0.20200224191309-1bf29d35e962 h1:UGcFuq/0VQ6y5uX37AFfCkwYTIg89S8B0d/R94q8DxE= +github.com/writeas/go-mysqldump v0.5.2-0.20200224191309-1bf29d35e962/go.mod h1:Zuj7/A1hkMz7k0B8arb9bjzMYgfhsfcxCfqE8SyF0Vk= github.com/writeas/go-strip-markdown v2.0.1+incompatible h1:IIqxTM5Jr7RzhigcL6FkrCNfXkvbR+Nbu1ls48pXYcw= github.com/writeas/go-strip-markdown v2.0.1+incompatible/go.mod h1:Rsyu10ZhbEK9pXdk8V6MVnZmTzRG0alMNLMwa0J01fE= github.com/writeas/go-webfinger v0.0.0-20190106002315-85cf805c86d2 h1:DUsp4OhdfI+e6iUqcPQlwx8QYXuUDsToTz/x82D3Zuo=