Sponsored Content
Top Forums Shell Programming and Scripting Peer Review File/Folder Script Post 302683849 by rwhite35 on Wednesday 8th of August 2012 05:31:28 PM
Old 08-08-2012
Peer Review File/Folder Script

Hello *nix friends,
I've written a shell script that allow web admin's to copy file/folder from a development site to the production site. It's more or less a poor man SVN.

I'm posting the script here because I was able to get many questions answered through this forum and also, I want to improve this script. It works as is but there is probably more efficiencies that could be incorporated. Finally, I've keep the comments down for brevity.

Code:
#!/bin/sh
# Make copies of files and folders from a dev site to production site
# requires two arguments, the file or folder name ($1) a destination path ($2)
# the third argument ($3) is optional and specifies a search path to
# look for files/folders in sub directories off the root

#### usage examples ####
# with dest and search path: ./copy.sh fileName.ext path/to/dest/ path/to/search
# with dest only: ./copy.sh folderName path/to/dest
# with current dir: ./copy.sh fileName.ext .

#### variables ####
# log stdout and stderr
LOGFILE=copyFile.log
DATE=`date`
# file or folder name to copy
HANDLE="$1"
# destination is the location to copy files and folders
DEST="$2"
# optional - appends a sub directory path to SRC
SEARCH="$3"
# previous is a backup folder of current production file
PREV="previous"
# short variable for development root
SRC="/path/to/development/root/"
# short variable for production root
PROD="/path/to/production/root/"

#### copy process ####
# change director to the development root
cd $SRC

# test if handle is a file and NOT a directory
isFile=$(find $SRC$SEARCH -type f -name $HANDLE -print0)
if [[ -f $isFile ]]; then

     # test if dest was passed as a "." dot (or the site root directory)
     if [[ $DEST == "." ]] ; then
 
     #split handle string at delimiting dot
     NAME=${HANDLE%.*}
     EXT=${HANDLE#*.}

     # if backup directory previous isn't present in site root, make it
        if [[ ! -d "$PREV" ]]; then
             mkdir $PREV 2>&1 > /dev/null
        fi

   # make a backup of the current production file
      find $PROD -type f -name $HANDLE -print0 | xargs -0 -I '{}' cp '{}' ${SRC}previous/$NAME"_bk."$EXT
 
      #now copy new file from dev to prod
      cp $isFile $PROD
      echo $DATE-"The file "$HANDLE" was copied to root at "$PROD 1>>$LOGFILE

  # else the dest argument is a sub folder of root, line 52
  else
     #split the string string at delimiting dot
     NAME=${HANDLE%.*}
     EXT=${HANDLE#*.}

       # look in dest, if a backup directory isn't present make one
       cd $DEST
           if [[ ! -d "$PREV" ]]; then
                 mkdir $PREV 2>&1 > /dev/null
          fi
       # back to development root directory
       cd -
 
      #### make three copies ####
      # backup the current production file
        find $PROD$SEARCH -type f -name $HANDLE -print0 | xargs -0 -I '{}' cp '{}' ${DEST}previous/$NAME"_bk."$EXT

      # now copy the new file to the dev target mirror dir
        cp $isFile $DEST

      # finally, copy the new file to the production target directory
        cp $isFile $PROD$DEST

      # echo feedback to the log file
        echo $DATE-"The file "$HANDLE" copied to the sub folder: "$DEST 1>>$SRC$LOGFILE

  #close if statement from line 55 and exit script
  fi
  exit

# else if isFile is empty,
elif [[ -z $isFile ]]; then

        # assign path info to isDir
        isDir=$(find $SRC$SEARCH -type d -name $HANDLE)

        # backup the current production folder
        # remember we are working from SRC root, all path info relative to SRC
        find $PROD$SEARCH -type d -name $HANDLE | xargs -I '{}' cp -R '{}' ${DEST}previous/

        # copy new directory to dev target mirror dir
        cp -R $isDir $DEST

        # now make a new or overwrite an old folder at the production site
        cp -R $isDir $PROD$DEST

        # echo feedback to the log file
        echo $DATE-"The folder "$HANDLE" copied to: "$DEST 1>>$SRC$LOGFILE
else
        # if everything else fails, echo feedback to the log file
        echo $DATE-"Error-Nothing copied or created for "$HANDLE 1>>$SRC$LOGFILE
fi

If you are working from a local server and need to copy files to a remote server, you could switch out the 'cp' statements with 'scp' statements.

Okay, any thoughts and/or improvements are welcomed.
rwhite35
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Review the Shell Script

Hi, I want to copy all the log file except current date log from one server to another server. Log File will be like this LOGNIG_08_11_2008*.log For this cd /test/log date -d '1 day ago' "+%d_%m_%Y" -->This command gives previous day scp LOGSNIG_date -d '1 day ago' "+%d_%m_%Y"... (2 Replies)
Discussion started by: srinivasvandana
2 Replies

2. Shell Programming and Scripting

Please, review script.

Hi guys, I 've been brewing this shellscript, but I can't test it until next tuesday. In the meantime I am too curious wether it will work or not, so I'd like to hear your comments. Background: I want to watch the user quota for mailboxes in various email-domains on a IMAP-server. I have... (1 Reply)
Discussion started by: algernonz
1 Replies

3. Solaris

peer-to-peer connection on solaris and windows

hi everyone. As a solaris/unix beginner, I am trying a peer-to-peer connection with my Sun and Windows. Here is my setup: -Windows XP -Sun System (Netra T1) which is connected to the console of the WinXP machine. -Windows XP has wireless connection which enables me to access internet ... (3 Replies)
Discussion started by: graboid888
3 Replies

4. Shell Programming and Scripting

a shell script for review.

I have written a bit of shell that lets our company check all our SSL certs. the aim is to have a list of servers and run this check from cron once a week. Our managers have decided that we will not run BASH, so it has been written in /bin/sh and only needs openssl, no perl, no bash, no extra... (8 Replies)
Discussion started by: robsonde
8 Replies

5. Shell Programming and Scripting

Please review script for search in all files

I have written a little script to scan users home directories for certain commands located inside a file. The script is setup to include a small help section and allows for passing a username argument to override scanning of all users home directories. A lot of searching and trial and error has... (7 Replies)
Discussion started by: bkeep
7 Replies

6. IP Networking

Firewall stopping Peer to Peer File sharing

I am looking for advice on a router. I am new to Linux and am trying to use Limewire and Ktorent and can make no connection. Limewire indicates I have a firewall. I have a Linksys router WRK54G and my guess is that is the problem. I have spent hours upon hours trying to get it to work using info... (0 Replies)
Discussion started by: Paul K
0 Replies

7. IP Networking

Problem with Static route through peer to peer connection

Hi, I am trying to add a static route in one of 3 server (S3) so that I can access the main application server (S1). But problem is, the server (S3) where I am trying to add static route is connected with another server (S2) which is in same private network of application server (S1). I have... (9 Replies)
Discussion started by: ImranBD
9 Replies

8. Shell Programming and Scripting

File to Folder script

I am trying to write a linux shell script that will take every file in a folder /home/user/desktop/fileme and place it in a folder with the same name as the folder before /home/user/desktop/fileme/stuff.txt /home/user/desktop/fileme/other.avi /home/user/desktop/fileme/last.jpg after... (2 Replies)
Discussion started by: FustFust
2 Replies

9. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies
All times are GMT -4. The time now is 06:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy