defect122

Batch Programming - Backup Batch with 7 zip

Jun 30th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Batch – Programming
  2.  
  3. Backup
  4.  
  5. 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.
  6.  
  7. 7za is not included in the regular 7zip-installation, it can be downloaded from this link:
  8. http://www.7-zip.org/download.html
  9. (command line version)
  10.  
  11. 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.
  12. The NAME-variables store the name of the 7-zip backup files.
  13.  
  14. Then a for-loop ist used to delete previously added backups (of course that can be omitted, if not needed).
  15.  
  16. The 7-zip commands are explained in more detail below.
  17.  
  18. 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.
  19.  
  20. At last, all newly created 7-zip files containing the backup will be transferred onto an external hard drive using xcopy (t:).  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. Code:
  47.  
  48. @echo off
  49.  
  50. rem Example batch 7za file
  51.  
  52. set DIR=d:\Documents
  53. set DIR2=c:\Backup
  54. set DIR3=d:\BATCH-FILES
  55. set NAME=c:\Backup\Backup-%date%.7z
  56. set NAME2=c:\Backup\Backup2-%date%.7z
  57.  
  58. for /R %dir2% %%f in (*.7z *.gpg *.zip) do (
  59. del %%f )
  60.  
  61. %za% a -m0=PPMd -mx9 -t7z -mmt=on -mhe -mmem=512m %NAME% %DIR% @%DIR3%\list.txt -xr!*.pdf -p123
  62.  
  63. ping -n 5 127.5.5.5 > nul
  64.  
  65. %za% a -m0=LZMA2 -mx9 -t7z -mmt=on -mhe %NAME2% %DIR%\*.pdf -r @%DIR3%\list2.txt -p123
  66.  
  67. ping -n 5 127.5.5.7 > nul
  68.  
  69. dir /b %DIR2%
  70.  
  71. ping -n 3 127.4.1.6 > nul
  72.  
  73. xcopy %DIR2%\*.7z t:\Backup\10.0.0.2\ /F /G /Y /C
  74.  
  75. ping -n 3 127.4.4.3 > nul
  76.  
  77. pause & exit > nul
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. Using 7za:
  97.  
  98. 7z a -m0=PPMd -mx9 -t7z -mmt=on -mhe -mmem=512m %NAME% %DIR% @%DIR3%\list.txt -xr!*.pdf -p123
  99.  
  100. 7z a
  101. The "a" switch tells 7z to archive something.
  102.  
  103. -m0=PPMd
  104. PPMd is an archiving algorithm and good for archiving text documents. So PPMd will be used here.
  105.  
  106. -mx9
  107. Sets the strength of the compression level, 9 is maximum here.
  108.  
  109. -t7z
  110. This switch sets the data format to 7z. Alternatively it could be zip: -tzip or a ISO image: -tiso and so on.
  111.  
  112. -mmt=on
  113. Enables multithreading (what most modern CPUs support) if it isn´t already on by default. If it is, it can be omitted.
  114.  
  115. -mhe
  116. This switch enables the encryption of the header of the archive.
  117.  
  118. -mmem=256m
  119. Sets the amount of memory that is used for the compression method PPMd. Default is 24.
  120.  
  121. %NAME%
  122. 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.
  123.  
  124. %DIR%
  125. The variable for d:\Documents.
  126.  
  127. @%DIR3%\list.txt -xr!*.pdf
  128. 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.
  129. 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.
  130.  
  131. -p123
  132. 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.
  133.  
  134.  
  135.  
  136.  
  137. %za% a -m0=LZMA2 -mx9 -t7z -mmt=on -mhe %NAME2% %DIR%\*.pdf -r @%DIR3%\list2.txt -p123
  138.  
  139. 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.
  140. The -r switch enables the recurse directories-option. For me, It did not work without that switch.
  141.  
  142.  
  143.  
  144. 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).
  145. 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