Backing Up and Restoring Your WSL2 Ubuntu Environment
When upgrading your WSL2 Ubuntu environment, it’s crucial to backup your files first. This guide provides scripts to backup your WSL2 Ubuntu files to Google Drive before an upgrade and restore them afterwards. These scripts use rclone, a powerful command-line program to manage files on cloud storage.
Note: This method is particularly handy for users with limited disk space for local backups or for those who want to keep a copy of their data in the cloud. It allows you to create backups without consuming local storage and provides the added benefit of off-site data storage.
Prerequisites
- WSL2 with Ubuntu installed
rcloneinstalled and configured with your Google Drive
- To install rclone:
sudo apt install rclone - To configure:
rclone config(create a remote named “googledrive” for Google Drive)
Backup Script
Save this script as backup_wsl2_ubuntu.sh:
#!/bin/bash
# Function to backup directly to Google Drive using rclone
backup_to_gdrive() {
echo "Backing up directly to Google Drive..."
BACKUP_DATE=$(date +"%Y%m%d_%H%M%S")
GDRIVE_BACKUP_PATH="googledrive:WSL_Backup/ubuntu_backup_$BACKUP_DATE"
# Check if rclone is installed
if ! command -v rclone &> /dev/null; then
echo "rclone is not installed. Please install it first."
exit 1
fi
# Check if alangoogledrive remote exists
if ! rclone listremotes | grep -q "^googledrive:"; then
echo "The 'googledrive' remote is not found in rclone configuration."
echo "Please ensure your Google Drive remote is correctly set up."
exit 1
fi
# Perform the backup
if rclone sync /home/$(whoami) "$GDRIVE_BACKUP_PATH" --exclude ".cache/**" --progress; then
echo "Backup completed successfully to $GDRIVE_BACKUP_PATH"
else
echo "Backup failed. Exiting."
exit 1
fi
}
# Main script
echo "WSL2 Ubuntu Backup Script"
backup_to_gdrive
To use the backup script:
- Save the script and make it executable:
chmod +x backup_wsl2_ubuntu.sh - Run it:
./backup_wsl2_ubuntu.sh
This will create a new folder in your Google Drive named “WSL_Backup” with a timestamped backup of your home directory.
Restore Script
Save this script as restore_wsl2_ubuntu.sh:
#!/bin/bash
# Function to restore from Google Drive using rclone
restore_from_gdrive() {
echo "Restoring from Google Drive backup..."
# Check if rclone is installed
if ! command -v rclone &> /dev/null; then
echo "rclone is not installed. Please install it first."
exit 1
fi
# Check if googledrive remote exists
if ! rclone listremotes | grep -q "^googledrive:"; then
echo "The 'googledrive' remote is not found in rclone configuration."
echo "Please ensure your Google Drive remote is correctly set up."
exit 1
fi
# List available backups
echo "Available backups:"
rclone lsf googledrive:WSL_Backup/ | sort -r
# Ask user to choose a backup
read -p "Enter the name of the backup you want to restore: " BACKUP_NAME
GDRIVE_BACKUP_PATH="googledrive:WSL_Backup/$BACKUP_NAME"
# Check if the chosen backup exists
if ! rclone lsf "$GDRIVE_BACKUP_PATH" &> /dev/null; then
echo "The specified backup does not exist. Please check the name and try again."
exit 1
fi
# Ask for confirmation before restoring
read -p "This will overwrite files in your home directory. Are you sure you want to proceed? (y/n): " confirm
if [[ $confirm != [yY] && $confirm != [yY][eE][sS] ]]; then
echo "Restore cancelled."
exit 1
fi
# Perform the restore
echo "Restoring files to /home/$(whoami)..."
if rclone sync "$GDRIVE_BACKUP_PATH" /home/$(whoami) --progress; then
echo "Restore completed successfully from $GDRIVE_BACKUP_PATH"
else
echo "Restore failed. Please check the error messages above."
exit 1
fi
echo "Restore process completed. Please verify your files."
}
# Main script
echo "WSL2 Ubuntu Restore Script"
restore_from_gdrive
To use the restore script:
- Save the script and make it executable:
chmod +x restore_wsl2_ubuntu.sh - Run it:
./restore_wsl2_ubuntu.sh
This script will list available backups, let you choose which one to restore, and then restore the files to your home directory.
Important Notes
- Always review scripts before running them, especially those that modify your files.
- The backup script excludes the
.cachedirectory to save space and time. - The restore script will overwrite files in your home directory. Make sure you understand this before confirming the restore.
- If you’ve made changes you want to keep since the backup, consider modifying the restore script to use
rclone copyinstead ofrclone sync. - It’s a good practice to test these scripts with a small amount of data first to ensure they work as expected in your environment.
- This cloud-based backup method is especially useful if you have limited local disk space or want an off-site copy of your data. It allows you to create backups without using local storage and provides easy access to your backups from any device with internet access.
By using these scripts, you can safely backup your WSL2 Ubuntu environment before performing an upgrade, and restore your files afterwards if needed. Remember to always keep multiple backups of important data!