Find the answer to your Linux question:
Results 1 to 9 of 9
Oftentimes, I want to restore a directory from a tar.gz backup file. When I extract the backup tar.gz, is there a way to make it only replace newer files in ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    8

    replace newer files when extracting from tar

    Oftentimes, I want to restore a directory from a tar.gz backup file.

    When I extract the backup tar.gz, is there a way to make it only replace newer files in the target directory?

    Currently, I have only been able to achieve this by deleting the entire target directory and extracting the entire backup file. This is time consuming because the backups are sometimes hundreds of gigabytes.

    Thanks in advance.

  2. #2
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    From the man page for tar:
    -k, --keep-old-files
    don't replace existing files when extracting

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    8
    so, i don't think this actually works as it simply wouldn't overwrite any of the files in the directory i am trying to restore to an earlier state.

  4. #4
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    Try this, also from the man page, which you might want to read:

    --keep-newer-files
    don't replace existing files that are newer than their archive copies

  5. #5
    Linux Guru Lazydog's Avatar
    Join Date
    Jun 2004
    Location
    The Keystone State
    Posts
    2,281
    I think the OP is looking for

    Code:
           --overwrite
                  overwrite existing files and directory metadata when extracting

    Regards
    Robert

    Linux
    The adventure of a life time.

    Linux User #296285
    Get Counted

  6. #6
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    That wasn't what I understood the question to be, but maybe...

  7. #7
    Just Joined!
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by sgosnell View Post
    Try this, also from the man page, which you might want to read:

    --keep-newer-files
    don't replace existing files that are newer than their archive copies
    no, this doesn't work either because the newer files are the ones i want to replace.

    --overwrite will accomplish my end goal, but it is no faster than deleting the directory and re-extracting it.

    the issue is that since the last backup, maybe only 10-20 files out of 20 million were modified. it is inefficient to have to overwrite 20 million files when really only 10-20 need to be restored to their previous state. does anybody know of a way that this can be accomplished in a more efficient manner?

  8. #8
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    I think the way to approach this is to delete the newer files, and that can be done based on the date, and then run either tar or better, rsync, and have it ignore existing files. That way only files that don't exist, because you just deleted them, will be copied.

    Are you sure you're going the correct direction with this tar? I've never seen a need to replace modified files with older ones, but if that's what you really need to do, the only way I know to do that is to delete the newer files first, and then transfer only missing files. If you don't need compression, I think rsync is a better tool for this. You could easily make up a script that would delete files newer than the original backup, then rsync only missing files. That shouldn't take long.

    Find is probably the tool you want to do the deletions. Read the man page for find, and Delete Files In Directory By Date - Linux / UNIX Forum and Delete Files Older Than x Days on Linux - How-To Geek. Mtime may be better than ctime, depending on what is happening to the files you want to delete. Use -10 instead of +10 (or whatever number) to find newer files instead of older ones. Finding the required number is left as an exercise for the reader.
    Last edited by sgosnell; 01-29-2011 at 08:03 PM.

  9. #9
    Linux Guru Lazydog's Avatar
    Join Date
    Jun 2004
    Location
    The Keystone State
    Posts
    2,281
    Quote Originally Posted by nocloud View Post
    does anybody know of a way that this can be accomplished in a more efficient manner?
    You are most likely going to have to write a script that compare all the files and then only extract what has changed.

    Regards
    Robert

    Linux
    The adventure of a life time.

    Linux User #296285
    Get Counted

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...