Curl Shell Script To Purge Cache From Bitgravity Content Delivery Network

by on May 22, 2012 · 0 comments

A content delivery network or content distribution network (CDN) is a system of computers containing copies of data, placed at various points in a network so as to maximize bandwidth for access to the data from clients throughout the network. Please note that the following script will only work with bitgravity CDN. You need to get API access keys using bitgravity portal.

  1. #!/bin/bash
  2. # A simple shell script to purge Bitgravity cdn cache using API and curl
  3. # command line. Tested on RHEL 6.x, Debian 6.x, and Ubuntu Linux
  4. # ----------------------------------------------------------------------------
  5. # Written by Vivek Gite <http://www.cyberciti.biz/>
  6. # (c) 2012 nixCraft under GNU GPL v2.0+
  7. # ----------------------------------------------------------------------------
  8. # Last updated: 22/May/2012
  9. # ----------------------------------------------------------------------------
  10.  
  11. ######*** SET ME FIRST ***#####
  12. # api keys #
  13. akid="you@example.com_or_YOUR-API-USERNAME"
  14. akpass="YOUR-API-PASSWORD"
  15.  
  16.  
  17. # 1 disable it, 0 enable it - show debug info?
  18. _debug=1
  19.  
  20. # 1 disable it, 0 enable it - only show success / fail message?
  21. _quite=0
  22.  
  23. # either curl or wget
  24. _purge="/usr/bin/curl -I"
  25.  
  26.  
  27. ## CDN domain ##
  28. origin="$1"
  29.  
  30. ## file ##
  31. file="$2"
  32. myurl="${origin}${file}"
  33.  
  34.  
  35. [ $# -eq 0 ] && { echo "Usage $0: cdn.example.net /path/to/file.js"; exit 1 ; }
  36.  
  37. ## Calculate md5 ##
  38. md5="${akpass}/service/?api=proxycache&method=purge&object=${myurl}&accessKeyId=${akid}"
  39. hash="$(echo -n "$md5" | md5sum )"
  40.  
  41. ## Build final url for $_curl ##
  42. url="http://wsa-api.bitgravity.com/service/?api=proxycache&method=purge&object=${myurl}&accessKeyId=${akid}&h=${hash}"
  43.  
  44.  
  45. ## Show debug info ? ##
  46. [ $_debug -eq 0 ] && {
  47. ### debug ###
  48. echo
  49. echo "************** [ DEBUG ] ***********************"
  50. echo "MD5 url: $md5"
  51. echo "MD5 hash: $hash"
  52. echo "$url"
  53. echo "**************************************************"
  54. echo
  55. }
  56.  
  57. ## Purge it ##
  58. [ $_quite -eq 0 ] && $_purge -I "$url" || { $_purge -I "$url" &>/dev/null && echo "Sucess with HTTP/200" || echo "Failed."; }

How do I access this script?

Simply run it as follows to purge a cdn url called http://files.cyberciti.biz/static/css/layout.css:
$ ./script files.cyberciti.biz /static/css/layout.css
Sample outputs:

HTTP/1.1 200 OK
Date: Tue, 22 May 2012 10:07:25 GMT
Server: Apache
Set-Cookie: PHPSESSID=xxxxxxxxxxxxxxxxx; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: user_id=you%40example.com; path=/; domain=.wsa-api.bitgravity.com
Set-Cookie: ahash=yyyyyyyyyyyyyyyyyyyyy; path=/; domain=.wsa-api.bitgravity.com
X-Served-By: util10.sjc1.bitgravity.com
Connection: close
Content-Type: text/plain

How do I purge lage number of urls?

Create a text file called urls.txt with urls as follows one per line:

files.cyberciti.biz /path/to/js/1.js
files.cyberciti.biz /path/to/js/2.js
files.cyberciti.biz /path/to/js/3.js
files.cyberciti.biz /static/css/foo.jpg
cdn.cyberciti.biz /dyn/bar.html
video.cyberciti.biz /path/to/video/1
video.cyberciti.biz /path/to/video/1.flv

Run the script as follows:

  1. #!/bin/bash
  2. # bulk_purge.sh
  3. # Written by Vivek Gite <http://www.cyberciti.biz/>
  4. # (c) 2012 nixCraft under GNU GPL v2.0+
  5. # ----------------------------------------------------
  6. _purge="/path/to/purge_script.sh"
  7. _urls="/path/to/urls.txt"
  8. while IFS= read -r domain file
  9. do
  10. $_purge "$domain" "$file"
  11. done < "${_urls}"
  12.  

Run it as follows:
$ ./bulk_purge.sh



4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

Previous Script:

Next Script: