Skip to content

Instantly share code, notes, and snippets.

@abiiranathan
Created April 26, 2023 12:14
Show Gist options
  • Save abiiranathan/f38c56f797ab21e83beac7693d4b9fa4 to your computer and use it in GitHub Desktop.
Save abiiranathan/f38c56f797ab21e83beac7693d4b9fa4 to your computer and use it in GitHub Desktop.
Backup dotfiles, ssh keys, gnupg keys, zsh config, Vscode Settings. Restore with a single command.
#!/bin/bash
# Define the backup directory
BACKUP_DIR="$HOME/dotfile-backup"
get_vscode_settings_path() {
case "$OSTYPE" in
linux-gnu*)
# Linux
if [ -n "$WSL_DISTRO_NAME" ]; then
# Running in WSL
read -p "Please enter your Windows username: " USER_HOME
# Trim leading and trailing whitespace from the input string
USER_HOME="${USER_HOME#"${USER_HOME%%[![:space:]]*}"}"
USER_HOME="${USER_HOME%"${USER_HOME##*[![:space:]]}"}"
if [ -z "$USER_HOME" ]; then
echo "Error: username is empty." >&2
return 1
fi
echo "/mnt/c/Users/$USER_HOME/AppData/Roaming/Code/User/settings.json"
else
echo "$HOME/.config/Code/User/settings.json"
fi
;;
darwin*)
# macOS
echo "$HOME/Library/Application Support/Code/User/settings.json"
;;
msys*)
# Windows
echo "$APPDATA/Code/User/settings.json"
;;
*)
# Unsupported operating system
echo "Error: unsupported operating system." >&2
return 1
;;
esac
}
echo "Getting VSCode settings file path..."
vscode_settings_path="$(get_vscode_settings_path)"
if [ $? -ne 0 ]; then
echo "Failed to get VSCode settings file path." >&2
fi
# Get the directory name of the settings.json file
vscode_settings_dir=$(dirname "$vscode_settings_path")
install_vscode(){
# Add the Microsoft GPG key to your system
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
# Add the Visual Studio Code repository to your system
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
# Update your package list
sudo apt update -y
# Install Visual Studio Code
sudo apt install code
}
backup(){
# Remove existing backup dir
rm -rf "$BACKUP_DIR"
mkdir "$BACKUP_DIR"
# Create the backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
mkdir "$BACKUP_DIR"
fi
# Backup Git credentials
cp "$HOME/.gitconfig" "$BACKUP_DIR"
cp "$HOME/.git-credentials" "$BACKUP_DIR"
cp -r "$HOME/.ssh" "$BACKUP_DIR"
if [ -f "$vscode_settings_path" ]; then
cp "$vscode_settings_path" "$BACKUP_DIR"
# Backup VS Code extensions
code --list-extensions > "$BACKUP_DIR/vscode-extensions.txt"
fi
# Backup GnuPG keys
cp -r "$HOME/.gnupg" "$BACKUP_DIR"
# Backup Bash and Zsh settings
cp "$HOME/.bashrc" "$BACKUP_DIR"
cp "$HOME/.zshrc" "$BACKUP_DIR"
cp "$HOME/.zsh_history" "$BACKUP_DIR"
cp "$HOME/.profile" "$BACKUP_DIR"
# Backup Oh My Zsh directory
cp -r "$HOME/.oh-my-zsh" "$BACKUP_DIR"
}
# Define the restore function
restore() {
# Install zsh
sudo apt-get install zsh -y
# Restore Git credentials
cp "$BACKUP_DIR/.gitconfig" "$HOME"
cp "$BACKUP_DIR/.git-credentials" "$HOME"
cp -r "$BACKUP_DIR/.ssh" "$HOME"
# Restore GnuPG keys
cp -r "$BACKUP_DIR/.gnupg" "$HOME"
# Restore Bash and Zsh settings
cp "$BACKUP_DIR/.bashrc" "$HOME"
cp "$BACKUP_DIR/.zshrc" "$HOME"
cp "$BACKUP_DIR/.zsh_history" "$HOME"
cp "$BACKUP_DIR/.profile" "$HOME"
# Restore Oh My Zsh directory
cp -r "$BACKUP_DIR/.oh-my-zsh" "$HOME"
# Install Vscode if it does not exist
if [ -x "$(command -v code)" ]; then
echo "Visual Studio Code is installed"
else
install_vscode
fi
if [ -f "$BACKUP_DIR/settings.json" ]; then
# Restore VS Code settings
cp "$BACKUP_DIR/settings.json" "$vscode_settings_dir"
# Install VS Code extensions
while read extension; do
code --install-extension "$extension"
done < "$BACKUP_DIR/vscode-extensions.txt"
fi
# Restart zsh terminal
exec zsh
}
# If the first argument is "backup", run the backup function
if [ "$1" == "backup" ]; then
echo "Backing up dotfiles and secrets to $BACKUP_DIR..."
backup
echo "Done!"
# If the first argument is "restore", run the restore function
elif [ "$1" == "restore" ]; then
echo "Restoring dotfiles and secrets from $BACKUP_DIR..."
restore
echo "Done!"
# If the first argument is neither "backup" nor "restore", print an error message
else
echo "Error: Invalid argument. Usage: dotfile.sh [backup|restore]"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment