Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a simple way to backup files that have changed in the last 24 hours and creating a daily 7zip file ( I don't need permssion or users just files ).

Using the following command I can pass a list of changed file to 7Zip, and this works well. However no path information of the changed files is stored in 7zip.

/usr/bin/find /local-storage/public_html/ -type f -size -25M -mtime -1 -print | /usr/bin/xargs -n 1 /usr/bin/7z a -t7z -m0=lzma -mx=9 /local-storage/public_html_2013-09-03.7z

So if I now try to extract that 7zip file it is all just in 1 directory and no paths.

Hoping someone here can point out what I have missed :-)

share|improve this question
What command are you using to extract? – jjlin Sep 3 '13 at 17:01
7z x public_html_2013-09-03.7z – StephenTrapped Sep 3 '13 at 17:05

I'm not sure that 7z can do that, that seems to be what they mean in this section of the man page:

   On Linux/Unix, in order  to  backup  directories  you
   must use tar :
    -  to backup a directory  : tar cf - directory | 7za a -si directory.tar.7z
    -  to  restore  your  backup  :  7za  x  -so  directory.tar.7z | tar xf -

Instead, I would suggest creating an uncompressed tar ball and then compressing it with 7z:

find /local-storage/public_html/ -type f  -mtime -1 -print0 | 
xargs --null tar rf foo.tar && 
7za a -t7z -m0=lzma -mx=9 /local-storage/public_html_2013-09-03.tar.7z foo.tar &&
rm foo.tar

This will create a file called public_html_2013-09-03.tar.7z which will contain the tar ball foo.tar. You can then extract everything and keep the right paths (directories will be created if not present and files will be overwritten so make sure you know what you're doing):

7z x -so foo.tar.7z | tar rf -
share|improve this answer
That tar cf should be a tar rf in case xargs needs to make more than one tar call. But really, if you're going to be creating a tarball as the end result, you may as well just use xz for compression. – jjlin Sep 3 '13 at 18:55
@jjlin good point, answer edited. As for the compression I agree completely, using 7z is not worth the trouble but perhaps the OP just needs the highest compression available for some reason. – terdon Sep 4 '13 at 13:07
Is this a Linux-specific limitation? 7Zip in Windows stores my full path, which can be used by extracting with the x command instead of the e command. – Dane Sep 4 '13 at 14:28

It seems 7z isn't particularly well-suited to what you're trying to do. Nevertheless, if you're set on using 7z, you can still do what you need by creating a temporary tree of files and creating the archive from that. For example, you could put this list of commands into a shell script:

cd /tmp
find /local-storage/public_html -type f -size -25M -mtime -1 -print0 | xargs -0 tar -rf public_html_2013-09-03.tar
tar -xf public_html_2013-09-03.tar
7z a -r -t7z -m0=lzma -mx=9 /local-storage/public_html_2013-09-03.7z local-storage
rm -rf local-storage public_html_2013-09-03.tar

That's the basic idea. Note the -r passed to 7z. You can enhance this with better temp directory handling, generating a date string automatically, and so forth.

share|improve this answer

Thanks everyone for your input. I found a method that whilst not great works for me in my situation. I have used some ideas from posters here and created the following bash script.

YESTERDAY=`date -d "1 days ago" '+%Y-%m-%d'`
cd /local-storage/public_html/
/usr/bin/find . -type f -size -25M -mtime -1 > /tmp/"$TIMESINCELINUXSTART"-changed-files.txt
/bin/mkdir -p /tmp/"$TIMESINCELINUXSTART"/public_html
/usr/bin/rsync --files-from=/tmp/"$TIMESINCELINUXSTART"-changed-files.txt /local-storage/public_html /tmp/"$TIMESINCELINUXSTART"/public_html/
/usr/bin/7z a -t7z -m0=lzma -mx=9 /local-storage/snapshots/public_html-changes-during-"$YESTERDAY".7z /tmp/"$TIMESINCELINUXSTART"/public_html
/bin/rm /tmp/"$TIMESINCELINUXSTART"-changed-files.txt
/bin/rm -rf /tmp/"$TIMESINCELINUXSTART"

If anyone has improvements or comments on this, you are invited to comment. The only addition I would like to add it to not create an 7zip file if no changed files are found.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .