|
Answer» It's been a while since I've been here, but I used to be a pretty active user. Anyway, our network recently got hit by the CryptoWall virus (it encrypts all your files and then asks you to pay to decrypt). Luckily I had a backup that was just over a week old, so we didn't loose too much.
I have been using a batch file that someone on here helped me write a few years ago. Now I'd like to make some modifications to make it more secure. The batch file first uses the xcopy command with a few switches to only backup new or modified files from the source so that it doesn't have to copy the entire server every time. The batch file secondly uses two short loops to go through the entire directory tree and delete files from the backup that have been removed from the source. The problem with this is if the source gets deleted or corrupted, it will delete all those files from the backup (huge problem). What I've decided it should do is log the total file count on the source each time it backs up and if the count is < the previous count by some arbitrary number (100 is good), then it does not delete the files from the backup.
I'd also like to implement some hashing, but I know that can't be DONE directly in batch. Does anyone know of any command line programs that will go through an entire directory tree and generate a list of hashes and then be able to go back a verify those hashes against the files?
Here is the batch file so far:
Code: [Select]echo off setLocal EnableDelayedExpansion title Folder Sync System color 0a set backCMD=xcopy /c /f /d /e /h /i /r /y set /p backPath=Enter the name of the folder to backup: set sourcePath=Z:\%backPath% set backPath=%cd%%backPath%
echo Backing up %sourcePath% to %backPath%. . . echo.
rem BACKUP THE FILES THAT HAVE BEEN CHANGED %backCMD% %sourcePath% %backPath%
echo. echo Removing old stuff in %backPath%. . . echo.
rem CHECK FOR FILES THAT HAVE BEEN REMOVED FROM rem THE SOURCE AND DELETE THEM FROM THE BACKUP DIRECTORY for /r %backPath% %%g in (*.*) do ( set var=%%g if not exist "%sourcePath%!var:%backPath%=!" ( echo File:!var! echo. del /f /q "!var!" ) )
rem REMOVE ANY EMPTY DIRECTORIES FROM BACKUP for /d /r %backPath% %%g in (*) do ( set var=%%g if not exist "%sourcePath%!var:%backPath%=!" ( echo Dir:!var! echo. rd /s /q "!var!" ) )
echo %date% : %time% : %backPath% >> backup.log echo Finished backing up %sourcePath% echo Log entry added in backup.log pause exit Well we used md5 on our linux servers years ago.I was thinking a possible better way to do this would be to have a dedicated folder for files deleted from the source which would move from the backup to this folder. The only thing I don't know how to do is have the move command create an empty directory structure to move the file to.
say I want to move a file: move thing.txt C:\bla\bla\bla
but bla bla bla does not exist. Is there a way to automatically create the structure?I searched around and it looks like I just need to remove the filename part of the path and then I can just use the md command to make the empty directories. (I was not aware that md will create a full path, not just one folder.)
Code: [Select]set pathonly=%%~dpg I could not find any hashing utility to my liking, so I wrote my own in C. It scans and verifies an entire directory tree. It uses OpenSSL for the md5 library. If anyone uses it, let me know of any problems because I just finished it.
Usage: recmd5 [scan or verify] [name of hash file] [directory to scan (only include if using scan)]
When scanning, it will generate [NAME]_md5.log file in the current directory and that will contain all the hashes. When verifying, it will generate [NAME]_md5_err.log containing all the corrupted and missing files.
http://www.filedropper.com/recmd5Don't use the link in the previous post. The compiler wasn't linking it right so I had to include the whole runtimes. I fixed it, so the standalone exe is attached.
[attachment deleted by admin to conserve space]I used a tool called fastsum in 2004 to verify my backups
Code: [Select]FastSum - extremely fast utility for your files INTEGRITY control. FastSum calculates short and strong digests of your data via POPULAR MD5 algorithm to use it as references checksums for ulterior data integrity verification.
FastSum - Allows to rectify the errors occurring while data transfer. For example: Network transfers, CD-R and DVD burning and much more. It is developed for easy processing of a huge files count and has usable command-line interface. It is an analogue of a famous md5sum Unix platform utility.
Using Fastsum
FastSum is a console application, it means that the parameters are set from the command line. There are two basic modes: checksums calculation - checksums are used as samples for ulterior verification of sample values with the current ones to identify the integrity of files between the verifications. The second mode is – verification mode. Parameters have the following SYNTAX:
FSUM [Path] [File | /T:Type] [/C:File] [/V] [/O] [/R]
Quote I used a tool called fastsum in 2004 to verify my backups
Looks like exactly what I needed.
Anyway, I need help with the script in the OP. It doesn't work if I type a folder with a space in the name. I tried surrounding it with quotes, but it still causes an error on the part of the code to delete files in backup. "The filename, directory name, or volume label syntax is incorrect." Can someone help with this?Test this:
Code: [Select]echo off setLocal EnableDelayedExpansion title Folder Sync System color 0a set backCMD=xcopy /c /f /d /e /h /i /r /y set /p backPath=Enter the name of the folder to backup: set "sourcePath=Z:\%backPath%" set "backPath=%cd%%backPath%"
echo Backing up "%sourcePath%" to "%backPath%". . . echo.
rem BACKUP THE FILES THAT HAVE BEEN CHANGED echo %backCMD% "%sourcePath%" "%backPath%" pause %backCMD% "%sourcePath%" "%backPath%" echo.Did you see the post above?Thanks for the help. I tried that and the deleting files part still doesn't work (the part after what you posted). The first part works fine with the quotes around it, but when I add quotes everywhere in the other part, it still doesn't work.
I added quotes around %backPath%, but it made no difference. There's no quotes around the echos because it doesn't MATTER. Here is the part that still doesn't work:
Code: [Select]rem CHECK FOR FILES THAT HAVE BEEN REMOVED FROM rem THE SOURCE AND DELETE THEM FROM THE BACKUP DIRECTORY for /r "%backPath%" %%g in (*.*) do ( set var=%%g if not exist %sourcePath%!var:%backPath%=! ( echo File:!var! echo. del /f /q "!var!" ) )
rem REMOVE ANY EMPTY DIRECTORIES FROM BACKUP for /d /r "%backPath%" %%g in (*) do ( set var=%%g if not exist "%sourcePath%!var:%backPath%=!" ( echo Dir:!var! echo. rd /s /q "!var!" ) ) I didn't notice there were scrollbars so didn't see the rest of the code.
What errors do you see on the console? Your changes look correct by adding quotes in the for commands but we can't see what changes are above it atm.
WRT the echo command - in general that variable also needs to be quoted in certain circumstances, such as when it contains an & or other poison characters.
You also removed the double quotes around this %sourcePath%!var:%backPath%=!
QuoteYou also removed the double quotes around this %sourcePath%!var:%backPath%=!
WOW. Can't believe I didn't see that. That fixed the problem. I know the original had that, but I was making a lot of changes to the file.
Thanks for mentioning that.
BTW the error was: "The filename, directory name, or volume label syntax is incorrect."
Um... One more thing. So now I want to replace the del command you see in the code with md and move. You can see what I want to do in this pseudo code:
md "%backPath%_DELETED\[path to file without name here]" move "%backPath%[path to file]" "%backPath%_DELETED\[path to file]"
If it's not too complicated, could you help me with this? Thanks.Replace the top line with the lower block and run it with dummy test files to see what is echoed to the console.
If it's right then close the window with the mouse and remove the 2 echo statements and the pause
Code: [Select]del /f /q "!var!"
Code: [Select]echo if not exist "%backPath%_DELETED%%~pg" md "%backPath%_DELETED%%~pg" echo move "%%g" "%backPath%_DELETED%%~pg" pause I read your post, and I'll get to testing it when I can.
|