用 Git 备份文件夹

                     

贡献者: addis

  • 本文处于草稿阶段。
预备知识 Git 笔记

   以下 bash 程序可以把若干个文件夹中的每个都用 git 备份到 备份目录。运行完后,每个 文件夹* 会保存为 备份目录/文件夹*.git。这个其实就是 git 仓库中的 .git 文件夹(注意并不是 bare repo,不可以用于上游)。

   要注意的是,Git(尤其是 Windows 上)处理大的二进制文件的速度较慢。笔者在 Windows 上在两个 HDD 硬盘之间用该脚本备份,写入速度平均 12MiB/s。但笔者认为 Git 的丰富功能和灵活性、以及广泛的普及可以弥补这一不足。另见 bup(待研究)、git-annex(最新版本依赖于 symlink)以及 git-lfs(没有本地 repo)。更简单直接地,可以用 rsync 甚至 cp 备份

   备份脚本:用法 git-backup.sh 备份目录。所有子目录中,如果包含 .gitattributes,就会备份到 备份目录/子目录名.git(相当于正常使用 git 时的 备份目录/.git 文件夹)。

代码 1:git-backup.sh
#! /bin/bash

if (($# != 1)); then
  echo "error: must specify a dest path!" 1>&2
  exit
fi

dest="$1" # backup directory

for repo in */ ; do
  repo=${repo%/}
  if ! [ -f "$repo/.gitattributes" ]; then
    continue;
  fi

  printf "\n\n\n===============================\n"
  echo "$repo"
  printf "===============================\n\n\n"
  if ! [ -d "$repo" ]; then
    echo "error: directory not found!"
    continue
  fi
  gitdir="${dest}${repo}.git"
  dirflag="--work-tree=$repo --git-dir=$gitdir"
  echo "mkdir $gitdir ..."
  mkdir -p "$gitdir" &&
  echo "git init ..." &&
  git $dirflag init &&
  echo "git add -A ..." &&
  git $dirflag add -A --verbose &&
  # 其他有用的命令:
  echo "git commit ..."
  git $dirflag commit -m 'update'
  # echo "git status ..."
  # git $dirflag status
  # echo "git gc"
  # git $dirflag gc
done

  

未完成:如何批量检查变化?批量 commit?push?多重备份?


致读者: 小时百科一直以来坚持所有内容免费,这导致我们处于严重的亏损状态。 长此以往很可能会最终导致我们不得不选择大量广告以及内容付费等。 因此,我们请求广大读者热心打赏 ,使网站得以健康发展。 如果看到这条信息的每位读者能慷慨打赏 10 元,我们一个星期内就能脱离亏损, 并保证在接下来的一整年里向所有读者继续免费提供优质内容。 但遗憾的是只有不到 1% 的读者愿意捐款, 他们的付出帮助了 99% 的读者免费获取知识, 我们在此表示感谢。

                     

友情链接: 超理论坛 | ©小时科技 保留一切权利