CoolFace
Apppublic

wonkitty/apple_oh

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
backups_test.py139 linesDownload Raw Back to utils
1 2import os3import shutil4import hashlib5import time6 7LOGS_FOLDER = '/content/Applio-RVC-Fork/logs'8WEIGHTS_FOLDER = '/content/Applio-RVC-Fork/weights'9GOOGLE_DRIVE_PATH = '/content/drive/MyDrive/RVC_Backup'10 11def import_google_drive_backup():12    print("Importing Google Drive backup...")13    GOOGLE_DRIVE_PATH = '/content/drive/MyDrive/RVC_Backup'  # change this to your Google Drive path14    LOGS_FOLDER = '/content/Applio-RVC-Fork/logs'15    WEIGHTS_FOLDER = '/content/Applio-RVC-Fork/weights'16    weights_exist = False17    files_to_copy = []18    weights_to_copy = []19    20    def handle_files(root, files, is_weight_files=False):21        for filename in files:22            filepath = os.path.join(root, filename)23            if filename.endswith('.pth') and is_weight_files:24                weights_exist = True25                backup_filepath = os.path.join(WEIGHTS_FOLDER, os.path.relpath(filepath, GOOGLE_DRIVE_PATH))26            else:27                backup_filepath = os.path.join(LOGS_FOLDER, os.path.relpath(filepath, GOOGLE_DRIVE_PATH))28            backup_folderpath = os.path.dirname(backup_filepath)29            if not os.path.exists(backup_folderpath):30                os.makedirs(backup_folderpath)31                print(f'Created folder: {backup_folderpath}', flush=True)32            if is_weight_files:33                weights_to_copy.append((filepath, backup_filepath))34            else:35                files_to_copy.append((filepath, backup_filepath))36 37    for root, dirs, files in os.walk(os.path.join(GOOGLE_DRIVE_PATH, 'logs')):38        handle_files(root, files)39    40    for root, dirs, files in os.walk(os.path.join(GOOGLE_DRIVE_PATH, 'weights')):41        handle_files(root, files, True)42 43    # Copy files in batches44    total_files = len(files_to_copy)45    start_time = time.time()46    for i, (source, dest) in enumerate(files_to_copy, start=1):47        with open(source, 'rb') as src, open(dest, 'wb') as dst:48            shutil.copyfileobj(src, dst, 1024*1024)  # 1MB buffer size49        # Report progress every 5 seconds or after every 100 files, whichever is less frequent50        if time.time() - start_time > 5 or i % 100 == 0:51            print(f'\rCopying file {i} of {total_files} ({i * 100 / total_files:.2f}%)', end="")52            start_time = time.time()53    print(f'\nImported {len(files_to_copy)} files from Google Drive backup')54 55    # Copy weights in batches56    total_weights = len(weights_to_copy)57    start_time = time.time()58    for i, (source, dest) in enumerate(weights_to_copy, start=1):59        with open(source, 'rb') as src, open(dest, 'wb') as dst:60            shutil.copyfileobj(src, dst, 1024*1024)  # 1MB buffer size61        # Report progress every 5 seconds or after every 100 files, whichever is less frequent62        if time.time() - start_time > 5 or i % 100 == 0:63            print(f'\rCopying weight file {i} of {total_weights} ({i * 100 / total_weights:.2f}%)', end="")64            start_time = time.time()65    if weights_exist:66        print(f'\nImported {len(weights_to_copy)} weight files')67        print("Copied weights from Google Drive backup to local weights folder.")68    else:69        print("\nNo weights found in Google Drive backup.")70    print("Google Drive backup import completed.")71 72def backup_files():73    print("\n Starting backup loop...")74    last_backup_timestamps_path = os.path.join(LOGS_FOLDER, 'last_backup_timestamps.txt')75    fully_updated = False  # boolean to track if all files are up to date76    try:77        with open(last_backup_timestamps_path, 'r') as f:78            last_backup_timestamps = dict(line.strip().split(':') for line in f)79    except:80        last_backup_timestamps = {}81 82    while True:83        updated = False84        files_to_copy = []85        files_to_delete = []86 87        for root, dirs, files in os.walk(LOGS_FOLDER):88            for filename in files:89                if filename != 'last_backup_timestamps.txt':90                    filepath = os.path.join(root, filename)91                    if os.path.isfile(filepath):92                        backup_filepath = os.path.join(GOOGLE_DRIVE_PATH, os.path.relpath(filepath, LOGS_FOLDER))93                        backup_folderpath = os.path.dirname(backup_filepath)94 95                        if not os.path.exists(backup_folderpath):96                            os.makedirs(backup_folderpath)97                            print(f'Created backup folder: {backup_folderpath}', flush=True)98 99                        # check if file has changed since last backup100                        last_backup_timestamp = last_backup_timestamps.get(filepath)101                        current_timestamp = os.path.getmtime(filepath)102                        if last_backup_timestamp is None or float(last_backup_timestamp) < current_timestamp:103                            files_to_copy.append((filepath, backup_filepath))  # add to list of files to copy104                            last_backup_timestamps[filepath] = str(current_timestamp)  # update last backup timestamp105                            updated = True106                            fully_updated = False  # if a file is updated, all files are not up to date107 108        # check if any files were deleted in Colab and delete them from the backup drive109        for filepath in list(last_backup_timestamps.keys()):110            if not os.path.exists(filepath):111                backup_filepath = os.path.join(GOOGLE_DRIVE_PATH, os.path.relpath(filepath, LOGS_FOLDER))112                if os.path.exists(backup_filepath):113                    files_to_delete.append(backup_filepath)  # add to list of files to delete114                del last_backup_timestamps[filepath]115                updated = True116                fully_updated = False  # if a file is deleted, all files are not up to date117 118        # Copy files in batches119        if files_to_copy:120            for source, dest in files_to_copy:121                shutil.copy2(source, dest)122            print(f'Copied or updated {len(files_to_copy)} files')123 124        # Delete files in batches125        if files_to_delete:126            for file in files_to_delete:127                os.remove(file)128            print(f'Deleted {len(files_to_delete)} files')129 130        if not updated and not fully_updated:131            print("Files are up to date.")132            fully_updated = True  # if all files are up to date, set the boolean to True133            copy_weights_folder_to_drive()134 135        with open(last_backup_timestamps_path, 'w') as f:136            for filepath, timestamp in last_backup_timestamps.items():137                f.write(f'{filepath}:{timestamp}\n')138        time.sleep(15)  # wait for 15 seconds before checking again139