在服务器上,安装 git
cd /
git init
git add .
git commit -a -m "Yes, this is server"
Run Code Online (Sandbox Code Playgroud)
然后/.git/
指向网络驱动器(SAN、NFS、Samba 等)或其他磁盘。每小时/每天等使用 cron 作业来更新更改。.git 目录将包含所有服务器文件的版本副本(不包括无用/复杂的文件,如 /proc、/dev 等)
对于不重要的开发服务器,我不希望在适当的备份系统上设置它的麻烦/成本,并且备份只是为了方便(即我们不需要备份该服务器,但它会节省如果出现问题,有一段时间),这可能是一个有效的备份解决方案,还是会倒在一大堆便便中?
lar*_*sks 100
你不是傻子。使用git
作为备份机制可以是有吸引力的,尽管有什么其他人说,git
工程的二进制文件就好了。阅读Git Book 中的此页面以获取有关此主题的更多信息。基本上,因为git
没有使用增量存储机制,它并没有真正关心什么文件看起来像(但效用git diff
是相当低的二进制文件与股票配置)。
git
用于备份的最大问题是它不保留大多数文件系统元数据。具体git
不记录:
您可以通过编写工具将这些信息显式记录到您的存储库中来解决这个问题,但要做到这一点可能很棘手。
谷歌搜索git 备份元数据产生了许多似乎值得一读的结果(包括一些已经尝试弥补我在这里提出的问题的工具)。
etckeeper是为备份/etc
和解决许多这些问题而开发的。
Phi*_*ent 12
虽然从技术上讲你可以做到这一点,但我会提出两个警告:
1,您正在使用二进制数据的源版本控制系统。因此,您将它用于它不是为之设计的东西。
2、如果您没有构建新机器的流程(文档或自动化),我会担心您的开发流程。如果你买了一辆公共汽车,谁会知道该怎么做,什么是重要的?
灾难恢复很重要,但是自动化(脚本化)新开发盒的设置比只备份所有内容更好。当然,将 git 用于您的脚本/文档,但不是用于计算机上的每个文件。
我使用 git 作为 Windows 系统的备份,它非常有用。在文章的底部,我展示了我用来在 Windows 系统上配置的脚本。使用 git 作为任何系统的备份提供了两大优势:
底线: git 备份为您提供了控制备份发生方式的不可思议的力量。
我在我的 Windows 系统上配置了这个。第一步是创建本地 git 存储库,您将在其中提交所有本地数据。我建议使用本地的第二个硬盘驱动器,但使用相同的硬盘驱动器也可以(但预计您会将其推到远程的某个地方,否则如果硬盘驱动器死机,您就会搞砸。)
您首先需要安装 cygwin(使用 rsync),并安装适用于 Windows 的 git:http : //git-scm.com/download/win
接下来,创建您的本地 git 存储库(仅运行一次):
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%
Run Code Online (Sandbox Code Playgroud)
接下来,我们有我们的备份脚本包装器,它将被 Windows 调度程序定期调用:
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
Run Code Online (Sandbox Code Playgroud)
接下来,我们有包装器调用的备份脚本本身:
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%
Run Code Online (Sandbox Code Playgroud)
我们有 exclude-from.txt 文件,我们把所有文件都放在那里忽略:
排除-from.txt:
target/
logs/
AppData/
Downloads/
trash/
temp/
.idea/
.m2/
.IntelliJIdea14/
OLD/
Searches/
Videos/
NTUSER.DAT*
ntuser.dat*
Run Code Online (Sandbox Code Playgroud)
你需要去任何远程仓库并对它们执行“git init --bare”。您可以通过执行备份脚本来测试脚本。假设一切正常,请转到 Windows 计划程序并将每小时备份指向 vbs 文件。之后,您将拥有计算机每小时的 git 历史记录。非常方便——每个人不小心删除了一段文字而错过了它?只需检查您的 git 存储库。
嗯,这不是一个坏主意,但我认为有两个危险信号需要提出:
...但是,它仍然可以成为与腐败相关的事情的良好备份。或者就像你说的,如果 .git/ 文件夹在其他地方。
... 所以你可能需要告诉你的 cronjob 添加标签,然后确保未标记的提交将被清理。