12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
-
- # Regular expressions matching files and folders to exclude from the backup
- EXCLUDES = [
- "/Cache",
- "/OfflineCache/",
- "\.mfasl$",
- "/urlclassifier",
- "/lock$",
- "/parent.lock$",
- "/.parentlock$",
- ]
-
- import os, os.path, re, sys, tarfile, time
- from optparse import OptionParser
-
- # Parse command line
- parser = OptionParser()
- parser.add_option("-f", "--folder", dest="profile_path", help="profile folder to be saved")
- parser.add_option("-u", "--user", dest="fd_user", help="username for connection to FileDropper (optional)")
- parser.add_option("-p", "--password", dest="fd_pass", help="password for connection to FileDropper (optional)")
- (options, args) = parser.parse_args()
-
- # Check if constraints are respected
- if options.profile_path is None:
- print "No profile specified"
- sys.exit(1)
- 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):
- print "Username AND password needed"
- sys.exit(1)
-
- profile_name = os.path.basename(options.profile_path)
- date = time.strftime("%Y%m%d_%H%M%S")
- archive_name = "%s-%s.tar.bz2" % (profile_name, date)
-
- # Compile exclusion regexps
- EXCLUDES_RE = []
- for regexp in EXCLUDES:
- r = re.compile(regexp)
- EXCLUDES_RE.append(r)
-
- # Uncompressed size
- total_size = 0
-
- # Exclusion decision function
- def is_excluded(filename):
- global EXCLUDES_RE, total_size
- for regexp in EXCLUDES_RE:
- if regexp.search(filename):
- return True
- total_size += os.path.getsize(filename)
- return False
-
- # Open the destination tar archive
- tar = tarfile.open(archive_name, 'w:bz2')
-
- # Add the data to the archive
- tar.add(options.profile_path, profile_name, exclude=is_excluded)
-
- tar.close()
-
- # Display archive name and sizes
- print "Profile saved to %s" % archive_name
- print "%.1f MB --> %.1f MB" % (total_size/(1024**2), os.path.getsize(archive_name)/(1024**2))
-
- # Upload to FileDropper if needed
- if options.fd_user is not None and options.fd_pass is not None:
- import FileDropper
-
- fd = FileDropper.FileDropper()
- fd.login(options.fd_user, options.fd_pass)
-
- # Upload the file
- url = fd.upload(archive_name)
-
- # Get the file ID
- lst = fd.list()
- file_id = -1
- for line in lst:
- if line[6] == url:
- file_id = line[1]
- break
- if file_id == -1:
- # File not found in list!
- raise Exception("Error in upload, could not get file ID, permissions not set")
-
- # Set the permissions
- fd.set_perm(file_id, FileDropper.FD_PERM_PRIVATE)
-
- # Logout
- fd.logout()
-
- print "File uploaded to FileDropper: %s" % url
|