Introduction
This post is part of a series about automating data backups in Linux.
In the previous post we automated the process to backup a .bashrc file with a Bash script.
In this post we’ll automate the process of backing up Visual Studio Code (VS Code) settings and a list of installed extensions with a Bash script.
The Script
We’ve developed Bash scripts for automating tasks in previous posts, so we’ll leverage that work and modify it for our current purposes.
Specifically, we’ll start with the script developed for backing up the .bashrc file since it’s requirements are so similar.
Also, there are several published approaches for this task - for instance, this StackOverflow question and Elliot DeNolf’s post - but the most comprehensive solution we identified was found in Dominik Zarsky’s post and script. We’ll incorporate portions of that logic for the task.
Script code
backup_vscode.sh
#!/bin/bash
########################################################
## VS Code Settings and Extensions backup/sync script
## Based on a script by Loupeznik (https://github.com/Loupeznik), which can be found at https://github.com/Loupeznik/utils/blob/master/backup_utils/vscode-backup.sh
## Script prints all installed VS Code extensions into a txt file and backs up settings and keybindings config files.
########################################################
# VARIABLES
vscode_settings_directory="/home/$USER/.config/Code/User"
backup_top_directory="/home/$USER/backups/"
backup_instance_directory="vscode-configuration-$(date +%Y-%m-%d)"
backup_filename_for_settings="vscode-settings-archive"
backup_filename_for_extensions="vscode-extensions-list.txt"
# VARIABLES, constructed
backup_output_directory=$backup_top_directory$backup_instance_directory
# FUNCTION
# Provides contextual output
script_info() {
echo
echo
echo
echo
echo =============================================================
echo ==================== script info - START ====================
echo
echo "Executing $BASH_SOURCE"
echo
echo "VS Code Settings and Extensions backup script"
echo
echo "This script will output backup files to $backup_output_directory"
echo
echo ===================== script info - END =====================
echo =============================================================
echo
echo
}
# FUNCTION
# Tests for existing backup directory. Prompts user if existing directory found. Otherwise creates new backup directory.
create_backup_instance_directory() {
echo "[GENERAL] Setting up output folder."
if [ -d $backup_output_directory ]; then
echo
echo "[GENERAL] The directory $backup_output_directory already exists."
echo "[GENERAL] DO YOU WISH TO PERMANENTLY DELETE IT? - Press y to continue, any other key to exit."
echo
while : ; do
read -n 1 k <&1
if [[ $k = y ]]; then
echo ""
echo "[GENERAL] Deleting the existing $backup_output_directory and creating a new directory with that name."
rm -rf $backup_output_directory
break
else
echo
echo "[GENERAL] Exiting"
echo
exit 0
fi
done
fi
mkdir $backup_output_directory
}
# FUNCTION
# Creates backups of Visual Studio Code (VS Code) settings files.
backup_settings_files() {
echo
echo "[SETTINGS] Backing up your config files."
if [ ! -f $vscode_settings_directory/keybindings.json ]; then
echo "[SETTINGS] Backing up settings.json file."
echo "[SETTINGS] Keybindings config file not found."
tar -czf $backup_output_directory/$backup_filename_for_settings.tar.gz -C $vscode_settings_directory settings.json
else
echo "[SETTINGS] Backing up settings.json file."
echo "[SETTINGS] Backing up keybindings.json file."
tar -czf $backup_output_directory/$backup_filename_for_settings.tar.gz -C $vscode_settings_directory settings.json keybindings.json
fi
}
# FUNCTION
# Creates a list of installed Visual Studio Code (VS Code) extensions.
create_list_of_extensions() {
echo
echo "[EXTENSIONS] Exporting your extensions."
code --list-extensions | xargs -L 1 echo code --install-extension > $backup_output_directory/$backup_filename_for_extensions
echo
echo "[GENERAL] Backup complete."
echo
}
# MAIN program flow
script_info
create_backup_instance_directory
backup_settings_files
create_list_of_extensions
Script description
The VS Code settings location and output path are constructed in variables to provide a simple way to make future updates.
vscode_settings_directory="/home/$USER/.config/Code/User"
- Defines a variable that points to the directory that contains VS Code settings files.
backup_top_directory="/home/$USER/backups/"
- Defines a variable that points to the directory where we want to output the backup file.
backup_instance_directory="vscode-configuration-$(date +%Y-%m-%d)"
- Defines a variable that specifies the name of the backup directory. In this case, the literal string vscode-configuration, followed by the current date.
backup_filename_for_settings="vscode-settings-archive"
- Defines a variable that specifies the name of the settings backup archive.
backup_filename_for_extensions="vscode-extensions-list.txt"
- Defines a variable that specifies the name of the extensions list backup file.
backup_output_directory=$backup_top_directory$backup_instance_directory
- Defines a variable for the output filepath and directory name.
The backup process is separated into three separate functions that handle distinct portions of the backup process.
create_backup_instance_directory() {...}
- Tests for the existence of a subdirectory with the specified backup name within the specified directory. If it finds a match it prompts the user, asking if the folder should be deleted and replaced. If the user types ‘n’ the script exits. If the user types ‘y’ the existing subdirectory is deleted. Finally, a subdirectory is created with the specified name.
backup_settings_files() {...}
- Tests for the existence of a keybindings.json file in the specified directory. If the keybindings file is not found it copies and zips the settings.json file in that directory to the specified output directory.. If a keybindings file is found, it copies and zips both files and backs them up to the specified output directory.
code --list-extensions | xargs -L 1 echo code --install-extension > $backup_output_directory/code-extensions-install
- Lists the installed VS Code extensions, appends the literal string code –install-extension, and outputs the text to the specified output file.
Everything else in the script is output to the user for clarity.
Conclusion
This script backups both VS Code settings and a list of installed extensions.
The finalized script can be found here.