diff --git a/content/index.md b/content/index.md index 38992748..88fc4e75 100644 --- a/content/index.md +++ b/content/index.md @@ -2,4 +2,6 @@ hello friend. welcome to my [[digital_gardening_info|digital garden]]. +here is a python script i wrote: [[remarkable_backup_python_script.md]] + \ No newline at end of file diff --git a/content/remarkable_backup_python_script.md b/content/remarkable_backup_python_script.md new file mode 100644 index 00000000..a4845280 --- /dev/null +++ b/content/remarkable_backup_python_script.md @@ -0,0 +1,224 @@ +```python +from rmcl import Item, Document, Folder +from rmrl import render +import os +import shutil +import trio + +async def download_documents(remote_folder_id, local_download_dir): + root = await Item.get_by_id(remote_folder_id) + + for child in root.children: + if isinstance(child, Document): + await download_document(child, local_download_dir) + elif isinstance(child, Folder): + await download_documents_recursive(child, local_download_dir) + +async def download_documents_recursive(folder, local_download_dir): + for child in folder.children: + if isinstance(child, Document): + await download_document(child, local_download_dir) + elif isinstance(child, Folder): + await download_documents_recursive(child, local_download_dir) + +async def download_document(document, local_download_dir): + print(f"Downloading {document.name}...") + contents = await document.contents() + file_path = os.path.join(local_download_dir, f"{document.name}.zip") + + with open(file_path, "wb") as local_file: + local_file.write(contents.read()) + + print(f"{document.name} downloaded to {file_path}") + +def render_and_backup(local_download_dir, local_backup_dir): + for filename in os.listdir(local_download_dir): + if filename.endswith(".zip"): + source_zip = os.path.join(local_download_dir, filename) + + # Render the reMarkable document to PDF using rmrl + pdf_content = render(source_zip) + + # Save the rendered PDF to the output directory + output_file_path = os.path.join(local_backup_dir, f"{filename[:-4]}_rendered.pdf") + with open(output_file_path, "wb") as pdf_file: + pdf_file.write(pdf_content.read()) + + print(f"Document rendered and saved to: {output_file_path}") + +async def main(): + remote_folder_id = "" # Specify the ID of the folder you want to download + local_download_dir = "/path/to/local/download" # Specify your local download directory + local_backup_dir = "/path/to/local/backup" # Specify your local backup directory + + os.makedirs(local_download_dir, exist_ok=True) + os.makedirs(local_backup_dir, exist_ok=True) + + await download_documents(remote_folder_id, local_download_dir) + render_and_backup(local_download_dir, local_backup_dir) + +if __name__ == "__main__": + trio.run(main) + +``` + +using, paramiko, i can establish an ssh connection to the remarkable tablet: +```python +import paramiko + +def backup_documents_ssh(remote_path, local_destination): + # SSH connection details + ssh_host = 'remarkable' # replace with your remarkable's IP or hostname + ssh_port = 22 + ssh_user = 'root' + ssh_password = 'PlWOIV4Wix' # replace with your password or use key-based authentication + + # Create an SSH client + ssh_client = paramiko.SSHClient() + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + try: + # Connect to the remote server + ssh_client.connect(ssh_host, ssh_port, ssh_user, ssh_password) + + # SCP command to copy files from the remote server to the local destination + scp_command = f'scp -r {remote_path} {local_destination}' + + # Execute the SCP command + stdin, stdout, stderr = ssh_client.exec_command(scp_command) + + # Wait for the command to complete + stdout.channel.recv_exit_status() + + # Check for errors + if stderr.read().decode(): + print(f"Error: {stderr.read().decode()}") + else: + print("Backup successful!") + + except Exception as e: + print(f"An error occurred: {e}") + finally: + # Close the SSH connection + ssh_client.close() + +if __name__ == "__main__": + # Specify the remote path and local destination + remote_path = '/home/root/.local/share/remarkable/xochitl/' + local_destination = 'I:\iCloudDrive\iCloud~md~obsidian\BRAIN\00-09_admin\03_remarkable\03.01_remarkable_backup' + + # Perform the backup + backup_documents_ssh(remote_path, local_destination) + +``` + + +# final script +```python +import os +import paramiko +import shutil +import trio +from rmcl import Item, Document, Folder +from rmrl import render + +async def download_documents(remote_folder_id, local_download_dir): + root = await Item.get_by_id(remote_folder_id) + + for child in root.children: + if isinstance(child, Document): + await download_document(child, local_download_dir) + elif isinstance(child, Folder): + await download_documents_recursive(child, local_download_dir) + +async def download_documents_recursive(folder, local_download_dir): + for child in folder.children: + if isinstance(child, Document): + await download_document(child, local_download_dir) + elif isinstance(child, Folder): + await download_documents_recursive(child, local_download_dir) + +async def download_document(document, local_download_dir): + print(f"Downloading {document.name}...") + contents = await document.contents() + file_path = os.path.join(local_download_dir, f"{document.name}.zip") + + with open(file_path, "wb") as local_file: + local_file.write(contents.read()) + + print(f"{document.name} downloaded to {file_path}") + +def render_and_backup(local_download_dir, local_backup_dir): + for filename in os.listdir(local_download_dir): + if filename.endswith(".zip"): + source_zip = os.path.join(local_download_dir, filename) + + # Render the reMarkable document to PDF using rmrl + pdf_content = render(source_zip) + + # Save the rendered PDF to the output directory + output_file_path = os.path.join(local_backup_dir, f"{filename[:-4]}_rendered.pdf") + with open(output_file_path, "wb") as pdf_file: + pdf_file.write(pdf_content.read()) + + print(f"Document rendered and saved to: {output_file_path}") + +def backup_documents_ssh(remote_path, local_destination): + # SSH connection details + ssh_host = 'remarkable' # replace with your remarkable's IP or hostname + ssh_port = 22 + ssh_user = 'root' + ssh_password = 'your_password' # replace with your password or use key-based authentication + + # Create an SSH client + ssh_client = paramiko.SSHClient() + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + try: + # Connect to the remote server + ssh_client.connect(ssh_host, ssh_port, ssh_user, ssh_password) + + # SCP command to copy files from the remote server to the local destination + scp_command = f'scp -r {remote_path} {local_destination}' + + # Execute the SCP command + stdin, stdout, stderr = ssh_client.exec_command(scp_command) + + # Wait for the command to complete + stdout.channel.recv_exit_status() + + # Check for errors + if stderr.read().decode(): + print(f"Error: {stderr.read().decode()}") + else: + print("Backup successful!") + + except Exception as e: + print(f"An error occurred: {e}") + finally: + # Close the SSH connection + ssh_client.close() + +async def main(): + remote_folder_id = "" # Specify the ID of the folder you want to download + local_download_dir = "/path/to/local/download" # Specify your local download directory + local_backup_dir = "/path/to/local/backup" # Specify your local backup directory + + os.makedirs(local_download_dir, exist_ok=True) + os.makedirs(local_backup_dir, exist_ok=True) + + # Download documents from reMarkable + await download_documents(remote_folder_id, local_download_dir) + + # Render and backup downloaded documents + render_and_backup(local_download_dir, local_backup_dir) + + # Perform SSH backup + remote_path = '/home/root/.local/share/remarkable/xochitl/' + local_destination = 'I:\iCloudDrive\iCloud~md~obsidian\BRAIN\00-09_admin\03_remarkable\03.01_remarkable_backup' + backup_documents_ssh(remote_path, local_destination) + +if __name__ == "__main__": + trio.run(main) + +``` \ No newline at end of file