func pkgZip(root string, files []string) (path string, err error) { log.Info("package to zip:", path) tmpFile, err := utils.TempFile("files", "tmp-", "-"+filepath.Base(root)+".zip") if err != nil { return } z, err := zip.Create(tmpFile) if err != nil { return } for _, f := range files { var save string if f == "" { continue } // binary file use abspath //fmt.Println(root, f) if strings.HasSuffix(f, root) { save = f[len(root):] } else { save = filepath.Base(f) } info, er := os.Stat(f) if er != nil { continue } log.Debug("add", save, f) if info.IsDir() { if err = z.AddDir(save, f); err != nil { return } } else { if err = z.AddFile(save, f); err != nil { return } } } if err = z.Close(); err != nil { log.Error(err) return } return tmpFile, nil }
func DoBackup(app *GoInk.App) (string, error) { os.Mkdir(backupDir, os.ModePerm) filename := path.Join(backupDir, utils.DateTime(time.Now(), "YYYYMMDDHHmmss.zip")) z, e := zip.Create(filename) if e != nil { return "", e } root, _ := os.Getwd() z.AddDir("static", path.Join(root, "static")) z.AddDir("data", path.Join(root, "data")) z.AddDir(app.View().Dir, path.Join(root, app.View().Dir)) e = z.Flush() if e != nil { return "", e } println("backup success in " + filename) return filename, nil }
// DoBackup backups whole files to zip archive. // If withData is false, it compresses static files to zip archive without data files, config files and install lock file. func DoBackup(app *GoInk.App, withData bool) (string, error) { os.Mkdir(backupDir, os.ModePerm) // create zip file name from time unix filename := path.Join(backupDir, utils.DateTime(time.Now(), "YYYYMMDDHHmmss")) if withData { filename += ".zip" } else { filename += "_static.zip" } z, e := zip.Create(filename) if e != nil { return "", e } root, _ := os.Getwd() if withData { // if with data, add install lock file and config file lockFile := path.Join(root, "install.lock") if utils.IsFile(lockFile) { z.AddFile("install.lock", lockFile) } configFile := path.Join(root, "config.json") if utils.IsFile(configFile) { z.AddFile("config.json", configFile) } } z.AddDir("static/css", path.Join(root, "static", "css")) z.AddDir("static/img", path.Join(root, "static", "img")) z.AddDir("static/js", path.Join(root, "static", "js")) z.AddDir("static/lib", path.Join(root, "static", "lib")) z.AddFile("static/favicon.ico", path.Join(root, "static", "favicon.ico")) if withData { // if with data, backup data files and uploaded files z.AddDir("data", path.Join(root, "data")) z.AddDir("static/upload", path.Join(root, "static", "upload")) } z.AddDir(app.View().Dir, path.Join(root, app.View().Dir)) e = z.Flush() if e != nil { return "", e } println("backup success in " + filename) return filename, nil }
func runDump(ctx *cli.Context) { if ctx.IsSet("config") { setting.CustomConf = ctx.String("config") } setting.NewContext() models.LoadConfigs() models.SetEngine() log.Printf("Dumping local repositories...%s", setting.RepoRootPath) zip.Verbose = ctx.Bool("verbose") defer os.Remove("gogs-repo.zip") if err := zip.PackTo(setting.RepoRootPath, "gogs-repo.zip", true); err != nil { log.Fatalf("Fail to dump local repositories: %v", err) } log.Printf("Dumping database...") defer os.Remove("gogs-db.sql") if err := models.DumpDatabase("gogs-db.sql"); err != nil { log.Fatalf("Fail to dump database: %v", err) } fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix()) log.Printf("Packing dump files...") z, err := zip.Create(fileName) if err != nil { os.Remove(fileName) log.Fatalf("Fail to create %s: %v", fileName, err) } workDir, _ := setting.WorkDir() z.AddFile("gogs-repo.zip", path.Join(workDir, "gogs-repo.zip")) z.AddFile("gogs-db.sql", path.Join(workDir, "gogs-db.sql")) z.AddDir("custom", path.Join(workDir, "custom")) z.AddDir("log", path.Join(workDir, "log")) // FIXME: SSH key file. if err = z.Close(); err != nil { os.Remove(fileName) log.Fatalf("Fail to save %s: %v", fileName, err) } log.Println("Finish dumping!") }
// pack static asset and data // // in : *PackOption // out : { // "file":string // } // func (_ *PackApi) Pack(v interface{}) *Res { opt, ok := v.(*PackOption) if !ok { return Fail(paramTypeError(opt)) } zip.Verbose = false // create zip file name from time unix if opt.File == "" { opt.File = time.Now().Format("20060102150405.zip") } z, err := zip.Create(opt.File) if err != nil { return Fail(err) } root, err := os.Getwd() if err != nil { return Fail(err) } if opt.IsStaticAll { // pack all static files z.AddDir("static", path.Join(root, "static")) } else { // only pack default static files z.AddDir("static/admin", path.Join(root, "static", "admin")) z.AddDir("static/default", path.Join(root, "static", "default")) z.AddFile("static/sitemap.xsl", path.Join(root, "static", "sitemap.xsl")) } if opt.IsData { // pack data z.AddFile("config.toml", path.Join(root, "config.toml")) z.AddFile("purine.db", path.Join(root, "purine.db")) } if err = z.Flush(); err != nil { return Fail(err) } z.Close() return Success(map[string]interface{}{ "file": opt.File, }) }
func runDump(*cli.Context) { base.NewConfigContext() models.LoadModelsConfig() models.SetEngine() log.Printf("Dumping local repositories...%s", base.RepoRootPath) zip.Verbose = false defer os.Remove("gogs-repo.zip") if err := zip.PackTo(base.RepoRootPath, "gogs-repo.zip", true); err != nil { log.Fatalf("Fail to dump local repositories: %v", err) } log.Printf("Dumping database...") defer os.Remove("gogs-db.sql") if err := models.DumpDatabase("gogs-db.sql"); err != nil { log.Fatalf("Fail to dump database: %v", err) } fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix()) log.Printf("Packing dump files...") z, err := zip.Create(fileName) if err != nil { os.Remove(fileName) log.Fatalf("Fail to create %s: %v", fileName, err) } execDir, _ := base.ExecDir() z.AddFile("gogs-repo.zip", path.Join(execDir, "gogs-repo.zip")) z.AddFile("gogs-db.sql", path.Join(execDir, "gogs-db.sql")) z.AddFile("custom/conf/app.ini", path.Join(execDir, "custom/conf/app.ini")) z.AddDir("log", path.Join(execDir, "log")) if err = z.Close(); err != nil { os.Remove(fileName) log.Fatalf("Fail to save %s: %v", fileName, err) } log.Println("Finish dumping!") }
func runDump(*cli.Context) { base.NewConfigContext() log.Printf("Dumping local repositories...%s", base.RepoRootPath) zip.Verbose = false defer os.Remove("gogs-repo.zip") if err := zip.PackTo(base.RepoRootPath, "gogs-repo.zip", true); err != nil { log.Fatalf("Fail to dump local repositories: %v", err) } z, err := zip.Create("gogs-dump.zip") if err != nil { os.Remove("gogs-dump.zip") log.Fatalf("Fail to create gogs-dump.zip: %v", err) } execDir, _ := base.ExecDir() z.AddFile("gogs-repo.zip", path.Join(execDir, "gogs-repo.zip")) z.AddFile("custom/conf/app.ini", path.Join(execDir, "custom/conf/app.ini")) z.AddDir("log", path.Join(execDir, "log")) z.Close() log.Println("Finish dumping!") }
func runDump(ctx *cli.Context) { if ctx.IsSet("config") { setting.CustomConf = ctx.String("config") } setting.NewContext() models.LoadConfigs() models.SetEngine() TmpWorkDir, err := ioutil.TempDir(os.TempDir(), "gogs-dump-") if err != nil { log.Fatalf("Fail to create tmp work directory: %v", err) } log.Printf("Creating tmp work dir: %s", TmpWorkDir) reposDump := path.Join(TmpWorkDir, "gogs-repo.zip") dbDump := path.Join(TmpWorkDir, "gogs-db.sql") log.Printf("Dumping local repositories...%s", setting.RepoRootPath) zip.Verbose = ctx.Bool("verbose") if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil { log.Fatalf("Fail to dump local repositories: %v", err) } log.Printf("Dumping database...") if err := models.DumpDatabase(dbDump); err != nil { log.Fatalf("Fail to dump database: %v", err) } fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix()) log.Printf("Packing dump files...") z, err := zip.Create(fileName) if err != nil { os.Remove(fileName) log.Fatalf("Fail to create %s: %v", fileName, err) } if err := z.AddFile("gogs-repo.zip", reposDump); err != nil { log.Fatalf("Fail to include gogs-repo.zip: %v", err) } if err := z.AddFile("gogs-db.sql", dbDump); err != nil { log.Fatalf("Fail to include gogs-db.sql: %v", err) } customDir, err := os.Stat(setting.CustomPath) if err == nil && customDir.IsDir() { if err := z.AddDir("custom", setting.CustomPath); err != nil { log.Fatalf("Fail to include custom: %v", err) } } else { log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath) } if err := z.AddDir("log", setting.LogRootPath); err != nil { log.Fatalf("Fail to include log: %v", err) } // FIXME: SSH key file. if err = z.Close(); err != nil { os.Remove(fileName) log.Fatalf("Fail to save %s: %v", fileName, err) } log.Printf("Removing tmp work dir: %s", TmpWorkDir) os.RemoveAll(TmpWorkDir) log.Printf("Finish dumping in file %s", fileName) }