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

62,256

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 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.

Solution 5

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.

Share:
62,256
gluon
Author by

gluon

Updated on September 17, 2022

Comments

  • gluon
    gluon over 1 year

    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?

  • gluon
    gluon over 14 years
    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
  • ManiacD
    ManiacD over 14 years
    you need a backslash after ?svn for it to work that signifies to exclude everything underneath the .svn directory. -xr!?svn\*
  • ManiacD
    ManiacD over 14 years
    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
  • släcker
    släcker over 14 years
    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 ;)
  • gluon
    gluon over 14 years
    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.
  • Paul
    Paul almost 14 years
    Thanks for the export tip. Available on TortoiseSVN menu too.
  • Natalie
    Natalie over 10 years
    And 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\*
  • Valerio
    Valerio over 5 years
    asterisk part is the most important. This should get more upvotes.