⬆️ ⬇️

Automated backup of Windows workstations with rsync and vshadow - Part 1

If you have a desire to maintain the working station in the most "protected" state, as well as as much as possible to "automate" this process, then this post is for you.



Will be used:



Bit batch file

Some Linux server

Some Windows Server 2008 R2 and Group Policies

')

The idea is as follows - invisibly from the user himself, so as not to disrupt his work, do a weekly copy of his profile, in order to eliminate data loss, and save it on the server in the most secure form.



To do this, start with the client side. We use shadow volume create - expand to eliminate the problem of not reading user open files, such as Outlook pst, or documents. We will need a set from Microsoft that contains vshadow — this is Volume Shadow Copy Service SDK 7.2, which can be downloaded for free from Microsoft servers, http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=23490 . We will need vshadow.exe and vshadow.pdb files from this SDK. Attention - they are different for 64-bit and 32-bit systems.



After installation they can be found here:



64-bit -% PROGRAMFILES% \ Microsoft \ VSSSDK72 \ TestApps \ vshadow \ bin \ obj-chk \ amd64

32-bit -% PROGRAMFILES% \ Microsoft \ VSSSDK72 \ TestApps \ vshadow \ bin \ release-xp



We also need a set of files from cwrsync under Windows - http://sourceforge.net/projects/sereds/files/cwRsync/4.2.0/cwRsync_4.2.0_Installer.zip/download



After installation they can be found here:



% PROGRAMFILES% \ cwRsync \ bin



We put all the files in one folder, let's call it “Backup” for convenience, and start creating a file that will launch this “death machine” - in fact, this is a simple batch file that will work out all the actions. Let's name it for convenience “backup.cmd”



We write to the file:



(magic is unknown)



setlocal

if NOT "%CALLBACK_SCRIPT%"=="" goto :IS_CALLBACK

set SOURCE_VOLUME=C:

set DESTINATION_VOLUME=O:

set CALLBACK_SCRIPT=%~dpnx0

set TEMP_GENERATED_SCRIPT=GeneratedVarsTempScript.cmd

%~dp0\vshadow.exe -nw -p -script=%TEMP_GENERATED_SCRIPT% -exec=%CALLBACK_SCRIPT% %SOURCE_VOLUME%

del /f %TEMP_GENERATED_SCRIPT%

goto :EOF

:IS_CALLBACK

setlocal

call %TEMP_GENERATED_SCRIPT%

%~dp0\vshadow.exe -el=%SHADOW_ID_1%,%DESTINATION_VOLUME%




This part of the “macaroni code” makes shadow volume copy with C: and connects it to O :, which we will use for copying, and then remove it with the same script. Now the part that is responsible for the rsync actions:



(magic is unknown)



setlocal

SET CWRSYNCHOME=C:\Backup\ ( rsync vshadow)

SET CWOLDPATH=%PATH%

SET CYGWIN=nontsec

SET PATH=%CWRSYNCHOME%;%PATH%

SET HOME=:\Backup\LOG\ ( )

SET RSYNC_PASSWORD=( , rsync)

SET MY_NAME=%computername%



( , )



SET FS_SRC_HOME="/cygdrive/O/Users/" ( , "Users")

SET FS_SRC_BCK="/cygdrive/C/Backup/" ( rsync, )



del /q :\backup\log\*.log ( )



()



rsync -av --chmod=ug=rwx %FS_SRC_B2% backupusr@hostname::sbackupusr/%MY_NAME%

rsync -avhP --inplace --stats --del --modify-window=1 --log-file=%FS_SRC_BCK%DATA/backupwork.log --exclude-from=%FS_SRC_BCK%DATA/exclude %FS_SRC_HOME% backupusr@hostname::sbackupusr/%MY_NAME%/snapshot/

rsync -av %FS_SRC_BCK% backupusr@hostname::sbackupusr/%MY_NAME%/snapshot/Backup/




With the first command we create a directory by computer name% computername%

With the second command, we copy data from a temporarily connected O: \ Users disk, while preserving the log file.

By the third command, we copy the entire C: \ Backup folder, for safekeeping of the log and the program itself.



This line “backupusr @ hostname :: sbackupusr” indicates the user on the receiving side as well as the host of the receiving side. (It will be considered in the second psto).



After that, we need to disconnect the O: disk, because if we don’t do this, the next time vshadow will not be able to connect a copy there, a copy will be made from the old O: \ disk all the time, despite the fact that the files in the user’s folder have long been changed.



(magic is unknown)



SET HOME=%HOMEDRIVE%%HOMEPATH%



VSHADOW.EXE -ds=%SHADOW_ID_1%




All this needs to be folded into a regular .cmd file, as I mentioned above, save it to the Backup folder where rsync and vshadow files are stored, and run as needed. Works on Windows Vista, Windows 7, Windows XP, 32 and 64-bit



PS: Of course, it will be necessary to raise the host machine, in our case based on * nix, and create partitions that will be protected by true-crypt in case of theft of the server. Also in the future, this folder can be packaged into the msi installer for installation on all workstations via Group Policies and use them to create a Scheduled Tasks and close access to the Backup folder for the user. But that's another story.

Source: https://habr.com/ru/post/132123/



All Articles