CoolFace
Apppublic

wonkitty/apple_oh

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
backups.py142 linesDownload Raw Back to utils
1import os2import shutil3import hashlib4import time5import base646 7 8 9 10LOGS_FOLDER = '/content/Applio-RVC-Fork/logs'11WEIGHTS_FOLDER = '/content/Applio-RVC-Fork/weights'12GOOGLE_DRIVE_PATH = '/content/drive/MyDrive/RVC_Backup'13 14def import_google_drive_backup():15    print("Importing Google Drive backup...")16    weights_exist = False17    for root, dirs, files in os.walk(GOOGLE_DRIVE_PATH):18        for filename in files:19            filepath = os.path.join(root, filename)20            if os.path.isfile(filepath) and not filepath.startswith(os.path.join(GOOGLE_DRIVE_PATH, 'weights')):21                backup_filepath = os.path.join(LOGS_FOLDER, os.path.relpath(filepath, GOOGLE_DRIVE_PATH))22                backup_folderpath = os.path.dirname(backup_filepath)23                if not os.path.exists(backup_folderpath):24                    os.makedirs(backup_folderpath)25                    print(f'Created backup folder: {backup_folderpath}', flush=True)26                shutil.copy2(filepath, backup_filepath) # copy file with metadata27                print(f'Imported file from Google Drive backup: {filename}')28            elif filepath.startswith(os.path.join(GOOGLE_DRIVE_PATH, 'weights')) and filename.endswith('.pth'):29                weights_exist = True30                weights_filepath = os.path.join(WEIGHTS_FOLDER, os.path.relpath(filepath, os.path.join(GOOGLE_DRIVE_PATH, 'weights')))31                weights_folderpath = os.path.dirname(weights_filepath)32                if not os.path.exists(weights_folderpath):33                    os.makedirs(weights_folderpath)34                    print(f'Created weights folder: {weights_folderpath}', flush=True)35                shutil.copy2(filepath, weights_filepath) # copy file with metadata36                print(f'Imported file from weights: {filename}')37    if weights_exist:38        print("Copied weights from Google Drive backup to local weights folder.")39    else:40        print("No weights found in Google Drive backup.")41    print("Google Drive backup import completed.")42 43def get_md5_hash(file_path):44    hash_md5 = hashlib.md5()45    with open(file_path, "rb") as f:46        for chunk in iter(lambda: f.read(4096), b""):47            hash_md5.update(chunk)48    return hash_md5.hexdigest()49 50def copy_weights_folder_to_drive():51    destination_folder = os.path.join(GOOGLE_DRIVE_PATH, 'weights')52    try:53        if not os.path.exists(destination_folder):54            os.makedirs(destination_folder)55 56        num_copied = 057        for filename in os.listdir(WEIGHTS_FOLDER):58            if filename.endswith('.pth'):59                source_file = os.path.join(WEIGHTS_FOLDER, filename)60                destination_file = os.path.join(destination_folder, filename)61                if not os.path.exists(destination_file):62                    shutil.copy2(source_file, destination_file)63                    num_copied += 164                    print(f"Copied {filename} to Google Drive!")65 66        if num_copied == 0:67            print("No new finished models found for copying.")68        else:69            print(f"Finished copying {num_copied} files to Google Drive!")70 71    except Exception as e:72        print(f"An error occurred while copying weights: {str(e)}")73        # You can log the error or take appropriate actions here.74 75def backup_files():76    print("\nStarting backup loop...")77    last_backup_timestamps_path = os.path.join(LOGS_FOLDER, 'last_backup_timestamps.txt')78    fully_updated = False  # boolean to track if all files are up to date79    80    while True:81        try:82            updated = False  # flag to check if any files were updated83            last_backup_timestamps = {}84 85            try:86                with open(last_backup_timestamps_path, 'r') as f:87                    last_backup_timestamps = dict(line.strip().split(':') for line in f)88            except FileNotFoundError:89                pass  # File does not exist yet, which is fine90            91            for root, dirs, files in os.walk(LOGS_FOLDER):92                for filename in files:93                    if filename != 'last_backup_timestamps.txt':94                        filepath = os.path.join(root, filename)95                        if os.path.isfile(filepath):96                            backup_filepath = os.path.join(GOOGLE_DRIVE_PATH, os.path.relpath(filepath, LOGS_FOLDER))97                            backup_folderpath = os.path.dirname(backup_filepath)98                            if not os.path.exists(backup_folderpath):99                                os.makedirs(backup_folderpath)100                                print(f'Created backup folder: {backup_folderpath}', flush=True)101                            # check if file has changed since last backup102                            last_backup_timestamp = last_backup_timestamps.get(filepath)103                            current_timestamp = os.path.getmtime(filepath)104                            if last_backup_timestamp is None or float(last_backup_timestamp) < current_timestamp:105                                shutil.copy2(filepath, backup_filepath)  # copy file with metadata106                                last_backup_timestamps[filepath] = str(current_timestamp)  # update last backup timestamp107                                if last_backup_timestamp is None:108                                    print(f'Backed up file: {filename}')109                                else:110                                    print(f'Updating backed up file: {filename}')111                                updated = True112                                fully_updated = False  # if a file is updated, all files are not up to date113            114            # check if any files were deleted in Colab and delete them from the backup drive115            for filepath in list(last_backup_timestamps.keys()):116                if not os.path.exists(filepath):117                    backup_filepath = os.path.join(GOOGLE_DRIVE_PATH, os.path.relpath(filepath, LOGS_FOLDER))118                    if os.path.exists(backup_filepath):119                        os.remove(backup_filepath)120                        print(f'Deleted file: {filepath}')121                    del last_backup_timestamps[filepath]122                    updated = True123                    fully_updated = False  # if a file is deleted, all files are not up to date124            125            if not updated and not fully_updated:126                print("Files are up to date.")127                fully_updated = True  # if all files are up to date, set the boolean to True128                copy_weights_folder_to_drive()129                sleep_time = 15130            else:131                sleep_time = 0.1132            133            with open(last_backup_timestamps_path, 'w') as f:134                for filepath, timestamp in last_backup_timestamps.items():135                    f.write(f'{filepath}:{timestamp}\n')136            137            time.sleep(sleep_time)  # wait for 15 seconds before checking again, or 0.1s if not fully up to date to speed up backups138        139        except Exception as e:140            print(f"An error occurred: {str(e)}")141            # You can log the error or take appropriate actions here.142