Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a code base on my production server which is synced from a remote directory. One thing I want to improve about the rsync process is to make it make a local copy first before it takes place and over write the original files. Something like

before syncing files from remote :
cp -rf production_code_base production_code_base_old
now syncing remote changes to the production directory

Is there a way to do that?

share|improve this question
up vote 2 down vote accepted

besides the --backup-dir option (which i haven't used, so i'm not sure if it's appropriate), you could do a 'hard link backup' with cp -al production_code_base production_code_base_old. I'ts very fast since it doesn't copy the data, and it doesn't use up space for unchanged files.

Note: This is safe in this specific case because rsync never opens an existing file for writing, it writes everything to temporary files and finishes with a mv after that. Other transfer systems might overwrite existing files, destroying the hard-link 'backup'.

share|improve this answer
"Never" unless you give it the --inplace flag. Rsync is very flexible. :) – mattdm Dec 15 '10 at 3:16
@mattdm: you're right, of course. rsync can do almost anything, including shooting your own foot. – Javier Dec 15 '10 at 14:23

Well, there's lots of ways to do that. Like, the cp command you just posted. Or you can use the --backup-dir option to rsync.

But what are you trying to accomplish? Might it better be done with proper version control?

share|improve this answer
I'm using rsync for code deployment. In case there is something going wrong with the new code. I need to backup the previous code first. The code is actually version controlled by the GIT tool. But we don't want to put the .git/ stuff to production along with the real code. – Shawn Dec 15 '10 at 1:58
But, isn't the previous code also already in git? – mattdm Dec 15 '10 at 15:47

Your Answer


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.