Skip to content

Instantly share code, notes, and snippets.

@epassaro
Created September 14, 2025 23:40
Show Gist options
  • Select an option

  • Save epassaro/9f6cf86887422889919f761898b43468 to your computer and use it in GitHub Desktop.

Select an option

Save epassaro/9f6cf86887422889919f761898b43468 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import shutil
import socket
import yaml
from pathlib import Path
import sys
CONFIG_FILE = Path.home() / ".config" / "include.yml"
TEMPLATE = """\
backup_dir: /path/to/backup/dir
include:
- .bashrc
- .config/include.yml
"""
def copy_item(src: Path, dest: Path):
try:
if src.is_dir():
shutil.copytree(src, dest, dirs_exist_ok=True)
else:
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dest)
print(f"[OK] {src} -> {dest}")
except Exception as e:
print(f"[ERROR] {src}: {e}")
def run_backup(home: Path, hostname: str, include_paths, backup_dir: Path):
for rel in include_paths:
src = home / rel
if not src.exists():
print(f"[WARN] {src} no existe")
continue
dest = backup_dir / hostname / rel
copy_item(src, dest)
def main():
if not CONFIG_FILE.exists():
CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
CONFIG_FILE.write_text(TEMPLATE)
print(f"Se creó un archivo de configuración por defecto en {CONFIG_FILE}. Es necesario editarlo antes de volver a ejecutar.")
sys.exit(0)
hostname = socket.gethostname()
with open(CONFIG_FILE, "r") as f:
config = yaml.safe_load(f)
backup_dir = Path(config.get("backup_dir", "")).expanduser()
if not backup_dir:
raise ValueError("Falta 'backup_dir' en el archivo de configuración")
include_paths = config.get("include", [])
if not include_paths:
print("No hay rutas definidas en include.yml")
return
home = Path.home()
run_backup(home, hostname, include_paths, backup_dir)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment