因为工作需要写了一个基于Windows系统平台VSS技术的文件强力文件复制程序,它的最大特点是可以复制正在运行中的文件甚至是被锁定的任何数据文件(一般正常复制会报文件占用的错误)适用于多方同时使用中的文件数据备份任务计划。
比如你是公司电脑管理员,你要对公司大家使用的共享数据做个紧急备份,但是里面有很多用户还在使用某些文件或者某些软件,通知协调又很麻烦,为了能快速并且无视用户占用情况下做紧急备份,用这种程序就再合适不过了,而且我为了数据备份后存储的安全性,又集成了调用RAR数据压缩的功能,全自动备份并打包成一个压缩包,让数据备份变的更加安全又方便转移,还有就是可以设定好任务计划,可以实现定期定时全自动备份指定文件夹的文档、视频、音频、图片等等所有数据,包括局域网共享的文件,是不是感觉还不错呢?
来看看代码,首先是主程序(数据强制备份工具V3.cmd)脚本:
@echo off
title 数据强制备份工具V3 - 轶软工作室
setlocal enabledelayedexpansion
:CHECK_CONFIG
if not exist "%~dp0Config.ini" goto CREATE_CONFIG
for /f "tokens=1* delims=:" %%a in ('findstr /b "BackupFrom: BackupTo: BackupTime:" "%~dp0Config.ini"') do (
if "%%a"=="BackupFrom" set "BackupFrom=%%b"
if "%%a"=="BackupTo" set "BackupTo=%%b"
if "%%a"=="BackupTime" set "BackupTime=%%b"
)
set "BackupFrom=%BackupFrom:"=%"
set "BackupTo=%BackupTo:"=%"
set "BackupTime=%BackupTime:"=%"
if not defined BackupFrom set "BackupFrom="
if not defined BackupTo set "BackupTo="
if not defined BackupTime set "BackupTime="
:VALIDATE_PATHS
set "valid=1"
if not exist "!BackupFrom!" set "valid=0"
if not exist "!BackupTo!" set "valid=0"
:MENU_LOOP
cls
echo 要备份的文件夹:!BackupFrom!
echo 备份到的文件夹:!BackupTo!
echo 自动备份的时间:!BackupTime!
echo ===========================
echo 请选择操作:
echo 1.修改要备份的文件夹路径
echo 2.修改备份到的文件夹路径
echo 3.设定或创建自动备份计划
echo 4.删除创建的自动备份计划
echo 5.立即执行备份压缩任务
echo 6.立即退出备份程序
choice /c 123456 /n /m "请输入选择(1-6):"
if errorlevel 6 exit /b
if errorlevel 5 goto BACKUP_NOW
if errorlevel 4 goto DELETE_TASK
if errorlevel 3 goto CREATE_TASK
if errorlevel 2 goto SET_BACKUP_TO
if errorlevel 1 goto SET_BACKUP_FROM
:SET_BACKUP_FROM
set /p "newPath=请输入要备份的文件夹路径:"
if not exist "%newPath%" (
echo 路径无效,请重新输入!
timeout /t 3 >nul
goto SET_BACKUP_FROM
)
set "BackupFrom=%newPath%"
goto UPDATE_CONFIG
:SET_BACKUP_TO
set /p "newPath=请输入备份到的文件夹路径:"
if not exist "%newPath%" (
echo 路径无效,请重新输入!
timeout /t 3 >nul
goto SET_BACKUP_TO
)
set "BackupTo=%newPath%"
goto UPDATE_CONFIG
:UPDATE_CONFIG
(
echo BackupFrom:"%BackupFrom%"
echo BackupTo:"%BackupTo%"
echo BackupTime:"%BackupTime%"
) > "%~dp0Config.ini"
goto MENU_LOOP
:CREATE_TASK
schtasks /delete /tn "AutoBackupToolsTask" /f >nul 2>nul
set /p "BackupTimeInput=设定每天自动执行备份的时间(如果是23:55就输入2355按回车键)请输入:"
set "BackupTime=%BackupTimeInput:~0,2%:%BackupTimeInput:~2,2%"
schtasks /create /sc daily /st %BackupTime% /tn "AutoBackupToolsTask" /tr "\"%~dp0AutoBackupToolsTask.cmd\""
timeout /t 3 >nul
goto UPDATE_CONFIG
:DELETE_TASK
schtasks /delete /tn "AutoBackupToolsTask" /f >nul 2>nul && (echo 已删除计划任务) || echo 未找到计划任务
set "BackupTime="
(
echo BackupFrom:"%BackupFrom%"
echo BackupTo:"%BackupTo%"
echo BackupTime:"%BackupTime%"
) > "%~dp0Config.ini"
timeout /t 3 >nul
goto MENU_LOOP
:BACKUP_NOW
echo 正在执行备份操作...
set "BackupFrom="
set "BackupTo="
for /f "tokens=1* delims=:" %%a in ('findstr /b "BackupFrom: BackupTo:" "%~dp0Config.ini"') do (
if "%%a"=="BackupFrom" set "BackupFrom=%%b"
if "%%a"=="BackupTo" set "BackupTo=%%b"
)
set "BackupFrom=%BackupFrom:"=%"
set "BackupTo=%BackupTo:"=%"
if not defined BackupFrom (
echo 错误:未找到要备份的文件夹!
timeout /t 5 >nul
goto MENU_LOOP
)
if not exist "%BackupFrom%" (
echo 错误:要备份的文件夹路径 "%BackupFrom%" 不存在!
timeout /t 5 >nul
goto MENU_LOOP
)
if not defined BackupTo (
echo 错误:未找到备份到的文件夹路径(BackupTo)!
timeout /t 5 >nul
goto MENU_LOOP
)
if not exist "%BackupTo%" (
echo 错误:备份到的文件夹路径 "%BackupTo%" 不存在!
timeout /t 5 >nul
goto MENU_LOOP
)
echo 正在初始化临时目录...
if not exist "%temp%\备份的数据" (
md "%temp%\备份的数据" 2>nul || (
echo 错误:无法创建临时目录!请检查temp文件夹权限。
timeout /t 5 >nul
goto MENU_LOOP
)
)
echo 正在复制指定文件夹...
start "" /wait /min %~dp0shadowcopy.exe "%BackupFrom%\*.*" "%temp%\备份的数据" /s /i /y /J /R
if errorlevel 1 (
echo 错误:文件复制失败!请检查 Shadowcopy 是否可用。
timeout /t 5 >nul
goto MENU_LOOP
)
echo 正在压缩要备份的文件夹...
set "timestamp=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%"
set "timestamp=!timestamp: =0!"
start "" /wait "%~dp0WinRAR.exe" a -ibck -k -ep1 -m1 -r -s -inul -df "%BackupTo%\!timestamp!.rar" "%temp%\备份的数据"
if errorlevel 1 (
echo 错误:压缩失败!请检查 WinRAR 是否可用。
timeout /t 5 >nul
goto MENU_LOOP
)
echo 正在清理临时文件...
del /f /q "%temp%\备份的数据\*.*" >nul 2>nul
rd /s /q "%temp%\备份的数据" >nul 2>nul
echo 恭喜!数据已备份完成!压缩后的文件夹已保存至:
echo %BackupTo%\!timestamp!.rar
echo 正在打开备份目录...
timeout /t 3 >nul
start "" "%BackupTo%"
timeout /t 9 >nul
goto MENU_LOOP
:CREATE_CONFIG
schtasks /delete /tn "AutoBackupToolsTask" /f >nul 2>nul
set "BackupFrom="
set "BackupTo="
set "BackupTime="
(
echo BackupFrom:"%BackupFrom%"
echo BackupTo:"%BackupTo%"
echo BackupTime:"%BackupTime%"
) > "%~dp0Config.ini"
if exist "%~dp0Config.ini" (
goto MENU_LOOP
) else (
echo 错误:无法生成配置文件!请检查当前文件夹权限。
timeout /t 5 >nul
exit /b
)
goto MENU_LOOP
接下来是任务计划调用的(AutoBackupToolsTask.cmd)脚本:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%a in ('findstr /b "BackupFrom: BackupTo: BackupTime:" "%~dp0Config.ini"') do (
if "%%a"=="BackupFrom" set "BackupFrom=%%b"
if "%%a"=="BackupTo" set "BackupTo=%%b"
if "%%a"=="BackupTime" set "BackupTime=%%b"
)
set "BackupFrom=%BackupFrom:"=%"
set "BackupTo=%BackupTo:"=%"
set "BackupTime=%BackupTime:"=%"
if not defined BackupFrom set "BackupFrom="
if not defined BackupTo set "BackupTo="
if not defined BackupTime set "BackupTime="
:VALIDATE_PATHS
set "valid=1"
if not exist "!BackupFrom!" set "valid=0"
if not exist "!BackupTo!" set "valid=0"
echo 正在执行备份操作...
set "BackupFrom="
set "BackupTo="
for /f "tokens=1* delims=:" %%a in ('findstr /b "BackupFrom: BackupTo:" "%~dp0Config.ini"') do (
if "%%a"=="BackupFrom" set "BackupFrom=%%b"
if "%%a"=="BackupTo" set "BackupTo=%%b"
)
set "BackupFrom=%BackupFrom:"=%"
set "BackupTo=%BackupTo:"=%"
if not defined BackupFrom (
echo 错误:未找到要备份的文件夹!
timeout /t 5 >nul
goto MENU_LOOP
)
if not exist "%BackupFrom%" (
echo 错误:要备份的文件夹路径 "%BackupFrom%" 不存在!
timeout /t 5 >nul
goto MENU_LOOP
)
if not defined BackupTo (
echo 错误:未找到备份到的文件夹路径(BackupTo)!
timeout /t 5 >nul
goto MENU_LOOP
)
if not exist "%BackupTo%" (
echo 错误:备份到的文件夹路径 "%BackupTo%" 不存在!
timeout /t 5 >nul
goto MENU_LOOP
)
echo 正在初始化临时目录...
if not exist "%temp%\备份的数据" (
md "%temp%\备份的数据" 2>nul || (
echo 错误:无法创建临时目录!请检查temp文件夹权限。
timeout /t 5 >nul
goto MENU_LOOP
)
)
echo 正在复制指定文件夹...
start "" /wait /min %~dp0shadowcopy.exe "%BackupFrom%\*.*" "%temp%\备份的数据" /s /i /y /J /R
if errorlevel 1 (
echo 错误:文件复制失败!请检查 Shadowcopy 是否可用。
timeout /t 5 >nul
goto MENU_LOOP
)
echo 正在压缩要备份的文件夹...
set "timestamp=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%"
set "timestamp=!timestamp: =0!"
start "" /wait "%~dp0WinRAR.exe" a -ibck -k -ep1 -m1 -r -s -inul -df "%BackupTo%\!timestamp!.rar" "%temp%\备份的数据"
if errorlevel 1 (
echo 错误:压缩失败!请检查 WinRAR 是否可用。
timeout /t 5 >nul
goto MENU_LOOP
)
echo 正在清理临时文件...
del /f /q "%temp%\备份的数据\*.*" >nul 2>nul
rd /s /q "%temp%\备份的数据" >nul 2>nul
再配合下面相应的程序,就可以实现了数据硬拷贝了。
经过多次测试该方法是切实可行的,我在这里提供创意思路,如果你对该程序感兴趣,可以去gitcode下载我分享的数据强制备份工具V3【点击下载】相应软件程序进行测试,欢迎高手继续推演,感谢关注轶软工作室。