Introduction
This post is part of a series about automating data backups in Linux.
In this post we’ll construct a Bash script to automate the backup of the .bashrc file.
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.
Script code
backup_bashrc.sh
#!/bin/bash
# VARIABLES
bashrc_directory="/home/$USER/"
bashrc_filename=".bashrc"
backup_top_directory="/home/$USER/backups/"
backup_filename="backup_of_bashrc_$(date +%Y-%m-%d)"
# VARIABLES, constructed
bashrc_location=$bashrc_directory$bashrc_filename
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 ".bashrc backup script"
echo
echo "This script will copy $bashrc_location and output the file to $backup_output_directory"
echo
echo ===================== script info - END =====================
echo =============================================================
echo
echo
}
# FUNCTION
# Creates backup of .bashrc file.
create_backup_files() {
echo
echo "Backup started."
cp $bashrc_location $backup_output_directory
echo "Backup finished."
echo
}
# MAIN program flow
script_info
create_backup_files
Script description
Despite the script length, the core functionality is very simple. We are just running a cp
(copy) command and passing it the correct source and target variables (shown in the highlighted lines).
The source and target inputs are constructed in variables to provide a simple way to make future updates.
Everything else is output to the user for clarity.
Conclusion
That is all we need to backup the .bashrc via a Bash script.
The finalized script can be found here.