def make_missing_dirs(config, unpack_path, p_len): ''' This is an add on function that restores permissions to folders. This was a known bug for all of alpha but is finally patched. Folder permissions aren't stored in a tar so a separate system was created to handle this using pickle and paf functions. ''' fname = 'custom_dirs.make_missing_dirs()' # Find All Subdirs dirs = paf.find_subdirs(unpack_path) # Sort in Subdirs in Descending Paths dirs.sort(key=lambda x: x.count('/')) for d in dirs: if not os.path.exists(d[p_len:]): os.makedirs(d[p_len:]) if os.path.exists(unpack_path + '/folder_permissions.pickle'): # Load Folder Permissions Pickle folder_perms = pickle.load( open(unpack_path + '/folder_permissions.pickle', 'rb')) for x in folder_perms: os.system('chmod ' + paf.perm_to_num(x[1]) + ' ' + paf.escape_bash_input(x[0])) os.system('chown ' + x[2] + ':' + x[3] + ' ' + paf.escape_bash_input(x[0])) else: paf.write_to_log(fname, 'Folder Permissions Pickle is Missing!', config['log'])
def force_overwrite(config, unpack_path, p_len): ''' Restore Files Without Checksum ''' fname = 'custom_dirs.force_overwrite()' # Allow Exit Since This Is Bad Idea paf.prWarning( 'OVERWRITING FILES WITHOUT CHECKSUMS CAN BE EXTREMELY DANGEROUS!') if paf.yn_frame( 'Do You Still Want to Continue and Restore ALL The Files You Stored?' ) is False: return # Overwrite Files paf.write_to_log(fname, 'Starting Force Overwrite Process...', config['log']) print( 'Starting Full File Restore! Please Be Patient As All Files are Overwritten...' ) fs_stored = paf.find_files(unpack_path) try: fs_stored.remove(unpack_path + '/folder_permissions.pickle') except Exception: pass make_missing_dirs(config, unpack_path, p_len) for f in track(fs_stored, description='Overwriting Files'): os.system('cp -af ' + paf.escape_bash_input(f) + ' ' + paf.escape_bash_input(f[p_len:])) paf.prSuccess('Done Overwriting Files!') paf.write_to_log(fname, 'Finished Force Overwrite Of Files', config['log'])
def read_hosts_yaml(conf): """ Read and parse yaml file containing a pool of remote hosts. """ conf_path = str(base + paf.escape_bash_input(conf) + '.yaml') if not os.path.exists(conf_path): sys.exit('Error: No Config File Named "' + conf + '" Found in "' + base + '"!') with open(conf_path) as file: hosts = yaml.full_load(file) mand = {'NAME', 'USER', 'IP', 'PORT'} error = 0 for host in hosts: x = hosts[host] for val in mand: if val not in x: print('Error: Host ' + host + ' Is Missing The Field "' + val + '"!') error += 1 if error == 0: print('Successfully Loaded Pool of Hosts') return hosts else: print('') sys.exit(str(error) + ' Errors Were Found in ' + conf_path + '!')
def smart_overwrite(config, csum_results, unpack_path, p_len): ''' Main File Restoration Logic ''' fname = 'custom_dirs.smart_overwrite()' if csum_results['changed']: paf.write_to_log( fname, 'Found ' + str(len(csum_results['changed'])) + ' Changed Files', config['log']) print('') print('#################################') paf.prWarning('The Following Files Have Changed:') print('#################################') print('') for f in list(csum_results['changed']): paf.prChanged(f[0]) print('') if paf.yn_frame('Do You Want to Restore ' + str(len(csum_results['changed'])) + ' Files That Have Been CHANGED?') is True: for f in track(csum_results['changed'], description='Restoring Changed Files'): os.system('sudo cp -af ' + paf.escape_bash_input(unpack_path + f[0]) + ' ' + paf.escape_bash_input(f[0])) paf.write_to_log(fname, 'Restored Changed Files', config['log']) else: paf.write_to_log(fname, 'User Declined Restoring Changed Files', config['log']) if csum_results['removed']: paf.write_to_log( fname, 'Found ' + str(len(csum_results['removed'])) + ' Removed Files', config['log']) print('') print('######################################') paf.prWarning('The Following Files Have Been Removed:') print('######################################') print('') for f in list(csum_results['removed']): paf.prRemoved(f[p_len:]) print('') if paf.yn_frame('Do You Want to Restore ' + str(len(csum_results['removed'])) + ' Files That Have Been REMOVED?') is True: make_missing_dirs(config, unpack_path, p_len) for f in track(csum_results['removed'], description='Restoring Removed Files'): os.system('cp -af ' + paf.escape_bash_input(f) + ' ' + paf.escape_bash_input(f[p_len:])) paf.write_to_log(fname, 'Restored Removed Files', config['log']) else: paf.write_to_log(fname, 'User Declined Restoring Removed Files', config['log']) if csum_results['added']: paf.write_to_log( fname, 'Found ' + str(len(csum_results['added'])) + ' New Files', config['log']) print('') print('####################################') paf.prWarning('The Following Files Have Been Added:') print('####################################') print('') for f in list(csum_results['added']): paf.prAdded(f) print('') if paf.yn_frame('Do You Want to Remove ' + str(len(csum_results['added'])) + ' Files That Have Been ADDED?') is True: for f in track(csum_results['added'], description='Removing New Files'): os.system('rm ' + f) paf.write_to_log(fname, 'Removed New Files', config['log']) else: paf.write_to_log(fname, 'User Declined Removing New Files', config['log']) paf.prSuccess('Done Restoring Files!') paf.write_to_log(fname, 'Done Restoring Files', config['log'])