flag of the United Kingdom

One-to-Many File Distribution

Date:  Mon, 3rd-Jan-2005prevnext

Tags: Shell, SysAdmin, Unix

Computer Room

I recently had to update some CGI scripts duplicated within thirty or so virtual servers on one physical box. The thought of manually FTPing the revised scripts into each website's file-system wasn't a very attractive one. So I wrote a little shell script to automate the task.

The script completed the tedious job in seconds and I thought I'd share it here as it might be useful to someone...

This task was easily scriptable because of all the virtual servers employed the same scripts and directory structure. I uploaded the revised scripts into a directory called "/tmp/patches" on the server and configured their permissions as appropriate before running the distribution script.

The Distribution Script

echo "update.sh: Updating the Scripts..."

# Recursively scan "/usr/local/apache/htdocs" and delete any files that have a prefix of "DSbackup."
find /usr/local/apache/htdocs -type f -name "DSbackup.*" -exec rm -f {} ;

# Change into the "patches" directory.
cd /tmp/patches

# Recursively scan "/usr/local/apache/htdocs" for directories named "cgi-bin". Within the "cgi-bin" directories, identify files whose names match those in "/tmp/patches". Foreach match, rename the destination file with a "DSbackup." prefix.
for update in *.* ; do find /usr/local/apache/htdocs -type d -name "cgi-bin" -exec mv -f {}/$update {}/DSbackup.$update >> /dev/null 2>&1 ; ; done

# Change the permissions on those "DSbackup.*" files so that they are non-executable.
for update in *.* ; do find /usr/local/apache/htdocs -type d -name "cgi-bin" -exec chmod 666 -f {}/DSbackup.$update ; ; done

# Copy the revised files from "/tmp/patches" (the current working directory) to every "cgi-bin" directory within the "/usr/local/apache/htdocs" path. Duplicate the permissions of the source file(s) to those of the destination file(s).
for update in *.* ; do find /usr/local/apache/htdocs -type d -name "cgi-bin" -exec cp -p /tmp/patches/$update {}/. ; ; done

# Finished!
echo "update.sh: Update Complete!"

I uploaded this into "/tmp" as "update.sh" and set its permissions as "0755" (executable). Then I ran it:

cd /tmp

The result: a nice, quick update of a number of virtual servers with minimal effort.

You can comment on this entry, or read what others have written (6 comments).