How do I use 7-zip to backup files, but exclude some directories

To exclude all .svn directories you need to add the -xr!?svn\* switch

For example the following will create a backup of the C:\Project\To\Backup directory excluding any folders that satisfy ?svn:

"C:\Program Files\7-Zip\7z.exe" a -r -tzip -y -xr!?svn\* Project.zip C:\Project\To\Backup\*

Instead of using 7-Zip to exclude the .svn (or potentially _svn) folders, I would recommend using the svn export command (use svn.exe from SlikSVN) to export the working copy to a temporary folder:

svn export C:\Path\To\WC C:\Backup\Staging

Then use 7-Zip as follows:

7z.exe a "C:\Parth\To\Archive" "C:\Backup\Staging\*" -bd t7z -v2g -r

Then delete the staging folder.

This is what I do to backup my local working copies.


You can exclude files with 7zip using a list of files or directories:

/path/to7Zip/7z a -bd f:/backup/backup_2009-08-23_daily.zip home \
          '-xr@\path\to\backup_daily_exclude.lst'

The exclude file looks like:

home\Photos\iPod*
home\dhltd\*
BlogMatrix\Sparks\db\*.archive
home\eclipse\*
.svn

The key is the -xr and in particular the "r" which indicates apply the exclude list recursively, to each level of the directory. You may want to use 2 exclude file lists one for absolute and one for recursive exclusions. The above is from a bash script that runs in cygwin.


When I used

"C:\Program Files\7-Zip\7z.exe" a -r -ttar -xr!?git\* aufs2-util.tar aufs2-util\*

it ended up adding the .git directory which I didn't want, changing it to

"C:\Program Files\7-Zip\7z.exe" a -r -ttar -xr!?git\ aufs2-util.tar aufs2-util\*

got the desired result.


When I used

7z a "D:\codebase\w.7z" "D:\codebase\Edison\otm\Webapp" -t7z -mx0 -xr!WEB-INF\*

the WEB-INF directory was not excluded. Adding an asterisk before the dir name

7z a "D:\codebase\w.7z" "D:\codebase\Edison\otm\Webapp" -t7z -mx0 -xr!*WEB-INF\*

Got the desired result.