Files
dotfiles/bin/actions/source.py

82 lines
2.7 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import sys
import subprocess
# Add the helpers directory to the path
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'helpers'))
from functions import printfe
def get_borg_passphrase():
"""Get Borg passphrase from 1Password"""
try:
result = subprocess.run(
["op", "item", "get", "Borg Backup", "--vault=Dotfiles", "--fields=password", "--reveal"],
capture_output=True,
text=True,
check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError:
printfe("red", "Error: Failed to retrieve Borg passphrase from 1Password")
return None
def main():
"""Generate export commands for Borg environment variables"""
args = sys.argv[1:] if len(sys.argv) > 1 else []
# Get passphrase from 1Password
passphrase = get_borg_passphrase()
if not passphrase:
return 1
# Generate the export commands
exports = [
f'export BORG_REPO="/mnt/object_storage/borg-repo"',
f'export BORG_PASSPHRASE="{passphrase}"',
f'export BORG_CACHE_DIR="/home/menno/.config/borg/cache"',
f'export BORG_CONFIG_DIR="/home/menno/.config/borg/config"',
f'export BORG_SECURITY_DIR="/home/menno/.config/borg/security"',
f'export BORG_KEYS_DIR="/home/menno/.config/borg/keys"'
]
# Check if we're being eval'd (no arguments and stdout is a pipe)
if not args and not os.isatty(sys.stdout.fileno()):
# Just output the export commands for eval
for export_cmd in exports:
print(export_cmd)
return 0
# Print instructions and examples
printfe("cyan", "🔧 Borg Environment Setup")
print()
printfe("yellow", "Run the following command to setup your shell:")
print()
printfe("green", "eval $(dotf source)")
print()
printfe("red", "⚠️ Repository Permission Issue:")
printfe("white", "The Borg repository was created by root, so you need sudo:")
print()
printfe("green", "sudo -E borg list")
printfe("green", "sudo -E borg info")
print()
printfe("yellow", "Or copy and paste these exports:")
print()
# Output the export commands
for export_cmd in exports:
print(export_cmd)
print()
printfe("cyan", "📋 Borg commands (use with sudo -E):")
printfe("white", " sudo -E borg list # List all backups")
printfe("white", " sudo -E borg info # Repository info")
printfe("white", " sudo -E borg list ::archive-name # List files in backup")
printfe("white", " sudo -E borg mount . ~/borg-mount # Mount as filesystem")
return 0
if __name__ == "__main__":
sys.exit(main())