Bash Script: Remrun


Origin:

Remrun (remote run) is a bash script which converts local Markdown files to HTML and then uploads them to a remote website.

I wrote this script when I was experimenting with Markdown syntax and the feasibility of quick deployment of information using the least amount of power with the highest accessibility. The resulting HTML files are easily accessible from a traditional or terminal-based web browser.

Purpose:

This script automates the process of:

  1. Converting Markdown (.md) files to HTML.
  2. Uploading the generated HTML files to a remote web server.
  3. Transferring additional resources (e.g., directories like files and scripts) to the same server.
  4. Cleaning up temporary HTML files locally.

Script Breakdown:

1. Shebang and Initial Comment

#!/bin/bash
# This script converts markdown files to html and then uploads them to a website.
  • #!/bin/bash: Specifies that the script should be run using the Bash shell.
  • The comment describes the overall functionality of the script.

2. Navigate to Local Markdown Directory

echo "Rendering Site"
cd /local/path/to/markdown/site
  • Prints a message indicating the start of the site rendering process.
  • Changes the current working directory to where the Markdown files are stored.

3. Convert Markdown Files to HTML

for f in *.md; 
do 
    pandoc -o "${f%.md}".html "$f";
  • Iterates over all .md files in the directory.
  • Uses pandoc to convert each Markdown file (f) to an HTML file:
    • ${f%.md} removes the .md extension from the filename.
    • Appends .html to the resulting filename.

4. Move HTML Files (Commented Out)

# mv *.html ~/Webserver/sgweb/
  • Moves the generated HTML files to a local web server directory (currently commented out).

5. Finalize Rendering

echo "Site Rendered"
done;
  • Ends the loop after processing all Markdown files.
  • Prints a message indicating the completion of site rendering.

6. Sync HTML Files to Remote Server

sudo rsync -rtvzP --exclude template* /local/path/to/markdown/site*.html user@remote.server:/var/www/html;
  • Uses rsync to securely and efficiently sync the HTML files to the remote server:
    • -r: Recursive (includes subdirectories if any).
    • -t: Preserves modification times.
    • -v: Verbose output.
    • -z: Compresses data during transfer.
    • -P: Shows progress and allows resumption of interrupted transfers.
    • --exclude template*: Skips files matching template*.

7. Cleanup Temporary HTML Files Locally

rm /local/path/to/markdown/site/*.html &&
echo "HTML files have been removed."
  • Deletes the locally generated HTML files after successful upload.
  • Confirms the deletion with a printed message.

8. Transfer Additional Directories to Remote Server

echo "Transferring other directories now." &&
scp -r /local/path/to/markdown/site/files/ user@remote.server:/var/www/html &&
scp -r /local/path/to/markdown/site/scripts/ user@remote.server:/var/www/html &&
  • Uses scp (secure copy) to recursively upload the files and scripts directories to the remote server.

9. Completion Message

echo "File transfer completed." &&
cd -
  • Prints a final message indicating successful file transfers.
  • Returns to the previous directory using cd -.

Notes:

  • Ensure pandoc, rsync, and scp are installed on the system.
  • Update paths and server details (/local/path/to/markdown/site, user@remote.server, etc.) to match your environment.
  • Run this script with appropriate permissions (sudo if necessary).

Enhancements:

  1. Error Handling: Add checks to handle cases like missing pandoc or failed file transfers.
  2. Logging: Direct output to a log file for auditing and debugging purposes.
  3. Configuration: Externalize paths and server details into a configuration file for better maintainability.

Download remrun.zip

Scroll to Top