Skip to content

Instantly share code, notes, and snippets.

@FrancisVarga
Created November 26, 2012 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancisVarga/4150608 to your computer and use it in GitHub Desktop.
Save FrancisVarga/4150608 to your computer and use it in GitHub Desktop.
Python script dump http request, example with couchbase.
#!/usr/bin/env python
__author__ = 'Francis Varga'
import os
import urlopen
import argparse
import time
import sys
import tarfile
#define tmp folder for storing json file
couchbaseTmpDumpFile = "/tmp/couchbase__dump__json__"
errorLogFile = "/tmp/couchbase__dump__errror"
parser = argparse.ArgumentParser("curl {{couchbase_url}} >> {{json_file}} | tar -zcvf {{output_file.tar.gz}}")
parser.add_argument("-f", help="tar.gz file output", dest="fileOutput")
parser.add_argument("-u", help="Couchbase View / URL", dest="couchbaseUrl")
parser.add_argument("-t", help="Should tar?", dest="tarGz")
args = parser.parse_args()
sys.stderr = open(errorLogFile, "w")
httpReq = urlopen(args.couchbaseUrl)
timestamp = str(int(round(time.time() * 1000)))
tmpFile = couchbaseTmpDumpFile + timestamp
local_file = open(tmpFile, "w+")
local_file.write(httpReq.read())
local_file.close()
if tarGz:
tarDumpFile = tarfile.open(mode="w:bz2", name=args.fileOutput + "_" + timestamp + ".tar.gz")
tarDumpFile.add(tmpFile)
tarDumpFile.close()
os.remove(tmpFile)
else:
print "You find your file: " + tmpFile
os.remove(errorLogFile)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment