267 lines
10 KiB
Bash
Executable file
267 lines
10 KiB
Bash
Executable file
#!/bin/bash
|
|
############################################################
|
|
# Config Backup #
|
|
# #
|
|
# Backups my config files in my git repo. The user #
|
|
# specifies the directory of the git repo, the files to #
|
|
# backup, and the files to replace in this script. By #
|
|
# default the script may not be run as root. #
|
|
# #
|
|
# Change History #
|
|
# 03/11/2023 Out Of Ideas Original code. This is a #
|
|
# template for creating new #
|
|
# Bash shell script #
|
|
# Add new history entries as #
|
|
# needed. #
|
|
# #
|
|
# 27/11/2023 Out Of Odeas Initial version of the #
|
|
# program. Works as expected. #
|
|
# #
|
|
# 28/11/2023 Out Of Ideas Fix typo in description. #
|
|
# #
|
|
# 01/12/2023 Out Of Ideas Clean up orginization. #
|
|
# #
|
|
# 01/12/2023 Out Of Ideas Clean up formaitting. #
|
|
# #
|
|
# 01/12/2023 Out Of Ideas Remove extra comment. #
|
|
# #
|
|
# 03/12/2023 Out Of Ideas Add period to description. #
|
|
# #
|
|
# 03/12/2023 Out Of Ideas Remove extra space. #
|
|
# #
|
|
############################################################
|
|
############################################################
|
|
############################################################
|
|
# #
|
|
# This software is hereby released into the public domain. #
|
|
# #
|
|
# Anyone is free to copy, modify, publish, use, compile, #
|
|
# sell, or distribute this software, either in source code #
|
|
# form or as a compiled binary, for any purpose, with or #
|
|
# without modification. There are no restrictions or #
|
|
# requirements for the use of this software. #
|
|
# #
|
|
# This software is provided "as is," without any warranty, #
|
|
# express or implied, including but not limited to the #
|
|
# warranties of merchantability, fitness for a particular #
|
|
# purpose, or non-infringement. In no event shall the #
|
|
# authors be liable for any claim, damages, or other #
|
|
# liability, whether in an action of contract, tort, or #
|
|
# otherwise, arising from, out of, or in connection with #
|
|
# this software or the use or other dealings in this #
|
|
# software. #
|
|
# #
|
|
# To the fullest extent possible under law, the authors #
|
|
# have waived all copyright and patent rights to this #
|
|
# software. You are free to use this software without #
|
|
# any restrictions on copyright or patent rights. #
|
|
# #
|
|
# This Public Domain declaration, including the waiver of #
|
|
# copyright and patent rights, applies to the full extent #
|
|
# permitted by applicable laws and regulations. #
|
|
# #
|
|
############################################################
|
|
############################################################
|
|
############################################################
|
|
# Help #
|
|
############################################################
|
|
Help()
|
|
{
|
|
# Display Help
|
|
echo "Backups my config files in my git repo. The user specifies the directory of the git repo, the files to backup, and the files to replace in this script. By default the script may not be run as root."
|
|
echo
|
|
echo "Syntax: config-backup [-h|p|v|V]"
|
|
echo "options:"
|
|
echo "h Print this Help."
|
|
echo "p Print the Public Domain declaration."
|
|
echo "v Verbose mode."
|
|
echo "V Print version and exit."
|
|
echo
|
|
}
|
|
|
|
############################################################
|
|
# Public Domain Declaration #
|
|
############################################################
|
|
PUBLIC_DOMAIN_DECLARATION='
|
|
This software is hereby released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, with or without modification. There are no restrictions or requirements for the use of this software.
|
|
|
|
This software is provided "as is," without any warranty, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, or non-infringement. In no event shall the authors be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with this software or the use or other dealings in this software.
|
|
|
|
To the fullest extent possible under law, the authors have waived all copyright and patent rights to this software. You are free to use this software without any restrictions on copyright or patent rights.
|
|
|
|
This Public Domain declaration, including the waiver of
|
|
copyright and patent rights, applies to the full extent
|
|
permitted by applicable laws and regulations.
|
|
'
|
|
|
|
############################################################
|
|
# Version #
|
|
############################################################
|
|
VERSION="config-backup v1.2.3"
|
|
|
|
############################################################
|
|
# Check for root #
|
|
############################################################
|
|
CheckRoot()
|
|
{
|
|
# If we are not running as root, we exit the program.
|
|
if [ `id -u` != 0 ]
|
|
then
|
|
echo "ERROR: You must be root user to run this program"
|
|
exit
|
|
fi
|
|
}
|
|
|
|
############################################################
|
|
# Check for user #
|
|
############################################################
|
|
CheckUser()
|
|
{
|
|
# If we are running as root, we exit the program.
|
|
if [ `id -u` == 0 ]
|
|
then
|
|
echo "ERROR: You must NOT be root user to run this program"
|
|
exit
|
|
fi
|
|
}
|
|
|
|
############################################################
|
|
# Check for dependencies #
|
|
############################################################
|
|
|
|
CheckGit()
|
|
{
|
|
# If 'git' command is not found, we exit the program
|
|
if ! command -v git &> /dev/null; then
|
|
echo "Error: 'git' command not found. Please install git."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
############################################################
|
|
# Main Program #
|
|
############################################################
|
|
|
|
############################################################
|
|
# Sanity checks #
|
|
############################################################
|
|
# Are we running as root?
|
|
#CheckRoot
|
|
CheckUser
|
|
|
|
# Is 'git' comand found?
|
|
CheckGit
|
|
|
|
############################################################
|
|
# Initialize variables #
|
|
############################################################
|
|
DOTFILES_DIR="$HOME/Git/dot-files"
|
|
|
|
# List files and directories seperated by a space
|
|
CONFIG_FILES_TO_BACKUP=("$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.stumpwm.d" "$HOME/.config" "/etc/doas.conf" "/etc/portage")
|
|
|
|
# Files to update in $DOTFILES_DIR corrasponding to the items in the $CONFIG_FILES_TO_BACKUP array
|
|
# Order matters!
|
|
CONFIG_FILES_TO_REPLACE=("$DOTFILES_DIR/Gentoo/home/vehementham/.bashrc" "$DOTFILES_DIR/Gentoo/home/vehementham/.bash_profile" "$DOTFILES_DIR/Gentoo/home/vehementham/.stumpwm.d" "$DOTFILES_DIR/Gentoo/home/vehementham/.config" "$DOTFILES_DIR/Gentoo/etc/doas.conf" "$DOTFILES_DIR/Gentoo/etc/portage")
|
|
|
|
option=""
|
|
Msg=""
|
|
|
|
# If the number of config files to backup is inequal to the number of config files to replace, we exit the program.
|
|
if [ ${#CONFIG_FILES_TO_BACKUP[@]} != ${#CONFIG_FILES_TO_REPLACE[@]} ]
|
|
then
|
|
echo "ERROR: The number of config files to backup must be equal to the number of config files to replace."
|
|
exit
|
|
fi
|
|
|
|
# Unset variables
|
|
unset VERBOSE
|
|
|
|
############################################################
|
|
# Process the input options. Add options as needed. #
|
|
############################################################
|
|
# Get the options
|
|
while getopts ":hpvV" option;
|
|
do
|
|
case $option in
|
|
h) # display Help
|
|
Help
|
|
exit;;
|
|
p) # display $PUBLIC_DOMAIN_DECLARATION
|
|
echo $PUBLIC_DOMAIN_DECLARATION
|
|
exit;;
|
|
v) # enable $VERBOSE
|
|
VERBOSE=true;;
|
|
V) # display $VERSION
|
|
echo $VERSION
|
|
exit;;
|
|
\?) # incorrect option
|
|
echo "Error: Invalid option"
|
|
exit;;
|
|
esac
|
|
done
|
|
|
|
# Display a message before the program starts
|
|
#echo "$Msg"
|
|
|
|
# Display output if $VERBOSE is enabled
|
|
# Enter the $DOTFILES_DIR, and sync it
|
|
if [ $VERBOSE ]
|
|
then
|
|
|
|
echo "cd \"$DOTFILES_DIR\""
|
|
cd "$DOTFILES_DIR"
|
|
|
|
echo "git pull"
|
|
git pull
|
|
|
|
else
|
|
cd "$DOTFILES_DIR"
|
|
git pull
|
|
fi
|
|
|
|
# Get length of $CONFIG_FILES_TO_BACKUP array
|
|
len=${#CONFIG_FILES_TO_BACKUP[@]}
|
|
|
|
# Display output if $VERBOSE is enabled
|
|
# Replace the $CONFIG_FILES_TO_REPLACE with $CONFIG_FILES_TO_BACKUP
|
|
for (( i=0; i<$len; i++ ))
|
|
do
|
|
if [ $VERBOSE ]
|
|
then
|
|
|
|
echo "rm -rf \"${CONFIG_FILES_TO_REPLACE[$i]}\""
|
|
rm -rf "${CONFIG_FILES_TO_REPLACE[$i]}"
|
|
|
|
echo "cp -r \"${CONFIG_FILES_TO_BACKUP[$i]}\" \"${CONFIG_FILES_TO_REPLACE[$i]}\""
|
|
cp -r "${CONFIG_FILES_TO_BACKUP[$i]}" "${CONFIG_FILES_TO_REPLACE[$i]}"
|
|
|
|
else
|
|
rm -rf "${CONFIG_FILES_TO_REPLACE[$i]}"
|
|
cp -r "${CONFIG_FILES_TO_BACKUP[$i]}" "${CONFIG_FILES_TO_REPLACE[$i]}"
|
|
fi
|
|
done
|
|
|
|
# Display output if $VERBOSE is enabled
|
|
# Add, commit, and push
|
|
if [ $VERBOSE ]
|
|
then
|
|
|
|
echo "git add -A"
|
|
git add -A
|
|
|
|
read -p 'Commit message: ' MESSAGE
|
|
|
|
echo "git commit -m "$MESSAGE""
|
|
git commit -m "$MESSAGE"
|
|
|
|
echo "git push"
|
|
git push
|
|
|
|
else
|
|
git add -A
|
|
read -p 'Commit message: ' MESSAGE
|
|
git commit -m "$MESSAGE"
|
|
git push
|
|
fi
|
|
|