我将git用作Windows系统的备份,它非常有用。在文章的底部,我显示了用于在Windows系统上配置的脚本。将git用作任何系统的备份有两个主要优点:
- 与商业解决方案通常使用自己专有的格式不同,您的备份采用开放源代码格式,该格式得到了广泛的支持,并且记录充分。这使您可以完全控制数据。很容易看到哪些文件以及何时更改。如果要截断历史记录,也可以这样做。是否想抹掉您的历史记录?没问题。取回文件的版本与任何git命令一样简单。
- 您可以根据需要设置任意数量的镜像,并且所有镜像都可以具有自定义的备份时间。您将获得本地镜像,该镜像不受Internet流量缓慢的负担,从而使您(1)整天可以执行更频繁的备份,并且(2)可以快速恢复。(频繁备份是一个巨大的优势,因为我发现丢失文档的时间最多是用户错误。例如,您的孩子不小心覆盖了他过去5个小时一直在处理的文档。)但是,您会得到远程镜像,可以在发生本地灾难或盗窃时提供数据保护的优势。并且假设您要在自定义时间备份远程镜像以节省Internet带宽?没问题。
底线:git备份为您提供了控制备份方式的强大功能。
我在Windows系统上进行了配置。第一步是创建本地git repo,您将在其中提交所有本地数据。我建议使用本地第二块硬盘驱动器,但是可以使用同一块硬盘驱动器(但是,您可以将其推到远程某个地方,否则,如果硬盘驱动器死了,则将其拧紧。)
您首先需要安装cygwin(使用rsync),还需要为Windows安装git:http : //git-scm.com/download/win
接下来,创建本地git repo(仅运行一次):
init-repo.bat:
@echo off
REM SCRIPT PURPOSE: CREATE YOUR LOCAL GIT-REPO (RUN ONLY ONCE)
REM Set where the git repository will be stored
SET GBKUP_LOCAL_MIRROR_HOME=E:\backup\mirror
REM Create the backup git repo.
SET GIT_PARAMS=--git-dir=%GBKUP_LOCAL_MIRROR_HOME%\.git --work-tree=%GBKUP_LOCAL_MIRROR_HOME%
mkdir %GBKUP_LOCAL_MIRROR_HOME%
git %GIT_PARAMS% init
git %GIT_PARAMS% config core.autocrlf false
git %GIT_PARAMS% config core.ignorecase false
git %GIT_PARAMS% config core.fileMode false
git %GIT_PARAMS% config user.email backup@yourComputerName
git %GIT_PARAMS% config user.name backup
REM add a remote to the git repo. Make sure you have set myRemoteServer in ~/.ssh/config
REM The path on the remote server will vary. Our remote server is a Windows machine running cygwin+ssh.
REM For better security, you could install gitolite on the remote server, and forbid any non-fast-forward merges, and thus stop a malicious user from overwriting your backups.
git %GIT_PARAMS% remote add origin myRemoteServer:/cygdrive/c/backup/yourComputerName.git
REM treat all files as binary; so you don't have to worry about autocrlf changing your line endings
SET ATTRIBUTES_FILE=%GBKUP_LOCAL_MIRROR_HOME%\.git\info\attributes
echo.>> %ATTRIBUTES_FILE%
echo *.gbkuptest text>> %ATTRIBUTES_FILE%
echo * binary>> %ATTRIBUTES_FILE%
REM compression is often a waste of time with binary files
echo * -delta>> %ATTRIBUTES_FILE%
REM You may need to get rid of windows new lines. We use cygwin's tool
C:\cygwin64\bin\dos2unix %ATTRIBUTES_FILE%
接下来,我们有我们的备份脚本包装器,Windows Scheduler会定期调用它:
gbackup.vbs:
' A simple vbs wrapper to run your bat file in the background
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c C:\opt\gbackup\gbackup.bat"
oShell.Run strArgs, 0, false
接下来,我们有包装程序调用的备份脚本本身:
gbackup.bat:
@echo off
REM Set where the git repository will be stored
SET GBKUP_LOCAL_MIRROR_HOME=E:\backup\mirror
REM the user which runs the scheduler
SET GBKUP_RUN_AS_USER=yourWindowsUserName
REM exclude file
SET GBKUP_EXCLUDE_FILE=/cygdrive/c/opt/gbackup/exclude-from.txt
SET GBKUP_TMP_GIT_DIR_NAME=git-renamed
for /f "delims=" %%i in ('C:\cygwin64\bin\cygpath %GBKUP_LOCAL_MIRROR_HOME%') do set GBKUP_LOCAL_MIRROR_CYGWIN=%%i
REM rename any .git directories as they were (see below command)
for /r %GBKUP_LOCAL_MIRROR_HOME% %%i in (%GBKUP_TMP_GIT_DIR_NAME%) do ren "%%i" ".git" 2> nul
SET RSYNC_CMD_BASE=C:\cygwin64\bin\rsync -ahv --progress --delete --exclude-from %GBKUP_EXCLUDE_FILE%
REM rsync all needed directories to local mirror
%RSYNC_CMD_BASE% /cygdrive/c/dev %GBKUP_LOCAL_MIRROR_CYGWIN%
%RSYNC_CMD_BASE% /cygdrive/c/Users/asmith %GBKUP_LOCAL_MIRROR_CYGWIN%
%RSYNC_CMD_BASE% /cygdrive/c/Users/bsmith %GBKUP_LOCAL_MIRROR_CYGWIN%
cacls %GBKUP_LOCAL_MIRROR_HOME% /t /e /p %GBKUP_RUN_AS_USER%:f
REM rename any .git directories as git will ignore the entire directory, except the main one
for /r %GBKUP_LOCAL_MIRROR_HOME% %%i in (.git) do ren "%%i" "%GBKUP_TMP_GIT_DIR_NAME%" 2> nul
ren %GBKUP_LOCAL_MIRROR_HOME%\%GBKUP_TMP_GIT_DIR_NAME% .git
REM finally commit to git
SET GIT_PARAMS=--git-dir=%GBKUP_LOCAL_MIRROR_HOME%\.git --work-tree=%GBKUP_LOCAL_MIRROR_HOME%
SET BKUP_LOG_FILE=%TMP%\git-backup.log
SET TO_LOG=1^>^> %BKUP_LOG_FILE% 2^>^&1
echo ===========================BACKUP START=========================== %TO_LOG%
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime% %TO_LOG%
echo updating git index, committing, and then pushing to remote %TO_LOG%
REM Caution: The --ignore-errors directive tells git to continue even if it can't access a file.
git %GIT_PARAMS% add -Av --ignore-errors %TO_LOG%
git %GIT_PARAMS% commit -m "backup" %TO_LOG%
git %GIT_PARAMS% push -vv --progress origin master %TO_LOG%
echo ===========================BACKUP END=========================== %TO_LOG%
我们有exclude-from.txt文件,在其中将所有文件都忽略:
exclude-from.txt:
target/
logs/
AppData/
Downloads/
trash/
temp/
.idea/
.m2/
.IntelliJIdea14/
OLD/
Searches/
Videos/
NTUSER.DAT*
ntuser.dat*
您需要转到任何远程存储库,并对它们执行“ git init --bare”。您可以通过执行备份脚本来测试脚本。假设一切正常,请转到Windows Scheduler,然后将每小时备份指向vbs文件。之后,您将获得每小时一台计算机的git历史记录。这非常方便-每个人都意外删除了一部分文本并错过了吗?只需检查您的git存储库即可。