Python module providing an easy access to FileDropper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ffx-backup.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Regular expressions matching files and folders to exclude from the backup
  4. EXCLUDES = [
  5. "/Cache",
  6. "/OfflineCache/",
  7. "\.mfasl$",
  8. "/urlclassifier",
  9. "/lock$",
  10. "/parent.lock$",
  11. "/.parentlock$",
  12. ]
  13. import os, os.path, re, sys, tarfile, time
  14. from optparse import OptionParser
  15. # Parse command line
  16. parser = OptionParser()
  17. parser.add_option("-f", "--folder", dest="profile_path", help="profile folder to be saved")
  18. parser.add_option("-u", "--user", dest="fd_user", help="username for connection to FileDropper (optional)")
  19. parser.add_option("-p", "--password", dest="fd_pass", help="password for connection to FileDropper (optional)")
  20. (options, args) = parser.parse_args()
  21. # Check if constraints are respected
  22. if options.profile_path is None:
  23. print "No profile specified"
  24. sys.exit(1)
  25. if (options.fd_user is not None and options.fd_pass is None) or (options.fd_user is None and options.fd_pass is not None):
  26. print "Username AND password needed"
  27. sys.exit(1)
  28. profile_name = os.path.basename(options.profile_path)
  29. date = time.strftime("%Y%m%d_%H%M%S")
  30. archive_name = "%s-%s.tar.bz2" % (profile_name, date)
  31. # Compile exclusion regexps
  32. EXCLUDES_RE = []
  33. for regexp in EXCLUDES:
  34. r = re.compile(regexp)
  35. EXCLUDES_RE.append(r)
  36. # Uncompressed size
  37. total_size = 0
  38. # Exclusion decision function
  39. def is_excluded(filename):
  40. global EXCLUDES_RE, total_size
  41. for regexp in EXCLUDES_RE:
  42. if regexp.search(filename):
  43. return True
  44. total_size += os.path.getsize(filename)
  45. return False
  46. # Open the destination tar archive
  47. tar = tarfile.open(archive_name, 'w:bz2')
  48. # Add the data to the archive
  49. tar.add(options.profile_path, profile_name, exclude=is_excluded)
  50. tar.close()
  51. # Display archive name and sizes
  52. print "Profile saved to %s" % archive_name
  53. print "%.1f MB --> %.1f MB" % (total_size/(1024**2), os.path.getsize(archive_name)/(1024**2))
  54. # Upload to FileDropper if needed
  55. if options.fd_user is not None and options.fd_pass is not None:
  56. import FileDropper
  57. fd = FileDropper.FileDropper()
  58. fd.login(options.fd_user, options.fd_pass)
  59. # Upload the file
  60. url = fd.upload(archive_name)
  61. # Get the file ID
  62. lst = fd.list()
  63. file_id = -1
  64. for line in lst:
  65. if line[6] == url:
  66. file_id = line[1]
  67. break
  68. if file_id == -1:
  69. # File not found in list!
  70. raise Exception("Error in upload, could not get file ID, permissions not set")
  71. # Set the permissions
  72. fd.set_perm(file_id, FileDropper.FD_PERM_PRIVATE)
  73. # Logout
  74. fd.logout()
  75. print "File uploaded to FileDropper: %s" % url