Linux - transfer all files modified after a specific date to a new harddrive

2

I recently had my second hard-drive of the past semester begin to fail on me and become unable to ghost to a new drive. I still had the first hd that failed so we ghosted that drive but now I need to get all the files and things that have changed since the date of the first ghosting (first failed hd -> second failed hd) onto the new drive (all files on second failed hd since a date -> third working hd). Is there an easy way to script this in the linux terminal or use another program?

I've looked into using the following commands but they haven't been successful and fail after the first few folders have been archived:

find . -type f -mtime -58 |xargs tar --no-recursion -czvf allmodified.tgz

tar czvf allmodified.tgz `find . -type f -mtime -58`

The find code works and locates all the files on the drive I need but I still can't find a way to get them to archive with file structure so that I can unpack the files on the new drive (third working hd) and they will be preserved where they were in the past 2 months.

I'm not the best at describing my problems so if you have confusion please ask questions and I'll do my best to answer them.

Any hints help, thank you so much!

Samuel Kane

Posted 2013-11-15T18:23:55.717

Reputation: 21

Answers

2

They probably fail because you haven't safeguarded against whitespace in filenames in your second example. The invocation is a bit different:

find . -type f -mtime -58 -print0 | tar -czvf allmodified.tar.gz --null -T -

Here, find outputs file paths terminated by NUL characters. They are piped to tar, which reads input from stdin (-T -) delimited by null (--null). The --no-recursion isn't necessary since you will not include directories themselves in your tarball.

See also: GNU tar 1.27: 6.3 Reading Names from a File

You could then extract the archive from the same directory you took it, and it'd place the files in the directories they belong to. Note that tar will overwrite existing files upon extraction. To prevent that, use the --skip-old-files option.


As for your second example, it will fail if a file or path contains whitespace, because tar sees the two files foo bar and baz (which find outputs) as three, i.e. tar foo bar baz. The proper way to deal with this is to pipe the output of find with -print0 to the next command, if that command has a switch to read from NUL-delimited input (which most GNU utilities do).

slhck

Posted 2013-11-15T18:23:55.717

Reputation: 182 472

Using this method would I then be able to 'unzip' the tar on my new drive and have it place files accordingly? – Samuel Kane – 2013-11-15T18:48:53.707

Yes. Just tested it on a set of sample files. Note that your tarball has to be extracted from the same root directory as it was tarred in. Also, tar will overwrite existing files. To prevent that, use --skip-old-files when extracting. – slhck – 2013-11-15T18:53:16.327

So if I tarred it in the /media/ubuntu/OS directory of one, then I would need to move the tar to the /media/unbuntu/OS1 folder for the other drive? Could you also elaborate on how the --skip-old-files works when extracting (I'm really a novice at this), I'm not 100% confident I'm going to do this correctly and I'm going to ruin a lot of things. Thank you so much for your help – Samuel Kane – 2013-11-15T18:59:04.997

If you ran find … | tar … while you were in /media/ubuntu/OS/, then cd /media/ubuntu/OS1/ and tar -xcvf allmodified.tar.gz --skip-old-files, then only the missing files in OS1 are created from your OS backup. If you're unsure what will happen, try creating a smaller test archive first. Or list the contents of the archive with tar -tvf allmodified.tar.gz and imagine that tar creates each of those with the path it spits out (e.g. if tar -tvf writes ./foo/bar, then the file /media/ubuntu/OS1/foo/bar will be created). In any case, I'd recommend a small test first. – slhck – 2013-11-15T19:13:28.933

fantastic! thank you so much! I'll let you know the results once I've done the test and the whole project – Samuel Kane – 2013-11-15T19:21:21.973

No luck getting this to work on my disk, it just creates a .tar with nothing it in but doesn't update or display any further progress. I'm open to more suggestions! Thank you for the help so far! – Samuel Kane – 2013-11-15T20:15:58.233

Does find . -type f -mtime -58 print the files you want? Can you try again with everything in the command on one line? It might not have worked if there's a newline in your command when you paste it. – slhck – 2013-11-15T20:38:38.017

I ran this originally all on one line and the find... command does work to find all the files I want – Samuel Kane – 2013-11-15T21:03:00.900

Weird. You can come to chat if you want: http://chat.stackexchange.com/rooms/11524/discussion-between-slhck-and-samuel-kane – just ping me with @slhck and we'll figure this out.

– slhck – 2013-11-15T21:21:03.337