Browse Source

fix(backup): skip "data" in the custom directory (#7343)

Joe Chen 2 years ago
parent
commit
3a28168d41
3 changed files with 38 additions and 10 deletions
  1. 1 0
      CHANGELOG.md
  2. 35 6
      internal/cmd/backup.go
  3. 2 4
      internal/cmd/restore.go

+ 1 - 0
CHANGELOG.md

@@ -29,6 +29,7 @@ All notable changes to Gogs are documented in this file.
 
 - Unable to use LDAP authentication on ARM machines. [#6761](https://github.com/gogs/gogs/issues/6761)
 - Unable to choose "Lookup Avatar by mail" in user settings without deleting custom avatar. [#7267](https://github.com/gogs/gogs/pull/7267)
+- Mistakenly include the "data" directory under the custom directory in the Docker setup. [#7343](https://github.com/gogs/gogs/pull/7343)
 
 ### Removed
 

+ 35 - 6
internal/cmd/backup.go

@@ -101,15 +101,14 @@ func runBackup(c *cli.Context) error {
 		log.Fatal("Failed to include 'db': %v", err)
 	}
 
-	// Custom files
 	if !c.Bool("database-only") {
-		if err = z.AddDir(archiveRootDir+"/custom", conf.CustomDir()); err != nil {
-			log.Fatal("Failed to include 'custom': %v", err)
+		// Custom files
+		err = addCustomDirToBackup(z)
+		if err != nil {
+			log.Fatal("Failed to add custom directory to backup: %v", err)
 		}
-	}
 
-	// Data files
-	if !c.Bool("database-only") {
+		// Data files
 		for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
 			dirPath := filepath.Join(conf.Server.AppDataPath, dir)
 			if !com.IsDir(dirPath) {
@@ -166,3 +165,33 @@ func runBackup(c *cli.Context) error {
 	log.Stop()
 	return nil
 }
+
+func addCustomDirToBackup(z *zip.ZipArchive) error {
+	customDir := conf.CustomDir()
+	entries, err := os.ReadDir(customDir)
+	if err != nil {
+		return errors.Wrap(err, "list custom directory entries")
+	}
+
+	for _, e := range entries {
+		if e.Name() == "data" {
+			// Skip the "data" directory because it lives under the "custom" directory in
+			// the Docker setup and will be backed up separately.
+			log.Trace(`Skipping "data" directory in custom directory`)
+			continue
+		}
+
+		add := z.AddFile
+		if e.IsDir() {
+			add = z.AddDir
+		}
+		err = add(
+			fmt.Sprintf("%s/custom/%s", archiveRootDir, e.Name()),
+			filepath.Join(customDir, e.Name()),
+		)
+		if err != nil {
+			return errors.Wrapf(err, "add %q", e.Name())
+		}
+	}
+	return nil
+}

+ 2 - 4
internal/cmd/restore.go

@@ -119,8 +119,8 @@ func runRestore(c *cli.Context) error {
 		log.Fatal("Failed to import database: %v", err)
 	}
 
-	// Custom files
 	if !c.Bool("database-only") {
+		// Custom files
 		if osutil.IsDir(conf.CustomDir()) {
 			if err = os.Rename(conf.CustomDir(), conf.CustomDir()+".bak"); err != nil {
 				log.Fatal("Failed to backup current 'custom': %v", err)
@@ -129,10 +129,8 @@ func runRestore(c *cli.Context) error {
 		if err = os.Rename(filepath.Join(archivePath, "custom"), conf.CustomDir()); err != nil {
 			log.Fatal("Failed to import 'custom': %v", err)
 		}
-	}
 
-	// Data files
-	if !c.Bool("database-only") {
+		// Data files
 		_ = os.MkdirAll(conf.Server.AppDataPath, os.ModePerm)
 		for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
 			// Skip if backup archive does not have corresponding data