Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Batch – Programming
- Backup
- This batch file uses the 7-zip command line tool (7za) to back up some data and shows how to use the encryption functions of 7-zip. The data being backupped consists of textfiles and PDFs and no binary data.
- 7za is not included in the regular 7zip-installation, it can be downloaded from this link:
- http://www.7-zip.org/download.html
- (command line version)
- At first, variables are declared including the folders that are relevant for the backup. The DIR2-Variable is the folder in which the 7-zip archives of the backup will be stored.
- The NAME-variables store the name of the 7-zip backup files.
- Then a for-loop ist used to delete previously added backups (of course that can be omitted, if not needed).
- The 7-zip commands are explained in more detail below.
- The four ping commands are not needed at all for the objective of the batch, they are there for some extra delay. Slowing down the run of the batch in the command line window (Windows command processor) makes it look a bit better.
- At last, all newly created 7-zip files containing the backup will be transferred onto an external hard drive using xcopy (t:).
- Code:
- @echo off
- rem Example batch 7za file
- set DIR=d:\Documents
- set DIR2=c:\Backup
- set DIR3=d:\BATCH-FILES
- set NAME=c:\Backup\Backup-%date%.7z
- set NAME2=c:\Backup\Backup2-%date%.7z
- for /R %dir2% %%f in (*.7z *.gpg *.zip) do (
- del %%f )
- %za% a -m0=PPMd -mx9 -t7z -mmt=on -mhe -mmem=512m %NAME% %DIR% @%DIR3%\list.txt -xr!*.pdf -p123
- ping -n 5 127.5.5.5 > nul
- %za% a -m0=LZMA2 -mx9 -t7z -mmt=on -mhe %NAME2% %DIR%\*.pdf -r @%DIR3%\list2.txt -p123
- ping -n 5 127.5.5.7 > nul
- dir /b %DIR2%
- ping -n 3 127.4.1.6 > nul
- xcopy %DIR2%\*.7z t:\Backup\10.0.0.2\ /F /G /Y /C
- ping -n 3 127.4.4.3 > nul
- pause & exit > nul
- Using 7za:
- 7z a -m0=PPMd -mx9 -t7z -mmt=on -mhe -mmem=512m %NAME% %DIR% @%DIR3%\list.txt -xr!*.pdf -p123
- 7z a
- The "a" switch tells 7z to archive something.
- -m0=PPMd
- PPMd is an archiving algorithm and good for archiving text documents. So PPMd will be used here.
- -mx9
- Sets the strength of the compression level, 9 is maximum here.
- -t7z
- This switch sets the data format to 7z. Alternatively it could be zip: -tzip or a ISO image: -tiso and so on.
- -mmt=on
- Enables multithreading (what most modern CPUs support) if it isn´t already on by default. If it is, it can be omitted.
- -mhe
- This switch enables the encryption of the header of the archive.
- -mmem=256m
- Sets the amount of memory that is used for the compression method PPMd. Default is 24.
- %NAME%
- The variable, declared above in the code, stands for Backup-%date%.7z (the name of the 7zip file) including the current date (%date%). The file is saved in C:\Backup.
- %DIR%
- The variable for d:\Documents.
- @%DIR3%\list.txt -xr!*.pdf
- Since I already included the folder d:Documents for the archiving, I also want to include another folder, that is relevant to me for a backup. With @ I add another one, written in a textfile called list.txt. Its located in d:\BATCH-FILES, for which I have already declared a variable called DIR3. So in the list.txt you simply put the path to another folder or some single filenames. The -xr! switch excludes any pdf-file from being archived.
- So, in brief, I want to archive all contents of the folder d:\Documents as well as the one in the list.txt-file (which is d:\Studies) except all pdf-files in the folders.
- -p123
- Sets a password for the encrypted archive. Note, there is no space between the password and the switch. 123 is the password for the archive to gain access to the files.
- %za% a -m0=LZMA2 -mx9 -t7z -mmt=on -mhe %NAME2% %DIR%\*.pdf -r @%DIR3%\list2.txt -p123
- The same as above but with LZMA2 as algorithm for the compression. But this time only pdf-files shall be archived and no other filetypes. A second folder is included by adding @%DIR3%\list2.txt to the code. The contents for list2.txt is: d:\Studies\*.pdf.
- The -r switch enables the recurse directories-option. For me, It did not work without that switch.
- When finished, there are two 7-zip archives namely Backup-%date%.7z and Backup2-%date%.7z (The %date% is replaced with the current date such as 06.28.2014).
- One archive contains all files without pdf-files of two folders, and the other one contains only pdf-files of two folders.
Advertisement
Add Comment
Please, Sign In to add comment