1

I am looking to write a Linux shell script in order to perform a backup and restore of all of the source code in my project folder (.c, .cpp, .java, etc)

My script deletes all of these source code files and I want to be able to easily restore the files as well if need be.

Here is what I am doing now:

#copy the entire directory to a backup folder
cp -r $CLEANUP_PATH $BACKUP_PATH

#delete the project and copy from backup to restore source code
rm -rf $CLEANUP_PATH
cp -r $BACKUP_PATH $CLEANUP_PATH

The project is around 600MB, so it takes a long time when I perform these actions.

How would I be able to restore only the source code (rather than the entire directory) from a backup and ensure that each file is restored to the specific directories that each file belongs in?

5
  • 3
    Why don't you just use source control? Commented May 20, 2011 at 18:38
  • Does cp -r copy dot files? i.e. .exrc, etc? Commented May 20, 2011 at 18:44
  • @Carl - The intent here is to remove source code to protect intellectual property. The backup/restore functionality would mostly be for internal testing purposes. Correct me if I'm mistaken, but I believe that using source control would be a considerably slower approach for this. Commented May 20, 2011 at 18:52
  • @shellter - One of the things that I tried was: find $CLEANUP_PATH -iname "*.c" -exec cp {} {}.bkup \; However, this does not maintain the directory that the files came from when I go to restore from the backup. cp -r copies a directory recursively including every single file in the root directory and all subdirectories. Commented May 20, 2011 at 18:55
  • Why are you copying and then removing? If the destination and the source are on the same physical device, using mv will be a lot faster. Commented May 22, 2011 at 7:56

2 Answers 2

4

Consider using tar: it remembers everything about the files, including paths, permissions, and dates. If you source code is inside a few directories, you can extract those directiories by name from the tar file.

Sign up to request clarification or add additional context in comments.

4 Comments

What would the syntax look like if I want to locate and backup all .c files from /home/username/PROJECT_FOLDER for example?
tar zcvf /tmp/mybackup.tar.gz /home/username/PROJECT_FOLDER/*.c
Thanks, but this ended up being the syntax that I needed: tar -zcvf ${BACKUP_PATH}/c_backup.tar find ${CLEANUP_PATH} -name "*.c" How do I extract these files to restore them to the directories they came from without overwriting other existing files in those directories?
Nevermind, I figured it out. tar -xzvf c_backup.tar -C ${CLEANUP_PATH}
2

Consider using version control. It remembers everything about the files, including paths, permissions, dates, and previous revisions. This will not only prevent disaster recovery, but it will make you a better developer (for any number of reasons).

git init .
git add `find . -type f | egrep '\.(cpp|c|java)$'`
git commit -m "initial version"

The of course assumes you have stuff in the directory you do not want committed/saved. If you want to save everything (no binaries/library/other generated or generatable files please!) then you can git add .

After you do some work, check in the changes

git status
git commit -a -m "Descriptive comment about changes"

You can also make clones of the git repository in case you delete the directory. Offsystem clones in case you delete the machine.

git clone ssh://hostname/path/to/repository

2 Comments

We use TortoiseSVN where I work. I'm not sure if it has the same capabilities as git that you mention, but I'll look into it. Thanks.
While I promote git over svn, either is so far in advanced of "tar" and "cp" to be indistinguishable. The subversion commands will vary and you might need help getting a new repository set up for your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.