Skip to content

Instantly share code, notes, and snippets.

@fanannan
Last active August 29, 2015 14:18
Show Gist options
  • Save fanannan/b659b95bdf96a4c1070e to your computer and use it in GitHub Desktop.
Save fanannan/b659b95bdf96a4c1070e to your computer and use it in GitHub Desktop.
Python script to synchronize two http servers
#!/usr/bin/python
# this script have two functions:
#
# A: Gatherer
# 1) gets all updated files, excluding the files under symlinks
# 2) makes an archive file with a password, containing all updated files
# 3) places it as a downloadable file on the docroot
#
# B: Updater
# 1) download an archive file with a password, containing all updated file, from another server
# 2) unfreeze the archive
# 3) update all the files at the correspoding locations or create new files there
import sys
import shutil
import commands
path = "/var/www/html/"
password = "something"
archivefile = "my_last_updated_files.zip"
archivepath = path+archivefile
counterarchivefile = "downloaded_last_updated_files.zip"
counterarchivepath = path+counterarchivefile
interval = 10
pasttime = interval +5
def freeze():
global path, pasttime, password, archivepath
find = "/usr/bin/find "+path+' -xdev ! -path "*/tmp*" ! -path "*/cache*" ! -path "*/log*" ! -name "*_last_updated_files.zip" -mmin -'+str(pasttime)
zip = "/usr/bin/zip -P "+password
cmd1 = "rm "+archivepath
print cmd1
commands.getoutput(cmd1)
cmd2 = find+" -exec "+zip+" "+archivepath+" {} +"
print cmd2
result = commands.getoutput(cmd2)
def whoami():
cmd = "/bin/hostname"
result = commands.getoutput(cmd)
return result
def counterpart():
me = whoami()
if me == "xxx":
return "192.168.z.x"
else:
return "192.168.z.y"
def download():
global archivefile, counterarchivepath
url = "https://"+counterpart()+"/"+archivefile
cmd = "/usr/bin/wget --no-check-certificate --tries=2 --timeout=10 "+url+" -O "+counterarchivepath
print cmd
result = commands.getoutput(cmd)
return result
def melt():
global password, counterarchivepath
unzip = "/usr/bin/unzip -u -B -P "+password+" -d / "
#unzip = "/usr/bin/unzip -u -o -B -P "+password+" -d "+path
cmd = unzip+" "+counterarchivepath
print cmd
result = commands.getoutput(cmd)
if sys.argv[1] == "UP":
freeze()
elif sys.argv[1] == "DOWN":
download()
melt()
else:
print "argment should be either UP or DOWN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment