Backing up bat files. Backup: using a bat file. Backup using WinRAR

A man in a woolen cloth jacket was completely depressed by the office environment,
the smell of alizarin ink, for hours that breathed often and heavily,
and especially with a strict poster "I did my job - and go away."
Although the man in the jacket had not yet started his business, he already wanted to leave.

September 9, 2007

Recently I had a need to have backup of one database over the past few days. One of the conditions was to implement all this without using third-party or self-written programs. After some deliberation, it was decided to implement it using standard dos commands described in the bat file.

Those who know how to write BAT files may not read any further in principle. For the rest, let me explain that all lines starting with rem are comments. Therefore, they are not executed and you can copy the entire code.

And the algorithm is very simple. We have two folders archive_new, where the archive for yesterday is stored, and archive_old, where the archive for the day before yesterday is stored. Thus, we get the following:


rmdir / s / q \\ server \ archive \ archive_old

ren \\ server \ archive \ archive_new archive_old

mkdir \\ server \ archive \ archive_new
rem Copy the files with the database to the archive_new folder
copy / y * .base \\ server \ archive \ archive_new

Everything. With this algorithm, executed once a day, we will have two backups: for yesterday and the day before yesterday.

This simplest algorithm is ideal when you just need to copy files to another location. It has two drawbacks.

First, the bases can be large.

Secondly, absolute paths are registered here, and when you change the settings or structure, you will have to correct all the paths in the file.

These two disadvantages are easily solved by archiving the databases and setting variables for relative paths.


rem The current date will be used instead of the archive name
SET archive_name =% date% .rar

rem Delete the archive_old folder

rem Rename the archive_new folder to archive_old

rem Create directory archive_new

rem Archive all files except * .exe and * .bat
rar a -r -m5 -x * .bat -x * .exe% archive_name%
rem Copy the archive with the database to the archive_new folder
copy / y% archive_name%% server_dir% \ archive_new \
rem Move archive
move% archive_name%% serev_dir% \ archive_new \

NB: for the line with archiving to work, you need the file "rar.exe" from the standard WinRar distribution "a.

The above example has one major drawback: if the database is constantly used by the application, then the archiver will not be able to access it. In this case, you will have to slightly correct the design and first copy the files to another directory, where they are archived.

rem Declare variables for the archive name and path archive folder on server
SET archive_name =% date% .rar
SET server_dir = \\ server \ archive \
SET temp_dir = \\ temp \
rem Delete the archive_old folder
rmdir / s / q% server_dir% \ archive_old
rem Rename the archive_new folder to archive_old
ren% server_dir% \ archive_new archive_old
rem Create directory archive_new
mkdir% server_dir% \ archive_new
rem Copy the files with the database to the temp folder
copy / y * .base% temp_dir%
rem Archive all files in temp folder except * .exe and * .bat
rar a -r -m5 -ep1 -x * .bat -x * .exe% archive_name%% temp_dir%
rem Move the archive to the archive_new folder
move% archive_name%% server_dir% \ archive_new \
rem Clear the contents of the temp folder
del / q% temp_dir% *. *

In general, that's all. Now all that remains is to make a schedule for the execution of this .bat file and you do not have to worry about the safety of the site database.

P.S. For those who want to pervert, most of the things described here can be done through keys in RAR "e.

The most affordable backup tool is command line... You can find a more or less decent implementation of backup in Windows systems using CMD / BAT files using any search engine. However, there is often no need to accumulate and store all created archives, only a few of the most recent ones are enough. In this case, a backup implementation with support for archiving depth is required. Finding the implementation of archiving depth support in such scripts is already much more difficult.

Your attention is a relatively simple script (CMD script) that implements backup with a given archiving depth.

The script is designed to create backups by packing the contents of an arbitrary directory into an archive. In addition to the actual support of the archiving depth in the script, the saving of new backups to directories, the names of which are formed on the basis of the date / time of the backup, is realized. When performing archiving, RAR will also try to open shared files (it is useful not to force users to quit the program; for example, when backing up 1C file bases). The most convenient way to use this approach to data backup is to write the script launch into the scheduler.

Script content:

SETLOCAL enabledelayedexpansion rem Backup depth (the number of recent archives to be backed up) SET ArcDepth = 5 rem Path to the directory where the backups will be stored SET DirName = My Backup "s rem Path to the directory with the new backup (% DATE: = 0 %% TIME: = 0% - replaces spaces in% DATE% and% TIME% with zeros) FOR / f "tokens = 1-7 delims = / -:.," %% a IN ("% DATE: = 0% % TIME: = 0% ") do (SET NewBkDir =% DirName% \ %% c. %% b. %% a _ %% d. %% e. %% f. %% g) rem Path to the log file SET LogFile =% DirName% \% DirName% .LOG ECHO ***** START% DATE%% TIME% ***** >> "% LogFile%" IF NOT EXIST "% DirName%" (MD "% DirName % "ECHO"% DirName% "- created >>"% LogFile% ") ECHO Create a new directory for backup: >>"% LogFile% "IF NOT EXIST"% NewBkDir% "(MD"% NewBkDir% "ECHO"% NewBkDir% "- created >>"% LogFile% ") ELSE (ECHO"% NewBkDir% "- existed before >>"% LogFile% ") rem New backup block SET SrcData = D: \ My Src SET ArcName =% NewBkDir% \ MySrc.rar ECHO Archiving "% SrcData%" using RAR >> "% LogFile%" rar a -m5 -md4096 -ep1 -ri1 -dh -ilog "% LogFile%" "% ArcName%" "% SrcData%" rem Maintaining the archiving depth (only the last% ARCDEPTH% of directories should remain) ECHO Deleting old backups: >> " % LogFile% "SET Index = 0 rem DIR / AD / B / OD"% DirName% "- get a list of directories sorted by date, starting with the newest FOR / f" tokens = 1 "%% i IN (" DIR / AD / B / OD "% DirName%" ") DO (rem Skip the first% ArcDepth% archives, delete the rest SET / a Index + = 1 IF! Index! LEQ% ArcDepth% (echo "% DirName% \ %% i" - left >> "% LogFile%") else (RMDIR / S / Q "% DirName% \ %% i" echo "% DirName% \ %% i "- deleted >>"% LogFile% ")) ECHO ***** END% DATE%% TIME% ***** >>"% LogFile% "ECHO. >> "% LogFile%" ECHO. >> "% LogFile%"

Since it is used console version RAR archiver, it is necessary that RAR.exe is available for the script. For new localized versions of the archiver, in addition to the RAR.exe file, it is also desirable to have the RarLng.dll file, and for registered users - also the rarreg.key file. To ensure accessibility, you can, for example, just put everything required files to the working directory of the script.

The archiving depth here does not mean the period of time for which the archives are stored, but the number of the last stored archives. When using a backup interval of 1 day (day), the backup depth in days will coincide with the number of stored archives.

In variables containing paths to files / directories, you can use both absolute and relative paths, as well as spaces.

The example contains only one direct backup block (located after the corresponding comment). You can add several such blocks one after another, for example, by copying and pasting an existing block and changing the values ​​of the SrcData and ArcName variables.

Backups are decomposed into directories whose names contain the date and time of the backup in the following order: year, month, date, hours, minutes, seconds, hundredths of a second (valid for Russian regional settings). This is necessary so that the alphabetical sorting of the backup list coincides with the sorting by the date / time of the backup (that is, just for the convenience of working with copies). In the case of using regional settings, in which the order of the date elements is different, to obtain a similar effect, you must change the order of the %% a - %% g tokens in the command
SET NewBkDir =% DirName% \ %% c. %% b. %% a _ %% d. %% e. %% f. %% g

Checking the existence of a directory with a backup copy before creating it is necessary if you suddenly want to remove the "extra" from the directory name (for example, hundredths of a second) and run the script more often than this name allows. In this case, there will be no error during execution, but RAR will honestly overwrite the archives already in the directory.

The "native" encoding for CMD scripts is OEM (866), and for the Windows version of the RAR archiver - ANSI (Windows-1251). In this regard, the log file is somewhat unreadable if the script is saved in its native OEM encoding. On the one hand, for myself, I found this drawback uncritical (in the editor / viewer Far Manager it is not difficult to press F8 to change the encoding), and on the other hand, nothing prevents you from saving the script in ANSI encoding (output to the console by the ECHO commands is not used in the script - only to the log file, so the user will not see any cracks, but the log will be fine written in the same encoding). I would be grateful if you could suggest a more correct way to "make friends" these encodings.

Tags: team Windows string, backup, archive depth

We've all heard about the need to back up data. The collapse of the system, someone's playful pens, or they, only their own, and even many reasons why your data may deteriorate or be destroyed. There is no need to explain this to those who had to recover lost data for weeks.

In addition, according to the well-known law of meanness, the likelihood that the necessary files will deteriorate is much higher if you do not make a backup. So let's all be saved!

Automatic backup must meet certain requirements:

  • should be performed regularly. Moreover, the copying period is determined by the frequency of data update. Those. if the files change every day, then you need to copy them every day;
  • copies must be kept separate from the data source. It can be an external drive, or even better, store copies on remote server or in the cloud on the Internet. Then even if a room with a computer and external drive will fly up into the air, the data will remain quietly stored in the cloud on powerful and secure servers of large IT companies;
  • if the store has small size, copies should be compressed to save space;
  • for the same reason, you need to keep track of the number of last copies stored. For example, for very important data, I have 7 daily, 4 weekly and 12 monthly copies. The rest of the copies are deleted as unnecessary;
  • after saving, you need to check the availability of the backup and its integrity.

There are many programs, free and not so great, that back up your data. Let me give you an example of a free and paid program.

To do automatic start file, for example, once a week, you need

The Bat! is one of the best freeware products (free) among mail managers, a rich set of features, not inferior to such a leader as Microsoft Outlook, allowed The Bat! win over your large audience. However, like any software for Windows platforms, The Bat! not without surprises. Probably the biggest misfortune that can happen to users of a mail manager is the loss of letters. It happens that at the next launch of The Bat! We are met either with completely empty folders, where until recently there was an entire long-term archive of letters, or, for example, there are no letters for any period of time. It's too early to panic, because in most cases the letters are still on your computer, but the index index may have deteriorated.

Let's look at several ways to restore messages and folder structure in The Bat!

1. The most common The Bat! is the keyboard shortcut Ctrl + Alt + Shift + L in a running and active program that initiates the built-in tool for correcting the message base.

2. A possible culprit for the loss of letters may also be the index file MESSAGES.TBI or MESSAGES.TBN (depending on the version). With the program turned off, delete it and run BAT !, after a while the letters will be displayed.

3. Open the folder with your profile mail files and delete all files with names in it ACCOUNT.xxx Restart Bat, it will recreate these files by itself, but be careful, all settings will be deleted and they will need to be done again. You may need a combination of Ctrl + Alt + Shift + L.

4. If there is a backup copy, then you can get the missing ones without restoring all letters from it. For this we create new folder, preferably we copy one letter into it, close the program, go to this folder with an explorer, delete all files, copy the backup file here and change the extension BCK on EBB... At the next launch of the program, we receive old letters that can be copied to the main folder.

5. If the file is damaged and the program refuses to work, you need to resort to heavy artillery in the form of a program The Bat! Message Recovery(free).

6. The most loyal and quick way return letters is to restore them from a backup. Don't be lazy - make BACKUP COPIES!

We continue to study The Bat! In this tutorial, we'll walk through the backup and restore of mailboxes along with all the settings, search capabilities, and (hot key).

Backup

In previous lessons, we studied But when you reinstall the system or run the program on another computer or laptop, it becomes necessary to re-configure sending and receiving mail for each mailbox, automatic sorting, letter templates, etc. The task is especially complicated when there are a lot of mailboxes and they are registered with different mail services. Is solved this problem backup, thanks to which you can not only restore everything e-mail settings boxes, but also all incoming and outgoing letters in these boxes.

Let's consider the procedure for creating and restoring a backup copy of The Bat! using screenshots. The picture below shows where the corresponding menu items are located.

To create a backup, select the menu item of the same name and in the next window set the path and name of the future archive by clicking on "Browse". If necessary, write a comment to the archive.

IMPORTANT! When specifying a location for creating an archive, select a disk that is different from the system one, since when reinstalling the system, the archive may be overwritten. For example, if the system is located on disk "C", then specify the location for the archive on "D", "E" or on a flash drive.

After clicking "OK" a window will appear in which you can select all or individual boxes for archiving. Also, if necessary, you can set a password for the archive.

After pressing “OK” again, the backup process of The Bat!

To restore from a backup, select the appropriate menu item (see the first screenshot). Then click "Add" and find the archive created earlier. Then click "OK". In the next window, select the mailboxes for recovery and click "OK" again. We are waiting for some time and enjoy the result

By the way, if you use the portable version of The Bat !, located not on system disk, then after reinstalling the system mail client there will be no need to restore at all - it will be immediately ready to work. You just need to re-create the shortcut executable file on your desktop.

Search in The Bat!

The Bat! Mail client has a powerful search using fine tuning parameters of the required information. If you have a dozen mailboxes and hundreds or even thousands of letters in them, then this tool is simply irreplaceable.

Here are the search criteria you can customize:

1. Calling the search window
2. Search terms. You can select the following parameters:

  • text
  • sender (e-mail)
  • recipient (e-mail)
  • heading
  • note
  • Attached files
  • any part

3. Remove condition
4. Add a new condition
5. Selection of the parameter "AND", "OR"
"And" - all specified conditions must be met
"OR" - at least one of the specified conditions must be met
6. Select boxes and folders for search
7. Start search
8. Advanced search - makes it possible to set search parameters even more precisely. For example: letter size, letter attribute (status), limitation period, time interval and other criteria. I will not consider this mode. Everything is intuitive there, I think you can figure it out.

Let's look at two search examples:

Example 1. Let's say we need to find all letters with the phrase in the subject line "Windows 7 lesson" or "Windows 7 lesson" (we don't remember exactly). We will search in all mailboxes. The search setup will look like this:

Example 2. It is necessary to find a letter from the author (sender) D. Pecherkin, which contains the phrase in the text "how to make money in affiliate programs", but does not contain the word "mailing". It is also known that the letter is in the Yandex mailbox. The settings will be as follows:

The Bat! Hotkeys

The Bat! has a set for a quick call of a particular function. The most commonly used combinations are:

  • create a letter - Ctrl + N
  • get new mail - F2
  • get mail for all mailboxes at once - Alt + F2
  • send mail - Shift + F2
  • mailbox properties - Alt + Enter
  • quick reply to a letter - Ctrl + Enter
  • template call - Ctrl + space
  • creating a sorting rule - Shift + Ctrl + F
  • search - F7
  • address book - F8

See all hotkeys of The Bat! can be found in the program menu opposite the corresponding commands.

The program also has the ability to set your own hotkey combinations through the menu "Properties" / "Settings" / section "Other options" / "System keys".

That's all I wanted on this moment tell you about mail program The Bat! I hope the information in this lesson will be useful to you and will help you to work more productively with this wonderful program!