def send_email(self, result, head): ''' Send a message to the appropriate users. If result is False (failure) then error message will contain the reason. @param result: @param error_message: ''' log.debug("send_email: ", result, head) if result: message_text = head + \ _("\n\nStatistics:\n {files} files backed up.\n " "{folders} folders backed up.\n {size} copied.\n" ).format(files=self.nfiles, folders=self.nfolders, size=utils.readable_form(self.bytes)) subject = _("Backup {server}/{backup}/{type} completed").format(server=utils.get_hostname(), backup=self.backup.name, type=self.type) else: message_text = head subject = _("Backup {server}/{backup}/{type} failed").format(server=utils.get_hostname(), backup=self.backup.name, type=self.type) if not self.options.dry_run: messages = " " + "\n ".join([message.time + " " + message.message for message in self.db.run_messages(self.run_id)]) message_text += _("\nBackup messages:\n") + messages else: message_text = "\n" log.debug("Starting mail send") try: sendemail.sendemail(subject, message_text) except Exception as e: msg = _("Unable to email results. {error}").format(error=str(e)) if not self.dry_run: self.db.save_message(msg) else: print(msg) log.trace("send_email completed")
def start(): """ Starts the main program. """ hostname = get_hostname() locs = config.HOSTS[hostname] for l in locs: hr_text(l) path = locs[l] print_blue("# PATH: " + path) filename_db = config.PATH_DB + l + '.db' print "# creating DB ... ", filename_db dbf = Database(path, filename_db) if dbf.scan(): dbf.print_statistics() # simply logging f = open(config.PATH_LOGFILE, 'aw') f.write(get_datetime() + '\n') f.close()
def search(self, arguments, opt): """ This searches :-) This function is way too big and should be split up (it has 267 lines and up to 45 local variables!) * input: - arguments & opt based on cmd input by user """ list_with_action = True # if x 2 related to firefox's bookmarks if opt.bookmarks: # sqlite3 is required! import sqlite3 conn = sqlite3.connect(FF_SQLITE_DATABASE) c = conn.cursor() c.execute("select title, url from moz_places;") conn.commit() results = c.fetchall() if 1: for r in results: title = r[0] if title: pass else: title = '' line = title + r[1] # a stupid way word = arguments[0] try: word2 = arguments[1] except IndexError: word2 = '' if compile(word, I).search(line) \ and compile(word2, I).search(line): print_red_and_blue(title, ' ' + r[1]) exit(1) if opt.bookmarks_folder: # dirty but works # django required! try: from django.conf import settings except ImportError: print 'ImportError: Install python-django to run this feature: sudo apt-get install python-django or sudo pip install django' exit(1) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': FF_SQLITE_DATABASE, 'HOST': '', 'PORT': '', } } settings.configure(DATABASES=DATABASES) from orm.models import MozBookmarks show_path = False phrase = argv[2].strip() print 'phrase:', phrase, '\n' bookmarks = MozBookmarks.objects.all() c = 0 for b in bookmarks: if b.fk is None and b.title != '': # b.fk = has bookmarks try: path = b.title.strip() except AttributeError: print '-- b.title:', b.title while b.parent != 1: parent = MozBookmarks.objects.get(id=b.parent) path = parent.title.strip() + '/' + path b = parent if show_path: print path if path.lower().find(phrase.lower()) > -1: print path c += 1 exit(1) if opt.invert: arguments.reverse() # main search body words = '*' + '*'.join(arguments) + '*' # '*a*b*c*' words_rex = '.*' + '.*'.join(arguments) + '.*' # '.*a.*b*.c*' if opt.global_search: places = PLACES_GLOBAL else: places = PLACES_LOCAL places.append(get_hostname()) if opt.grep_here: places = ['current dir'] status = 'grepping the current directory' cmd = GREP_CMD + " '" + words.replace('*', '') + "' *" if opt.verbose: print 'cmd: ', cmd out = getoutput(cmd).strip() for l in out.split('\n'): if l.find(':') > -1: try: filename, text = l.split(':') filename += ':' except: items = l.split(':') filename = items[0] text = ':'.join(items[1:]) print_red_and_blue(filename, text) elif l.find('-') > -1: try: filename, text = l.split('-') filename += '-' except: items = l.split('-') filename = items[0] text = '-'.join(items[1:]) print_red_and_blue(filename, text) else: print l exit(1) if opt.wholename: # or basename wholename_or_basename = ' -w ' else: wholename_or_basename = ' -b ' if opt.find_dir: places = ['find directories@' + get_hostname()] if opt.find_find: places = ['find@' + get_hostname()] if opt.find_tu: places = ['find here -t tu@' + get_hostname()] # reset file paths_text_file = open('/home/' + getuser() + '/.mmfinder-paths', 'w') paths_text_file_to_open = open( '/home/' + getuser() + '/.mmfinder-paths-to-open', 'w') mmterminalpathtext = '' mmterminalpathtext_to_open = '' html_hits = '' c = 1 for p in places: hr_text(p + '...') html_hits += '#' + p + '\n' status = '' if opt.pdf_find: status = 'pdf searching...' cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i -r '" + words_rex + "pdf" elif opt.document_find: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_DOCUMENTS) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.find_media: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_MEDIA) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.rex: status = 'rex searching...' word = arguments[0] # <--- !!! cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + word + "'" elif opt.find_tu: status = 'finding here (tutaj)...' cmd = "find '" + getcwd() + "' -iname '" + words + "'" elif opt.find_find: status = 'finding /home/...' cmd = "find ~ -iname '" + words + "'" elif opt.find_dir: status = 'finding a dir /...' cmd = "find ~ -iname '" + words + "' -type d" # very slow :-( if False: if word.startswith('^'): word_without = word.replace('^', '') cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/" + word_without + \ "*' | xarguments file" #locate -r '/*python2.7*/' | less else: cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/*" + word + "*/'" \ #locate -r '/*python2.7*/' | less else: status = 'basic search...' cmd = "locate -d " + PATH_DB + p + '.db' + \ " " + wholename_or_basename + " -i '" + words + "'" if opt.un_grep: cmd = cmd + " | grep -v '" + opt.un_grep + "'" if opt.verbose: print '# status:', status print '# cmd', cmd # execute! out = getoutput(cmd).strip() if opt.dev: hr_text('dev::out') print out hr() if out and list_with_action: for item in out.strip().split('\n'): # @@ BUG @@ id = IDS[c] id = 'a' # TO FIX h = Hit(id, item) self.items.append(h) h.check_filetype() #print i.is_dir #if i.is_dir and find_dir: # i.show() #print 'x' #if not find_dir: hit = h.show(opt.not_less, opt.noncolor, c) if opt.www_browser: html_hits += hit + '\n' else: mmterminalpathtext += hit.split('\n')[0].strip() \ + ' #' + str(c) + '\n' mmterminalpathtext_to_open += hit.split('\n')[1].strip() \ + ' #' + str(c) + '\n' c += 1 print # and out becuase don't 'press key' for empty outputs if opt.key and out: raw_input('[press key]') if opt.www_browser: html_text = '<pre>' html_text += html_hits.replace('\n', '</br>') html_text += '<pre>' fn = HTML_FN f = open(fn, 'w') f.write(html_text) f.close() cmd = HTML_CMD + ' ' + fn print cmd system(cmd) paths_text_file.write(mmterminalpathtext) paths_text_file_to_open.write(mmterminalpathtext_to_open) paths_text_file.close()
def do_backup(self) : self.start_time = datetime.now() self.nfiles = 0 self.nfolders = 0 self.bytes = 0 success = False message = "" self.backup_folder = os.path.join(self.backup.name, self.start_time.strftime(const.DateTimeFormat) + " " + self.type) if not self.dry_run: self.run_id = self.db.start_run(self.backup.name, self.backup.store, self.type, self.start_time) msg = _("Backup {server}/{backup}/{type} beginning").format( server=utils.get_hostname(), backup=self.backup.name, type=self.type) if self.dry_run: msg += _(" (Dry Run)") log.info(msg) self.db.save_message(msg) if self.orig_type != self.type: # The backup type was switched self.db.save_message(_("NOTE: Backup type switched to {newtype} from {oldtype}").format( newtype=self.type, oldtype=self.orig_type)) # After here we have a run set up in the database, and can begin logging errors. try: # Check that if ENCRYPTION is enabled, that there is a password defined. if self.backup.encrypt and not self.config.data_passphrase: raise Exception("Backup encryption required, but no passphrase has been configured. Backup cancelled.") self.prepare_store() # Prepare output/destinations/encryption self.prepare_output() try: # Now we actually DO the backup, for each listed folder for folder in self.backup.include_folders: self.recursive_backup_folder(folder) log.debug("Committing saved fs entries...") self.db.fs_saved_commit() log.debug("Closing...") self.close_output(success=True) #raise Exception("Test Exception") except Exception as e: log.warn("Exception during backup:", str(e)) # We are going to fail. But we need to try and close # whatever we can. Closing may fail, but in this case # we ignore that error. try: self.close_output(success=False) except: pass raise e if self.backup.verify and not self.dry_run: log.info("Starting verify phase") msg = _("Backup {server}/{backup}/{type} verification starting").format( server=utils.get_hostname(), backup=self.backup.name, type=self.type) self.db.save_message(msg) v = Verify(self.backup.name, self.start_time) v.run() msg = _("Backup {server}/{backup}/{type} verification succeeded").format( server=utils.get_hostname(), backup=self.backup.name, type=self.type) self.db.save_message(msg) # self.do_verify() # Messaging... # If its a dry run, the command line specifies messaging. # Otherwise both the command line AND backup spec do. if not self.dry_run: self.db.update_run_status(const.StatusSuccess) message = _("Backup {server}/{backup}/{type} completed").format( server=utils.get_hostname(), backup=self.backup.name, type=self.type) if self.dry_run: message += " " + _("(Dry Run)") success = True if not self.dry_run: self.db.save_message(message) except Exception as e: log.error("Exception in backup. Recording. ", e) message = _("Backup {server}/{backup}/{type} failed. {error}").format( server=utils.get_hostname(), backup=self.backup.name, type=self.type, error=str(e)) success = False if not self.dry_run: self.db.update_run_status(const.StatusFailed) # After a failed backup - we must remove the backup data because it # cannot be trusted. run = self.db.run_details(self.run_id) # Delete the remote data log.debug("Attempting to delete remote run data") self.store.delete_run_data(run) # Delete the entries in the database (but not the failed run itself) # This means the messages will persist, so we can see the usage. log.debug("Attempting to delete DB run data") self.db.delete_run_versions(self.run_id) self.db.save_message(message) if self.options.message or (self.backup.notify_msg and not self.dry_run): try: from lib.dlg import Notify Notify(const.AppTitle, message) except Exception as e: # This one is not really an error... there is probably no-one logged in. msg = _("Unable to send notification message (no-one logged in)") if not self.dry_run: self.db.save_message(message) log.info(msg) if self.options.email or (self.backup.notify_email and not self.dry_run): try: self.send_email(success, message) except Exception as e: msg = _("Unable to email notification message: {error}").format( error=str(e)) if not self.dry_run: self.db.save_message(message) log.error(msg) if self.options.shutdown or (self.backup.shutdown_after and not self.dry_run): try: cmd = ["zenity", "--question", "--ok-label", _("Shutdown Now"), "--cancel-label", _("Cancel Shutdown"), "--text", _("Backup {backup} complete. Computer will shut down in 2 minutes").format(backup=self.backup.name), "--timeout", "120"] status = subprocess.call(cmd) log.debug("Shutdown query. status=%d" % status) if status == 0 or status == 5: print("Running shutdown") subprocess.Popen(["shutdown", "-P", "now"]) print("Done running shutdown") except Exception as e: msg = _("Unable to shutdown PC: {error}").format( error=str(e)) if not self.dry_run: self.db.save_message(message) log.error(msg)
def search(self, arguments, opt): """ This searches :-) This function is way too big and should be split up (it has 267 lines and up to 45 local variables!) * input: - arguments & opt based on cmd input by user """ list_with_action = True # if x 2 related to firefox's bookmarks if opt.bookmarks: # sqlite3 is required! import sqlite3 conn = sqlite3.connect(FF_SQLITE_DATABASE) c = conn.cursor() c.execute("select title, url from moz_places;") conn.commit() results = c.fetchall() if 1: for r in results: title = r[0] if title: pass else: title = '' line = title + r[1] # a stupid way word = arguments[0] try: word2 = arguments[1] except IndexError: word2 = '' if compile(word, I).search(line) \ and compile(word2, I).search(line): print_red_and_blue(title, ' ' + r[1]) exit(1) if opt.bookmarks_folder: # dirty but works # django required! try: from django.conf import settings except ImportError: print 'ImportError: Install python-django to run this feature: sudo apt-get install python-django or sudo pip install django' exit(1) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': FF_SQLITE_DATABASE, 'HOST': '', 'PORT': '', } } settings.configure(DATABASES=DATABASES) from orm.models import MozBookmarks show_path = False phrase = argv[2].strip() print 'phrase:', phrase, '\n' bookmarks = MozBookmarks.objects.all() c = 0 for b in bookmarks: if b.fk is None and b.title != '': # b.fk = has bookmarks try: path = b.title.strip() except AttributeError: print '-- b.title:', b.title while b.parent != 1: parent = MozBookmarks.objects.get(id=b.parent) path = parent.title.strip() + '/' + path b = parent if show_path: print path if path.lower().find(phrase.lower()) > -1: print path c += 1 exit(1) if opt.invert: arguments.reverse() # main search body words = '*' + '*'.join(arguments) + '*' # '*a*b*c*' words_rex = '.*' + '.*'.join(arguments) + '.*' # '.*a.*b*.c*' if opt.global_search: places = PLACES_GLOBAL else: places = PLACES_LOCAL places.append(get_hostname()) if opt.grep_here: places = ['current dir'] status = 'grepping the current directory' cmd = GREP_CMD + " '" + words.replace('*', '') + "' *" if opt.verbose: print 'cmd: ', cmd out = getoutput(cmd).strip() for l in out.split('\n'): if l.find(':') > -1: try: filename, text = l.split(':') filename += ':' except: items = l.split(':') filename = items[0] text = ':'.join(items[1:]) print_red_and_blue(filename, text) elif l.find('-') > -1: try: filename, text = l.split('-') filename += '-' except: items = l.split('-') filename = items[0] text = '-'.join(items[1:]) print_red_and_blue(filename, text) else: print l exit(1) if opt.wholename: # or basename wholename_or_basename = ' -w ' else: wholename_or_basename = ' -b ' if opt.find_dir: places = ['find directories@' + get_hostname()] if opt.find_find: places = ['find@' + get_hostname()] if opt.find_tu: places = ['find here -t tu@' + get_hostname()] # reset file paths_text_file = open('/home/' + getuser() + '/.mmfinder-paths', 'w') paths_text_file_to_open = open('/home/' + getuser() + '/.mmfinder-paths-to-open', 'w') mmterminalpathtext = '' mmterminalpathtext_to_open = '' html_hits = '' c = 1 for p in places: hr_text(p + '...') html_hits += '#' + p + '\n' status = '' if opt.pdf_find: status = 'pdf searching...' cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i -r '" + words_rex + "pdf" elif opt.document_find: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_DOCUMENTS) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.find_media: status = 'document searching...' extensions = '$|'.join(EXTENSIONS_OF_MEDIA) cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + \ words_rex + ".*(" + extensions + ")'" elif opt.rex: status = 'rex searching...' word = arguments[0] # <--- !!! cmd = "locate -d " + PATH_DB + p + '.db' + " " + \ wholename_or_basename + " -i --regex '" + word + "'" elif opt.find_tu: status = 'finding here (tutaj)...' cmd = "find '" + getcwd() + "' -iname '" + words + "'" elif opt.find_find: status = 'finding /home/...' cmd = "find ~ -iname '" + words + "'" elif opt.find_dir: status = 'finding a dir /...' cmd = "find ~ -iname '" + words + "' -type d" # very slow :-( if False: if word.startswith('^'): word_without = word.replace('^', '') cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/" + word_without + \ "*' | xarguments file" #locate -r '/*python2.7*/' | less else: cmd = "locate -d " + PATH_DB + p + '.db' + \ " -e -i -r '/*" + word + "*/'" \ #locate -r '/*python2.7*/' | less else: status = 'basic search...' cmd = "locate -d " + PATH_DB + p + '.db' + \ " " + wholename_or_basename + " -i '" + words + "'" if opt.un_grep: cmd = cmd + " | grep -v '" + opt.un_grep + "'" if opt.verbose: print '# status:', status print '# cmd', cmd # execute! out = getoutput(cmd).strip() if opt.dev: hr_text('dev::out') print out hr() if out and list_with_action: for item in out.strip().split('\n'): # @@ BUG @@ id = IDS[c] id = 'a' # TO FIX h = Hit(id, item) self.items.append(h) h.check_filetype() #print i.is_dir #if i.is_dir and find_dir: # i.show() #print 'x' #if not find_dir: hit = h.show(opt.not_less, opt.noncolor, c) if opt.www_browser: html_hits += hit + '\n' else: mmterminalpathtext += hit.split('\n')[0].strip() \ + ' #' + str(c) + '\n' mmterminalpathtext_to_open += hit.split('\n')[1].strip() \ + ' #' + str(c) + '\n' c += 1 print # and out becuase don't 'press key' for empty outputs if opt.key and out: raw_input('[press key]') if opt.www_browser: html_text = '<pre>' html_text += html_hits.replace('\n', '</br>') html_text += '<pre>' fn = HTML_FN f = open(fn, 'w') f.write(html_text) f.close() cmd = HTML_CMD + ' ' + fn print cmd system(cmd) paths_text_file.write(mmterminalpathtext) paths_text_file_to_open.write(mmterminalpathtext_to_open) paths_text_file.close()