Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have written this backup script that looks in a file and copies recent files into a folder.

#!/usr/bin/bash

# the number of days to do the backup for.
days=2;

# the files to backup.
location[0]='/opt/location'

# the location to copy the file to
copyLocation='/users/me/Backup/firstBackupScriptTry'

# preform the back up
for i in ${location[*]}
do
        find $i \! -name '*.class' -mtime -$days \! -type d -exec cp {} $copyLocation \;
done

It works but it is not that useful.

I would prefer the script to preserve the directory structure when it copies. Ie I would like it to do: cp -r from to but only copy the recent files.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

A way would be to modify the file names that you get when running find. So in your loop, having a matched file name in $filename you shall:

  1. In $filename, replace the leading $i with nothing
  2. find out the $dir_name in the result of 1 by using dirname
  3. append a trailing $copyLocation to $dirname and use it as argument for mkdir -p to create missing directories
  4. copy $filename to "$copyLocation/$dirname"

I'm also going to suggest yet another file sync alternative: unison. It is easier to use than rsync.

share|improve this answer

rsync is made for this task. Check the examples page for usage.

share|improve this answer
    
+1: helpful answer. I probably will end up using that, looks pretty good. However I am curious how to finish my script. Any ideas? –  sixtyfootersdude Feb 10 '10 at 19:35
    
rsync -a --include "/" --include='.class' --exclude="*" /opt/location/ /users/me/Backup/firstBackupScriptTry –  Justin Smith Feb 10 '10 at 20:53
    
which is to say that one line duplicates the whole of your script (instead of copying changes in the last two days, it copies all changes since last backup to the same location) –  Justin Smith Feb 10 '10 at 20:55
    
Since it looks like you're backing up code, you should also consider the -C option for rsync which excludes things like CVS/svn repository meta files, backup files and compiled files. –  Doug Harris Feb 10 '10 at 21:40
    
@doug the command line I posted only copies .class files and directories, nothing else whatsoever, as was specified in his original. But for a less picky backup, -C is definitely helpful. –  Justin Smith Feb 10 '10 at 22:08

A little off-topic perhaps, but you may want to take a look at BoxBackup. I used rsync scripts for a long time before moving to BoxBackup, and it really makes things easier - especially the "housekeeping"...

share|improve this answer
    
Hmm, thanks for the pointer. looks useful but not exactly what I am looking for. Any idea how to fix/finish my script? –  sixtyfootersdude Feb 10 '10 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.