📜 ⬆️ ⬇️

Easy backup automation for Windows

The question of backup concerns every specialist responsible for the safety of the priceless official documents / databases / other files entrusted to him. In order to feel more confident in the face of various probable force majeure, you need to periodically back up critical data. No less urgent is the problem of saving acquired by overwork and for home users.
We offer you a simple but effective script that helps to solve this problem.

We have the initial conditions:
Operating system: MS Windows XP, 2003, etc .;
Backup Objects: Folders and files located on local and network drives in the amount of ~ 200 GB;
Budget: 0 rub .;
The number of specially trained staff to set up the process: 1 person.
The number of specially trained staff to service the process: 0 people.
Task: daily full backup of data, storage of a specified number of backups created without operator participation.

In the vastness of the web, you can find a fairly large number of different programs that solve a similar problem. According to the task and the conditions set, the following programs were found, installed and tested under close combat conditions: Cobian Backup (9, 10), Toucan, Areca, FBackup, Backup2Net and Samsung Auto Backup backup software (included with a removable disk of the same name) .

Testing these programs on working data volumes showed their steady inability to cope with the simple task of automatically creating daily backups of data of the required volume. Problems arose both at the stage of preparing for the implementation of the reservation, and actually in the progress thereof.
Cobian Backup 10 lasted the longest (about a week), after which it went to the abandoned one and stubbornly began to make backups of only about 600 MB of data and dumping with a bunch of errors in the logs. Manipulations with the settings of shadow copying and archiving did not lead to anything, unfortunately, and in general the program left a rather pleasant impression, so it is recommended to use it (assuming stable work in your specific conditions).
Special hope was on Toucan, as the program belongs to the ACT. But alas, hopes were not justified. In the course of the assignment at different stages, the program fell out with a classic mistake about an unacceptable operation and the need for its (program) closure.
The remaining participants of the selection also did not suit me for various reasons. I don’t see much point in revealing the process of testing these programs because to the purpose of writing this article is not directly related.
Thus, a simple idea of ​​finding a free solution to the problem was failed miserably. Since there was no longer any strength, desire or hope to roam further across the expanses of the Web for free backup programs for commercial use, and backups were urgently needed, a natural decision / desire to build something of their own arose. The result of thinking for quite a short time was the solution proposed below for simple automation of data backup by means of an ordinary bat-script and a standard scheduler. The script works, as they say, out of the box, and does not require additional testing and debugging, you only need to provide it with input data and create the catalogs involved in the process in advance (there are no checks on this item for reasons of simplicity).
Anticipating the dissatisfaction of many about laying out ready-to-use solutions, I want to say that not many of our fellow citizens have the desire / time / opportunity / talent / to enter the right one in order to deal with the syntax of the standard Windows command line. I also dare to point out the non-zero probability that at least a few young and green administrators will take a look at the unquestionable bes uselessness of studying this field.

And so, let's get started. First give the full text of the script without comment. Full analysis with comments see below.

@Echo Off
echo %date% %time% ***Start***
SetLocal EnableDelayedExpansion
Set $SRC=list_add.txt
Set $EXC=list_excl.txt
Set $DST=D:\backups
Set $PSW=password
Set $COMPR=1
C:\"Program Files"\7-Zip\7z.exe a -t7z -mhe=on "%$DST%\files%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%_%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%.7z" @%$SRC% -scsWIN -p%$PSW% -mx=%$COMPR% -ssw -xr@%$EXC% >> log_7z.log
For /F "Delims=" %%i In ('DIR /B/O:-N %$DST%\files????-??-??_??-??-??.7z') Do (
Set /A $COPIES-=1
If !$COPIES! LSS 0 DEL "%$DST%\%%i"
echo %date% %time% ***End***

The result of the daily work of the script will be a set of backups of the data you need, which looks something like this:
dir d:\backups\


Promised analysis with comments.

@Echo Off ::
echo %date% %time% ***Start*** ::
SetLocal EnableDelayedExpansion :: .

In the usual case, to determine the value of an environment variable, you must use the% variable%. However, if the variable value changes inside the loop and is read there, then for the bat file to work correctly, use the SetLocal EnableDelayedExpansion command and use the "!" Symbol instead of the "%" symbol. Otherwise, the value of a variable in a loop will always be the same — the same as it was before the entry into the loop.

Further, for successful operation of the proposed script, it is necessary to set the following input parameters (variables):

$SRC=list_add.txt ::-
$EXC=list_excl.txt ::-
$DST=D:\backups ::
$PSW=password ::
$COMPR=1 :: 7zip (1= , 5= , 9=-, 0= )
$COPIES=7 ::

The next line starts archiving using 7-Zip files and folders listed in the list_add.txt file, except for the files specified in the exceptions list_excl.txt file .

C:\"Program Files"\7-Zip\7z.exe a -t7z -mhe=on "%$DST%\files%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%_%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%.7z" @%$SRC% -scsWIN -p%$PSW% -mx=%$COMPR% -ssw -xr@%$EXC% >> log_7z.log

Let us examine this line in more detail:

C: \ "Program Files" \ 7-Zip \ 7z.exe :: Indicates the location of the executable file of the 7-Zip archiver (or rather its console version);

a :: is a command to add files / folders to the archive;

-t7z :: key sets the type of archive being created = 7z;

-mhe = on :: enables encryption of the archive header and file / folder names in the archive with a password;

The command dynamically forms the full path and file name of the archive of the form “D: \ backups \ files2010-07-12_22-00-00.7z” , where the name contains the date and time of archiving;

@% $ SRC% :: specifies the name of the file list of the archived data, instead of% $ SRC% the script substitutes the name of the file;

-scsWIN :: sets the encoding win for the list file;

-p% $ PSW% :: key sets the password for encrypting the archive, instead of% $ PSW% the script substitutes the text string of the password;

-mx =% $ COMPR% :: key sets the compression level for the archived data;

The -ssw :: key allows archiving of files opened by applications for writing;

-xr @% $ EXC% :: sets the name of the file listing the data to be excluded from the archiving process, r = means that the subfolders should also be excluded, instead of % $ EXC% the script substitutes the name of the file;

>> log_7z.log :: symbols >> denote the redirection of messages displayed as a whole line from the screen (standard stream) to the log_7z.lo g file, which is convenient for the subsequent analysis of the script operation;

This part of the script is responsible for automatically deleting already unnecessary data archives, in accordance with the number specified by the $ COPIES variable.

For /F "Delims=" %%i In ('DIR /B/O:-N %$DST%\files????-??-??_??-??-??.7z') Do (
Set /A $COPIES-=1
If !$COPIES! LSS 0 DEL "%$DST%\%%i"

We will analyze in more detail. Automatic deletion is implemented by organizing a cycle in which a set of names (set) of archive files is searched (how it is formed - see the DIR line below) ...
In general, our cycle has the form: for / F [“Keyword of Disassembly”] {%% | %} variable in (Set Filename) do command [Command Line Parameters]
The / F switch determines that the loop will parse all the elements of the Set of NameFiles ;
“Key words of Disassembly” - indicate which separators are used between the elements of the set being parsed, in our case there is no separator;
%% i is the variable i , which contains in each iteration step the current element of the set being parsed (that is, the next file name in our case).

DIR /B/O:-N %$DST%\files????-??-??_??-??-??.7z

The command forms a set (set) of file names contained in the folder of the specified $ DST variable and corresponding to the mask files ???? - ?? ?? ?? ?? ?? ?? ?? ?? 7z , where the question mark denotes any character in the given positions; the / B switch only displays file names, and the / O switch : -N sets the sort by name ( N ) or in reverse order ( -N ), in our case the reverse order.
The set has the form:
Pay attention to the order of sorting items!

Do (...) - in brackets contains the command (s) executed in each iteration, in our case:
Set / A $ COPIES- = 1 :: decrement of the COPIES variable, which acts as a loop counter;
If! $ COPIES! LSS 0 DEL "% $ DST% \ %% i" :: the command checks the condition $ COPIES LSS 0 i.e. $ COPIES <0 , if true, then the command to delete the archive file with the name of the current element should be executed, so the number of stored copies of the archive will not exceed the specified value, in our example $ COPIES = 7 .

echo %date% %time% ***End*** ::

I will also give, for example, the contents of the list_add.txt list file:


And the list_excl.txt exception file :


Actually, that's the whole layout. The script is small, but reliably working. There are no tricks, everything is quite simple.
And finally, the banality ... To automate the launch, you can use the standard Windows scheduler, the command line of the form "d: \ backup \ backup.bat >> d: \ backup \ backup.log". This will allow displaying script messages in a log file.
Good luck to all.


1) Reference on command line parameters from MS.

2) Detailed help on the functions of the console version of 7-Zip is attached to the archiver.

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

All Articles