backup script with find and tar

Discussion in 'Mac Programming' started by wrathkeg, Sep 28, 2010.

  1. wrathkeg macrumors newbie

    Feb 1, 2010
    Hi all,

    I have written a fairly simple script to back up files. They key bit is this line of code, which finds everything changed in the last day, and makes a tar file containing all those files:

    find . -ctime -1 | xargs tar --no-recursion -czf $HOME/$TARFILE
    If I change a filename but not the contents, the file isn't picked up by this line of code. I can get 'find' to identify files with changed name by using 'atime' rather than 'ctime'. The problem would be my script would back up everything I had accessed, rather than just those things which had either changed contents or a changed filename. Does anyone have any suggestions as to how to modify the above line to suit my needs? I am trying to avoid a rewrite of the whole script, or adoption of a new method....

    Thanks in advance,

  2. ianray macrumors 6502

    Jun 22, 2010
    Despite your wish to use the same method, I would have to suggest rsync.

    One alternative is to compare the set of files on disk versus the archive, then remove deleted files and add renamed files to the archive. You could write some nice Perl to do this :)

    tar -tzf $HOME/$TARFILE >
    find . -type f >
    echo files on disk, but not in tar
    diff | grep "^<" | cut -b3-
    echo files in tar, but not on disk
    diff | grep "^>" | cut -b3-
  3. willieva macrumors 6502

    Mar 12, 2010
  4. wrathkeg thread starter macrumors newbie

    Feb 1, 2010
    Thanks for the suggestions.

    I had tried to use rsync, but I am trying to back up to Dropbox and I don't have enough space on there to store everything I want to keep backed up, so I just want to backup (upload) things I have changed in the last few days. From time to time I back up everything to an external hard drive.

    As far as I understand it, ctime only tells you when the file itself was last changed, and this includes changes to ownership and permissions, but not to filenames. Seems strange to me, but I'm sure there's a good reason.
  5. wrathkeg thread starter macrumors newbie

    Feb 1, 2010
    it occurs to me that one way round this might be to use the 'touch' command on a file after running 'mv' on it (I do pretty much all of my file manipulation from the terminal). I wonder if I can set up an alias for mv to do that........
  6. cqexbesd macrumors regular

    Jun 4, 2009
    If you kept an uncompressed copy of the tar file locally you might be able to use the -u option to tar. This would just make the tar file bigger each time but you said you backup in full periodically so you could then delete the tar file and start from scratch. You can still compress the tar file before upload.


  7. wrathkeg thread starter macrumors newbie

    Feb 1, 2010
    Great suggestion: thanks. I now have a script like this, which seems to do just what I wanted.
    # makes the dropbox folder if not there; prevents output to terminal
    mkdir -p $HOME/Dropbox/Work
    # sets up the filename
    # changes to the folder you want to search recursively
    cd $HOME/Work
    # makes the tarfile in case it doesn't already exist
    touch $TARFILE
    # finds all recently modified files and tars them: the argument to
    # -ctime specifies how recent things have to be, in days from current
    # time.  Currently set to 7 days.  This means you need to run this at
    # least once per week to keep things backed up.
    find . -ctime -7 | xargs tar --no-recursion -uf $TARFILE
    # goes into Dropbox
    cd $HOME/Dropbox/Work
    # unzips the file
    tar xf $TARFILE
    # changes to the directory containing tar file and deletes it
    # get back to where we were
    cd $PWD

Share This Page