Innei / Shiro

📜 A minimalist personal website embodying the purity of paper and freshness of snow.
https://innei.in
Other
3.47k stars 757 forks source link

你好,请问备份的时候会备份文章数据吗 #478

Closed liyown closed 3 weeks ago

liyown commented 1 month ago

Describe the bug

我看备份的文件很小,但是我在源码的里面看好像会吧mongoDB中的数据dump出来,然后打包在一起,但是数据大小对不上 image

备份逻辑

async backup() {
    const { backupOptions: configs } = await this.configs.waitForConfigReady()
    if (!configs.enable) {
      return
    }
    this.logger.log('--> 备份数据库中')
    // 用时间格式命名文件夹
    const dateDir = getMediumDateTime(new Date())

    const backupDirPath = join(BACKUP_DIR, dateDir)
    mkdirp.sync(backupDirPath)
    try {
      await $`mongodump --uri ${MONGO_DB.customConnectionString || MONGO_DB.uri} -d ${
        MONGO_DB.dbName
      }  ${flatten(
        excludeCollections.map((collection) => [
          '--excludeCollection',
          `${collection}`,
        ]),
      )} -o ${backupDirPath} >/dev/null 2>&1`
      // 打包 DB
      cd(backupDirPath)
      await $`mv ${MONGO_DB.dbName} mx-space`.quiet().nothrow()
      await $`zip -r backup-${dateDir} mx-space/* && rm -rf mx-space`.quiet()

      // 打包数据目录

      const flags = excludeFolders.flatMap((item) => ['--exclude', item])
      cd(DATA_DIR)
      await rm(join(DATA_DIR, 'backup_data'), { recursive: true, force: true })
      await rm(join(DATA_DIR, 'temp_copy_need'), {
        recursive: true,
        force: true,
      })

      await $`rsync -a . ./temp_copy_need --exclude temp_copy_need ${flags} && mv temp_copy_need backup_data && zip -r ${join(
        backupDirPath,
        `backup-${dateDir}`,
      )} ./backup_data && rm -rf backup_data`

      this.logger.log('--> 备份成功')
    } catch (error) {
      this.logger.error(
        `--> 备份失败,请确保已安装 zip 或 mongo-tools, mongo-tools 的版本需要与 mongod 版本一致,${error.message}` ||
          error.stderr,
      )
      throw error
    }
    const path = join(backupDirPath, `backup-${dateDir}.zip`)

    return {
      buffer: await readFile(path),
      path,
    }
  }

Reproduction

Validations

liyown commented 1 month ago

我看代码好像也没啥问题

Innei commented 3 weeks ago

当然会

liyown commented 3 weeks ago

但是数据里面好像只有表结构