Last active
January 31, 2023 08:28
-
-
Save mohatb/1177b517f986e6028cc35c0a6c9f8787 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Backup WSL | |
| #./script.sh backup ~/wsl-backup | |
| # Restore WSL | |
| #./script.sh restore ~/wsl-backup | |
| # Backup WSL | |
| backup_wsl() { | |
| local backup_dir="$1" | |
| # Create backup directory if it doesn't exist | |
| if [ ! -d "$backup_dir" ]; then | |
| mkdir -p "$backup_dir" | |
| fi | |
| # Get list of WSL distros | |
| local distros="$(wsl --list --quiet)" | |
| # Backup each distro | |
| while read -r distro; do | |
| wsl --export "$distro" "$backup_dir/$distro.tar" | |
| done <<< "$distros" | |
| } | |
| # Restore WSL | |
| restore_wsl() { | |
| local backup_dir="$1" | |
| # Get list of backup files | |
| local backup_files="$(ls "$backup_dir")" | |
| # Restore each backup file | |
| while read -r backup_file; do | |
| local distro="$(basename "$backup_file" .tar)" | |
| wsl --import "$distro" "$backup_dir/$backup_file" | |
| done <<< "$backup_files" | |
| } | |
| # Main function | |
| main() { | |
| local command="$1" | |
| local backup_dir="$2" | |
| # Validate arguments | |
| if [ -z "$command" ] || [ -z "$backup_dir" ]; then | |
| echo "Usage: $0 <backup|restore> <backup_dir>" | |
| exit 1 | |
| fi | |
| if [ "$command" == "backup" ]; then | |
| backup_wsl "$backup_dir" | |
| elif [ "$command" == "restore" ]; then | |
| restore_wsl "$backup_dir" | |
| else | |
| echo "Error: Invalid command" | |
| exit 1 | |
| fi | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment