This repository contains a robust bash script for backing up conda environments to YAML files.
backup_envs.sh- Main backup scripttest_backup.sh- Test script to verify backup functionalityREADME.md- This documentation
The improved backup script includes:
- ✅ Proper conda initialization using
conda shell.bash hook - ✅ Timestamped backup directories to avoid overwrites
- ✅ Error handling with colored output
- ✅ Safe environment parsing that handles conda's output format correctly
- ✅ Base environment protection (skips base to avoid issues)
- ✅ Backup summary with restoration instructions
- ✅ Cross-platform compatibility (works with Anaconda, Miniconda, etc.)
./backup_envs.shThis will:
- Create a timestamped backup directory (e.g.,
conda_envs_20241201_143022) - Export all conda environments to YAML files
- Create a summary file with restoration instructions
./test_backup.shThis will:
- Create a test conda environment
- Run the backup script
- Verify the backup was created correctly
- Clean up the test environment
The script creates a directory structure like this:
conda_envs_20241201_143022/
├── backup_summary.txt
├── conda_envs_list.txt
├── my_env1_env.yml
├── my_env2_env.yml
└── ...
To restore an environment from backup:
conda env create -f environment_name_env.ymlThe original script had several problems:
- Hardcoded paths: Assumed Anaconda was in
~/anaconda3/ - Incorrect conda activation: Used
source ~/anaconda3/bin/activateinstead of proper initialization - Poor file parsing: The logic for reading
conda env listoutput was flawed - No error handling: Would fail silently on errors
- No timestamping: Could overwrite previous backups
- Base environment issues: Could cause problems when trying to export the base environment
- Bash shell
- Conda (Anaconda, Miniconda, or any conda distribution)
- Write permissions in the current directory
Make sure conda is installed and in your PATH. You may need to initialize conda first:
conda init bashMake the script executable:
chmod +x backup_envs.shThis is normal if you don't have any conda environments created yet.
This script is provided as-is for educational and practical use.