Created
December 27, 2024 15:44
-
-
Save roelven/cc6ff07a97f95cf48a23e9960d6d7aeb to your computer and use it in GitHub Desktop.
Bash script to migrate important stuff from your old mac to this one using SSH and rsync
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
# MacBook Migration Script | |
# | |
# A simple script to move over your important stuff. | |
# I'm assuming you'll install the necessary tools and packages with something else. | |
# | |
# Prepare your mac before running this: | |
# 1. Enable SSH access on your old machine | |
# 2. Go to System Settings > Privacy & Security > Full Disk Access | |
# and enable it for "sshd-keygen-wrapper" | |
# 3. On your new mac, download this script and make it executable by running | |
# `$ chmod +x migrate-mac.sh` | |
# | |
# Now run the migration with a `$ ./migrate-mac.sh` | |
# | |
#!/bin/bash | |
# Set variables | |
OLD_MAC_IP="YOUROLDMACIP" | |
NEW_MAC_USER="USERNAME" | |
BACKUP_DIR="/Users/$NEW_MAC_USER/OldMacBackup" | |
# Create backup directory on the new MacBook | |
mkdir -p "$BACKUP_DIR" | |
# 1. Copy important directories | |
echo "Copying important directories..." | |
rsync -avz -e ssh "$NEW_MAC_USER@$OLD_MAC_IP:/Users/$NEW_MAC_USER/{Documents,Desktop,Downloads,Pictures,Music,Movies}" "$BACKUP_DIR/" | |
# 2. Copy dotfiles and important hidden folders | |
echo "Copying dotfiles and important hidden folders..." | |
rsync -avz -e ssh "$NEW_MAC_USER@$OLD_MAC_IP:/Users/$NEW_MAC_USER/{.bash_profile,.bashrc,.zshrc,.gitconfig,.vimrc,.docker,.bash_history,.oh-my-zsh}" "$BACKUP_DIR/" | |
# 3. Copy SSH keys and config | |
echo "Copying SSH keys and config..." | |
rsync -avz -e ssh "$NEW_MAC_USER@$OLD_MAC_IP:/Users/$NEW_MAC_USER/.ssh" "$BACKUP_DIR/" | |
# 4. Copy development-related directories | |
echo "Copying development directories..." | |
rsync -avz -e ssh "$NEW_MAC_USER@$OLD_MAC_IP:/Users/$NEW_MAC_USER/{Projects,Code,Workspace,Development}" "$BACKUP_DIR/" | |
# 5. Set up SSH keys on the new MacBook | |
echo "Setting up SSH keys..." | |
mkdir -p ~/.ssh | |
cp "$BACKUP_DIR/.ssh/id_rsa"* ~/.ssh/ | |
chmod 600 ~/.ssh/id_rsa | |
chmod 644 ~/.ssh/id_rsa.pub | |
# 6. Copy dotfiles and important hidden folders to their proper locations | |
echo "Setting up dotfiles and important hidden folders..." | |
for item in .bash_profile .bashrc .zshrc .gitconfig .vimrc .docker .bash_history .oh-my-zsh; do | |
if [ -e "$BACKUP_DIR/$item" ]; then | |
cp -R "$BACKUP_DIR/$item" ~/"$item" | |
fi | |
done | |
echo "Migration complete! Please review the transferred files and configurations." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment