How to access original files from before a symlink gets updated, which have since been moved to another dir

Luke Cousins asked:

We have a website and our deployment process goes somewhat like the following (with lots of irrelevant steps excluded)

echo "Remove previous, if it exists, we don't need that anymore"
rm -rf /home/[XXX]/php_code/previous

echo "Create the current dir if it doesn't exist (just in case this is the first deploy to this server)"
mkdir -p /home/[XXX]/php_code/current

echo "Create the var_www dir if it doesn't exist (just in case this is the first deploy to this server)"
mkdir -p /home/[XXX]/var_www

echo "Copy current to previous so we can use temporarily"
cp -R /home/[XXX]/php_code/current/* /home/[XXX]/php_code/previous/

echo "Atomically swap the symbolic link to use previous instead of current"
ln -s /home/[XXX]/php_code/previous /home/[XXX]/var_www/live_tmp && mv -Tf /home/[XXX]/var_www/live_tmp /home/[XXX]/var_www/live

# Rsync latest code into the current dir, code not shown here

echo "Atomically swap the symbolic link to use current instead of previous"
ln -s /home/[XXX]/php_code/current /home/[XXX]/var_www/live_tmp && mv -Tf /home/[XXX]/var_www/live_tmp /home/[XXX]/var_www/live

The problem we are having and would like help with is that, the first thing any website page load does is work out the base dir of the application and define it as a constant (we use PHP). If then during that page load a deployment occurs, the system tries to include() a file using the original full path and will get the new version of that file. We need it to get the old one from the old dir which has now moved as in:

  1. System starts page load and determines SYSTEM_ROOT_PATH constant to be /home/[XXX]/var_www/live or by using PHP’s realpath() it could be /home/[XXX]/php_code/current.

  2. Symlink for /home/[XXX]/var_www/live get updated to point to /home/[XXX]/php_code/previous instead of /home/[XXX]/php_code/current where it did originally.

  3. System tries to load /home/[XXX]/var_www/live/something.php and gets /home/[XXX]/php_code/current/something.php instead of /home/[XXX]/php_code/previous/something.php

I’m sorry if that is not explained very well. I’d really appreciate some ideas on how to get around this problem if someone can. Thank you.

My answer:


current should not be an actual directory containing your files, but a symbolic link. This is the way capistrano does it:

  1. Create a directory named with the current date.
  2. Deploy the new files to that directory.
  3. Change the current symlink to point to the new directory.
  4. Delete any old unwanted directories.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.