def print_directory_queue(self): """ Prints the directories to be backed up, and the directories to be skipped. """ print '\n\nThese directories will be backed up:' for directory_name in self.directory_queue: print_in_color('* %s' % directory_name, 'green') print '\nThese directories will NOT be backed up:' for directory in config.DIRECTORIES: if directory['name'] not in self.directory_queue: print_in_color('* %s' % directory['name'], 'red')
def sync_logs_and_config(self): """ Copies all logs and config files to the device and verifies that the copy was successful. """ source_dir = config.BASE_PATH target_dir = os.path.join(self.path, '.equanimity') print_header('Syncing logs to backup device') if not os.path.exists(target_dir): os.makedirs(target_dir) rsync_cmd = 'rsync -a --delete %s/ %s' % (source_dir, target_dir) subprocess.call(rsync_cmd, shell=True) if checksum_directory(source_dir) == checksum_directory(target_dir): print 'Logs synced successfully.' else: print_in_color('Logs NOT SYNCED.', 'red')
def print_summary(self): """ Prints a table with the age of the copy of each directory on the device. """ # Print the table heading row. print '+{:-<60}+'.format('') print '|', decorated_device_name = '<<< %s >>>' % self.name print_in_color( '{:^58}'.format(decorated_device_name), 'bold', line_feed=False ) print '|' # Print the column headings row. print '+{:-<24}+{:-<35}+'.format('', '') print '|{:^24}|{:^35}|'.format( '~ Directory ~', '~ Approximate backup age ~' ) print '+{:-<24}+{:-<35}+'.format('', '') # Print each directory's row. for directory in config.DIRECTORIES: print '| {:<23}|'.format(directory['name']), elapsed_seconds = self.directory_age(directory) if elapsed_seconds: raw_msg = readable_duration(elapsed_seconds) msg = '{:<33}'.format(raw_msg) if elapsed_seconds > (60 * 60 * 24 * 3): print_in_color(msg, 'red', line_feed=False) elif elapsed_seconds > (60 * 60 * 24 * 1): print_in_color(msg, 'amber', line_feed=False) else: print_in_color(msg, 'green', line_feed=False) # If there is no record of a verified copy, the directory doesn't # exist on this device so enter a dash rather than an age. else: print '{:<33}'.format('-'), print '|' print '+{:-<24}+{:-<35}+'.format('', '') # Add a blank line after the table. print '\n'
def sync_virtual_machine(self): """ Sync a Virtualbox virtual machine using rsync. """ # First, check that the VM isn't running. # This command's return code is `0` if it is running, `1` if it isn't. vboxmanage_cmd = 'vboxmanage showvminfo "%s" | grep -c "%s"' % ( os.path.basename(self.source), 'State: running' ) return_code = subprocess.call(vboxmanage_cmd, shell=True) if return_code == 0: vm_running = True else: vm_running = False # Only sync a virtual machine if it isn't running. if not vm_running: rsync_cmd = 'rsync -r --progress --inplace --no-whole-file ' + \ '--delete %s/ %s' % (self.source, self.target) subprocess.call(rsync_cmd, shell=True) log(' %s:' % self.name) log(' copied: True') self.verify_backup() else: print_in_color( "The '%s' VM is running and WILL NOT be backed up." % self.name, 'red' ) log(' %s:' % self.name) log(' copied: False')
def print_screen_header(): """ Clears the terminal and prints the program's heading. """ os.system('clear') print_in_color('*' * 60, 'bold') print_in_color('%s %s %s' % ('*' * 23, 'EQUANIMITY', '*' * 23), 'bold') print_in_color('*' * 60, 'bold') print '\nDevice detected: %s\n' % config.DEVICE['name']
def print_header(header): """ Prints a header with a line of asterisks above and below it. """ print '' print_in_color('*' * 60, 'bold') padding = '*' * ((60 - len(header) - 4) / 2) padded_header = '%s %s %s' % (padding, header, padding) # If the header has an odd number of characters, the padded header will be # one asterisk too short, so correct for this. if len(padded_header) == 59: padded_header += '*' print_in_color(padded_header, 'bold') print_in_color('*' * 60, 'bold') print ''
# If the `-s/--summary` option is passed when the program is invoked, # display the devices comparison then quit. if args.summary: print_devices_comparison() sys.exit() # If a device is present, perform a backup. if config.DEVICE: device = Device(config.DEVICE) print_screen_header() device.print_summary() device.queue_directories() device.print_directory_queue() confirm_continue() write_log_headers() backup_directories(device.directory_queue) device.sync_logs_and_config() print_devices_comparison(with_locations=True) else: print_in_color("\nNo backup device was found.\n", "red")