我的文件夹备份计划

我的文件夹备份计划

记录时间:2011-02-18

母备份脚本

  • 设定全局函数
  • 把之前的备份复制到指定的文件夹
  • 运行子备份计划
  • 文件名:backup-parent.bat
    • MY_HOME:我的文件夹所在地
ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

REM == configuration ==
ECHO Reading configuration ...
SET backup_scripts=%MY_HOME%\scripts\backups
SET old_target_dir=D:\backup\2cn.de_old
REM == global configuration ==
SET excluded_dirs=.svn,*.bak,target,.metadata,.settings
SET excluded_files=*.bak,*.log,.classpath,.project
REM SET target_dir=d:\backup
SET target_dir=D:\backup\2cn.de
REM SET packer_cmd=%ProgramFiles%\7-Zip\7z.exe
SET packer_cmd=%MY_HOME%\tools\LiberKey\Apps\7Zip\App\7-Zip\x86\7z.exe
SET packer_opts=-p=yun a  -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on
SET date_delimeter=.
SET temp_dir=%TEMP%\backup

REM == validation ==
ECHO Validating configuration  ...
IF NOT EXIST "%packer_cmd%" (
  ECHO No packer found in the location: %packer_cmd%
  GOTO end
)

REM == moving old backups ==
ECHO Moving old backups to OLD folder ...
IF EXIST "%target_dir%" (
  IF EXIST "%old_target_dir%" DEL /f /s /q "%old_target_dir%"\*.*
  MKDIR "%old_target_dir%"
  MOVE /y %target_dir%\*.* %old_target_dir%\
)

REM == running child backups ==
ECHO Running child backup scripts ...
FOR /f "delims=" %%A IN ('dir /s/b/a-d %backup_scripts%\*.bat') DO (
  SET current_backup=%%~A
  ECHO - running !current_backup! ...
  START "!DO NOT CLOSE! Running !current_backup! ... " /belownormal /wait !current_backup!
  ECHO - !current_backup! done
)
ECHO Done running child backups

:end
DATE /t
TIME /t

ENDLOCAL

子备份脚本

  • 检查环境设定
  • 复制备份文件到临时文件夹
  • 删除不需要备份的文件夹及文件
  • 压缩临时文件夹到指定位置
  • 删除临时文件夹
ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION


REM == configuration ==
ECHO Reading configuration ...
SET backup_name=
SET backup_dirs=
REM == project specific configuration (overwrites globals) ==
IF "%target_dir%"=="" SET target_dir=d:\backup\2cn.de
IF "%packer_cmd%"=="" SET packer_cmd=%MY_HOME%\tools\LiberKey\Apps\7Zip\App\7-Zip\x86\7z.exe
IF "%packer_opts%"=="" SET packer_opts=-p=yun a -tzip -r -mx5
IF "%date_delimeter%"=="" SET date_delimeter=.
IF "%temp_dir%"=="" SET temp_dir=%TEMP%\backup

REM == validation ==
ECHO Validating configuration  ...
ECHO backup_name=%backup_name%
ECHO bakcup_dirs=%backup_dirs"
ECHO target_dir=%target_dir%
ECHO packer_cmd=%packer_cmd%
ECHO packer_opts=%packer_opts%
ECHO date_delimeter=%date_delimeter%
ECHO temp_dir=%temp_dir%
IF NOT EXIST "%packer_cmd%" (
  ECHO No packer found in the location: %packer_cmd%
  GOTO end
)
FOR %%A IN (%backup_dirs%) DO (
  SET current_dir=%%~A
  IF NOT EXIST "!current_dir!" (
    ECHO Check backup_dirs configuration. Backup directory "!current_dir!" doesn't exist!
  )
)

REM == initialization ==
ECHO Initialializing ...
FOR /f "tokens=1,2,3 delims=%date_delimeter% " %%a IN ('date /t') DO (
  SET current_date=%%c%%b%%a
)
FOR /f "tokens=1,2 delims=: " %%a IN ('time /t') DO (
  SET current_time=%%a%%b
)
SET file_ts=%current_date%%current_time%


REM == preparation ==
ECHO Preparing temporary snapshot ...
IF NOT EXIST "%temp_dir%" MKDIR "%temp_dir%"
FOR %%A IN (%backup_dirs%) DO (
  SET current_dir=%%~A
  ECHO - copying !current_dir! ...
  SET dest_dir=%temp_dir%\!current_dir:~0,1!%%~pnxA
  XCOPY "!current_dir!" "!dest_dir!" /v /c /i /g /h /q /r /y /e
)

REM == assembly ==
ECHO  Packaging ...
SET target_output=%target_dir%\%backup_name%_%file_ts%.zip
ECHO Target output name is %target_output%
IF EXIST "%target_output%" DEL /f /q "%target_output%"
START "!DO NOT CLOSE! Compressing ... " /belownormal /wait "%packer_cmd%" %packer_opts% "%target_output%" "%temp_dir%\*"
ECHO Done compressing backed up files



REM == cleanup ==
ECHO Cleaning up ...
IF EXIST "%temp_dir%" RMDIR /s /q "%temp_dir%"

:end
TIME /t

ENDLOCAL 
 
EXIT 0

设置定时任务计划

  • 任务开始:开机后在电脑空闲30分钟的情况下启动备份计划
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s