daily pastebin goal
86%
SHARE
TWEET

rsync

a guest May 23rd, 2016 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. # DETAILS:
  4. # This script is designed to push newly downloaded files from a seedbox (offshore, local on your network, or even local on the same machine) to a target machine or directory.
  5. # It does this by polling the directory (locally) every 10 seconds for new downloads and once found will establish a rsync connection to push them to the target.
  6. # Pushed files are logged in a SQLite database so they are not pushed again the next time the source directory is checked.
  7. # The main advantage verses a plain old rsync script is the target directory can have files deleted from it while the source can continue seeding the files that were deleted on the target.
  8. #
  9. # INSTRUCTIONS:
  10. # Run this script on your seedbox in the background using:
  11. # ./seedbox-sync.py --verbose --progress --times /tmp/a/ /tmp/b/ &
  12. # All parameters except the last two are optional and the trailing "&" will make bash run it in the background.
  13. # The above example will sync files and directories from "/tmp/a/" to "/tmp/b/". If a file is later deleted from "/tmp/b/" it will never be resynced.
  14. # The list of known files is kept in the seedbox-sync.sqlite file which is created at first run.
  15. # IMPORTANT: Make sure the source directory is your torrent client's "completed" directory and not "incomplete" or regular download directory (see "moving files to another directory once complete") otherwise the s$
  16. #
  17. # EXAMPLE 1:
  18. # ./seedbox-sync.py --progress /opt/deluge-complete/ john@home.dyndns.com:~/downloads/
  19. # Running on a co-located seedbox, the Deluge client is set to copy completed files to /opt/deluge-complete/.
  20. # After a download is complete the new file or directory will be copied to your home machine using SSH.
  21. # All that is required here is on your home machine adding a user john, then creating a downloads directory for him in his home directory.
  22. #
  23. # EXAMPLE 2:
  24. # ./seedbox-sync.py --verbose --progress --times /tmp/a/ /tmp/b/ &
  25. # Sync all files from /tmp/a/ to /tmp/b/ where /tmp/a/ is the source where your torrent client saves completed files to and /tmp/b/ is the target where the downloads will be mirrored to.
  26. # The above example is using this script as a local sync on the same machine which is still useful because you then can delete downloads from your mirrored target directory and still remain seeding on your torrent$
  27.  
  28. import sqlite3
  29. import os
  30. import sys
  31. import subprocess
  32. from time import sleep
  33.  
  34. if len(sys.argv)<3:
  35.  print("Please run with atleast two parameters.")
  36.  exit(1)
  37.  
  38. sourceDir=sys.argv[-2].rstrip("/")
  39. destDir=sys.argv[-1].rstrip("/")
  40.  
  41. # Create the database if it doesn't exist.
  42. if not os.path.exists("seedbox-sync.sqlite"):
  43.  print("No database found, creating new one...")
  44.  con=sqlite3.connect("seedbox-sync.sqlite")
  45.  con.execute("create table rememberedFiles(filename varchar);")
  46. else:
  47.  con=sqlite3.connect("seedbox-sync.sqlite")
  48.  
  49. def isFileRemembered(filename):
  50.  cur=con.cursor()
  51.  cur.execute("select count(*) from rememberedFiles where filename=?",(filename,))
  52.  r=[row[0] for row in cur.fetchall()][0]
  53.  cur.close()
  54.  return r>0
  55.  
  56. def rememberFile(filename):
  57.  con.execute("insert into rememberedFiles values(?)",(filename,))
  58.  con.commit()
  59.  
  60. # The main loop.
  61. while True:
  62.  files=os.listdir(u""+sourceDir) # If you call os.listdir() with a UTF-8 string the result will be an array of UTF-8 strings instead of ASCII. Needed for passing UTF-8 into sqlite3 for filenames with special chara$
  63.  
  64.  # print("Sleeping for 30 Seconds...")
  65.  sleep(30)
  66.  
  67.  for file in files:
  68.   if(isFileRemembered(file)):
  69.    # This file has been copied already.
  70.    print("Skipping file: "+file)
  71.    continue
  72.  
  73.   # Sync the file.
  74.   print("Syncing new file: "+file)
  75.   cmd=["rsync"]
  76.   #cmd.append("--rsh=ssh -p22222") # Uncomment this line if your target directory or machine is listening on a port other than 22.
  77.   if "--verbose" in sys.argv:
  78.    cmd.append("--verbose")
  79.   if "--progress" in sys.argv:
  80.    cmd.append("--progress")
  81.  
  82.   if "--remove-source-files" in sys.argv: #enables removing files on seedbox
  83.    cmd.append("--remove-source-files")
  84.   cmd.append("--temp-dir=/"PUT YOUR TEMP DIR HERE") #saves files into temp folder first
  85.  # Give files in destination 0777 permissions for some NAS setups, but optional.
  86.  cmd.append("--chmod=ugo+rwx")
  87.  cmd.append("--perms")
  88. #  cmd.append("--compress")
  89.  cmd.append("--delay-updates")
  90.  cmd.append("--recursive")
  91.  cmd.append(sourceDir+"/"+file)
  92.  cmd.append(destDir+"/")
  93.  p=subprocess.Popen(cmd,shell=False)
  94.  if p.wait()==0:
  95.   rememberFile(file)
  96.   print("Synced & remembered: "+file)
  97.  else:
  98.   print("Failed to sync: "+file)
  99.  
  100. con.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Pastebin PRO 'AUTUMN SPECIAL'!
Get 60% OFF Pastebin PRO accounts!
 
Top