Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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 P!s 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 %ersion)
&t first" %ariables are declared including the folders that are rele%ant for the backup. The
'()-*ariable is the folder in which the 7-zip archi%es of the backup will be stored.
The +&,--%ariables store the name of the 7-zip backup files.
Then a for-loop ist used to delete pre%iously 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 ob.ecti%e of the batch" they are
there for some extra delay. /lowing down the run of the batch in the command line window
(0indows command processor) makes it look a bit better.
&t last" all newly created 7-zip files containing the backup will be transferred onto an
external hard dri%e using xcopy (t:).
Code:
@echo off
rem Example batch 7za file
set DIR=d:\Documents
set DIR2=c:\Backup
set DIR=d:\B!"#$%&I'E(
set )!*E=c:\Backup\Backup%+date+,7z
set )!*E2=c:\Backup\Backup2%+date+,7z
for -R +di.2+ ++f in /0,7z 0,1p1 0,zip2 do /
del ++f 2
+za+ a %m3=44*d %mx5 %t7z %mmt=on %mhe %mmem=672m +)!*E+ +DIR+ @
+DIR+\list,txt %x.80,pdf %p72
ping %n 6 727,6,6,6 9 nul
+za+ a %m3=':*!2 %mx5 %t7z %mmt=on %mhe +)!*E2+ +DIR+\0,pdf %. @
+DIR+\list2,txt %p72
ping %n 6 727,6,6,7 9 nul
dir -b +DIR2+
ping %n 727,;,7,< 9 nul
xcopy +DIR2+\0,7z t:\Backup\73,3,3,2\ -& -= -> -#
ping %n 727,;,;, 9 nul
pause ? exit 9 nul
Using 7za:
7z a %m3=44*d %mx5 %t7z %mmt=on %mhe %mmem=672m +)!*E+ +DIR+ @
+DIR+\list,txt %x.80,pdf %p72
7z a
The 1a1 switch tells 7z to archi%e something.
-m0=PPMd
PP,d is an archi%ing algorithm and good for archi%ing text documents. /o PP,d will be
used here.
-mx9
/ets the strength of the compression le%el" 2 is maximum here.
-t7z
This switch sets the data format to 7z. &lternati%ely it could be zip# -tzip or a '/3 image#
-tiso and so on.
-mmt=on
-nables multithreading (what most modern 4P5s support) if it isn6t already on by default.
'f it is" it can be omitted.
-mhe
This switch enables the encryption of the header of the archi%e.
-mmem=25m
/ets the amount of memory that is used for the compression method PP,d. efault is )7.
!"#M$!
The %ariable" declared abo%e in the code" stands for Backup%+date+,7z (the name of the
7zip file) including the current date (8date8). The file is sa%ed in #:\Backup.
!%&'!
The %ariable for d:\Documents,
@%DIR3%\list.txt -xr!*.pdf
/ince ' already included the folder d:Documents for the archi%ing" ' also want to include
another folder" that is rele%ant to me for a backup. 0ith ( ' add another one" written in a
textfile called )i*t+txt. 'ts located in d#9:&T4;-!'<-/" for which ' ha%e already declared a
%ariable called '(=. /o 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 archi%ed.
/o" in brief" ' want to archi%e all contents of the folder d:\Documents as well as the one in
the list.txt-file (which is d:\(tudies) except all pdf-files in the folders.
-p-2.
/ets a password for the encrypted archi%e. +ote" there is no space between the password
and the switch. >)= is the password for the archi%e to gain access to the files.
+za+ a %m3=':*!2 %mx5 %t7z %mmt=on %mhe +)!*E2+ +DIR+\0,pdf %. @
+DIR+\list2,txt %p72
The same as abo%e but with <?,&) as algorithm for the compression. :ut this time only
pdf-files shall be archi%ed and no other filetypes. & second folder is included by adding @
%DIR3%\list.txt to the code. The contents for list).txt is# d:\(tudies\0,pdf,
The -r switch enables the recurse directories-option. !or me" 't did not work without that
switch.
0hen finished" there are two 7-zip archi%es namely Backup%+date+,7z and Backup2%
+date+,7z (The +date+ is replaced with the current date such as @A.)B.)@>7).
3ne archi%e contains all files without pdf-files of two folders" and the other one contains
only pdf-files of two folders.

You might also like