3
\$\begingroup\$

I've recently set up a continuous integration pipeline to deploy a .NET Core application to my Linux machine living on the cloud.

My CI pipeline publishes my application, (specifically a .NET web application), zips it and then uploads by ftp.

Now, I've developed the following shell script which I will run as a service.

  • This script is able to detect as soon as a file has been uploaded to my FTP folder (/home/MyName/ftp/files) by tailing the vbftp log.

  • The script then stops the service running the actual web application, taking the site temporarily offline.

  • The existing site is then backed up.

  • We then unzip the newly uploaded file to the location where the web app lives.(/var/www/ MyProject).

  • The newly uploaded file is deleted and the service restarted.

Below are the entire contents of the shell script

#!/bin/sh

tail -F /var/log/vsftpd.log | while read line; do
  if echo "$line" | grep -q 'OK UPLOAD:'; then
    filename=$(echo "$line" | cut -d, -f2)
    if [ "$ filename" == "MyProject" ]; then
     # Stop site service 
     service MyProject stop 
     # Make backup of existing files 
     sudo zip -r /var/backups/site/$(date +%F)_MyProject /var/www/MyProject 
     # unzip the newly received files 
     # (-o to overwrite only the files which have changed) 
     sudo unzip -o /home/MyName/ftp/files/MyProject.zip -d /var/www/MyProject
     # Remove uploaded zip  
     sudo rm /home/MyName/ftp/files/MyProject.zip 
     #Restart service 
     service MyProject start 
  fi
done

I'm somewhat new to Linux and shell and would love some feedback on the script I've thus far produced above.

\$\endgroup\$
2
\$\begingroup\$

[ x == y ] is not strictly-conforming POSIX sh. Use [ x = y ] instead.

Instead of using /home/MyName, you may consider using the environment variable ${HOME}.

I think you made a typo in "$ filename". Use "$filename" instead.

Good job on the commenting and indentation.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.