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

43

25

I would like to use 7-zip to backup a directory, but I would like it to exclude all directories named ".svn" (anywhere in the source tree). Does anybody know if this is possible and in that case how?

Mikael Sundberg

Posted 2009-08-23T09:28:09.653

Reputation: 578

Answers

35

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\*

ManiacD

Posted 2009-08-23T09:28:09.653

Reputation: 526

Thanks ManiacD, but I can't get this to work -xr!?svn* gives me an error and if I try -xr!.svn* it still won't exclude .svn – Mikael Sundberg – 2009-08-23T12:03:44.487

you need a backslash after ?svn for it to work that signifies to exclude everything underneath the .svn directory. -xr!?svn\* – ManiacD – 2009-08-23T14:04:03.840

Don't worry I had heaps of problems initially getting it work as well. The 7-zip exclude switch -x (-xr recurse directory) with ! excludes filenames based on a wildcard search. Without the backslash it is trying to exclude filenames that match ?svn* ie. asvn.log with the \* at the end mean don't include anything underneath a directory matching ?svn – ManiacD – 2009-08-23T14:18:57.297

For details on how to use wildcards have a look at the help file shipped with 7-Zip. Helped me to fix a similar task a few days ago. 7-Zip uses wildcards a bit ... different ;) – släcker – 2009-08-23T15:22:19.640

I got it to work, strange that this answer currently is the only one that hasn't been upvoted. +1 and accepted. Thanks to to all who answered my question. – Mikael Sundberg – 2009-08-23T19:38:33.163

3And if you stumbled here looking for general folder exclusion, note that the ? is to match "period-svn" (.svn), so to to skip everything in folder SkipMe you would use -xr!SkipMe\* – drzaus – 2013-10-16T21:15:55.940

13

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.

Richard Slater

Posted 2009-08-23T09:28:09.653

Reputation: 576

Thanks for the export tip. Available on TortoiseSVN menu too. – Arnold Spence – 2010-07-28T23:00:17.887

12

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.

Tim Desjardins

Posted 2009-08-23T09:28:09.653

Reputation: 175

4

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.

user28989

Posted 2009-08-23T09:28:09.653

Reputation:

3

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.

Ashutosh Jindal

Posted 2009-08-23T09:28:09.653

Reputation: 322

asterisk part is the most important. This should get more upvotes. – Valerio – 2018-10-05T14:55:22.580

0

For me, I was trying to backup a bunch of maven code directories and wanted to exclude any directory named "target":

"C:\Program Files\7-Zip\7z.exe" a -r -tzip -y -xr!*\target\* site.zip C:\code\site\

Relevant exclude parameter was -xr!*\target\*.

Kit Menke

Posted 2009-08-23T09:28:09.653

Reputation: 119