Introduction
This post is part of a series about automating data backups in Linux.
In the previous post we automated the process to backup Visual Studio Code (VS Code) settings and a list of installed extensions.
In this post we’ll construct a Bash script to automate the backup of global git config values.
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 started with the script developed for backing up the .bashrc file since it’s requirements are so similar. We then modified it for this task.
Script code
backup_git_config.sh
#!/bin/bash
# VARIABLES
inquiry_top_directory="/home/$USER/"
git_config_command="git config --global --list"
backup_top_directory="/home/$USER/backups/"
backup_filename="backup_of_global_git_configs_$(date +%Y-%m-%d).txt"
# VARIABLES, constructed
backup_output_directory=$backup_top_directory$backup_filename
# FUNCTION
# Provides contextual output
script_info() {
echo
echo
echo
echo
echo =============================================================
echo ==================== script info - START ====================
echo
echo "Executing $BASH_SOURCE"
echo
echo "git config backup script"
echo
echo "This script will backup the user's global git config values to the file:"
echo "$backup_output_directory"
echo
echo ===================== script info - END =====================
echo =============================================================
echo
echo
}
# FUNCTION
# Creates a backup of the user's global git config values
create_backup_files() {
cd $inquiry_top_directory
echo
echo "Backup started."
echo
#Display the results
${git_config_command}
#Output the results to a file
${git_config_command} > $backup_output_directory
echo
echo "Backup finished."
echo
}
# MAIN program flow
script_info
create_backup_files
Script description
As with previous backup scripts, despite the script length, the core functionality is very simple. We are running a single git command and saving the results to a file. (The relevant sections are highlighted.)
git_config_command="git config --global --list"
- Defines a variable that holds the relevant git command, git config –global –list, which instructs git to list all global config settings.
${git_config_command}
- The saved command is run and the results are first output to the screen.
${git_config_command} > $backup_output_directory
- The saved command is run again and the results are saved to a file.
Everything else is output to the user for clarity.
Conclusion
That is all we need to backup global git config values via a Bash script.
The finalized script can be found here.