shell-base/setup/backup.sh

37 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
[[ ! $(groups | grep sudo) ]] && echo this script must be run by a user with sudo privileges && exit 1
[[ ! $BASH_SHELL_BASE ]] && BASH_SHELL_BASE="$(dirname "$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")"
echo Backng up to $BASH_SHELL_BASE/setup/backup
echo ok to continue?
echo "type \`yes\` fully or just <enter> for no"
read -e answer
[[ ! $answer = "yes" ]] && exit 1
set -o errexit
mkdir -p $BASH_SHELL_BASE/setup/backup/etc/profile.d
# mkdir -p $BASH_SHELL_BASE/setup/backup/user
mkdir -p $BASH_SHELL_BASE/setup/backup/root
# host system files in /etc
[[ -f /etc/profile ]] && cp -v /etc/profile $BASH_SHELL_BASE/setup/backup/etc
[[ -f /etc/bash.bashrc ]] && cp -v /etc/bash.bashrc $BASH_SHELL_BASE/setup/backup/etc
[[ -d /etc/profile.d ]] && cp -vR /etc/profile.d/ $BASH_SHELL_BASE/setup/backup/etc/
# current user files in $HOME
# [[ -f $HOME/.bash_profile ]] && cp -v $HOME/.bash_profile $BASH_SHELL_BASE/setup/backup/user
# [[ -f $HOME/.profile ]] && cp -v $HOME/.profile $BASH_SHELL_BASE/setup/backup/user
# [[ -f $HOME/.bashrc ]] && cp -v $HOME/.bashrc $BASH_SHELL_BASE/setup/backup/user
# [[ -f $HOME/.bash_logout ]] && cp -v $HOME/.bash_logout $BASH_SHELL_BASE/setup/backup/user
if [[ $(stat -c "%G" /root) = "sudo" ]]; then
[[ -f /root/.bash_profile ]] && cp -v /root/.bash_profile $BASH_SHELL_BASE/setup/backup/root
[[ -f /root/.profile ]] && cp -v /root/.profile $BASH_SHELL_BASE/setup/backup/root
[[ -f /root/.bashrc ]] && cp -v /root/.bashrc $BASH_SHELL_BASE/setup/backup/root
[[ -f /root/.bash_logout ]] && cp -v /root/.bash_logout $BASH_SHELL_BASE/setup/backup/root
else
echo no sudo group read access to /root so backing up /root as root
sudo -E -- bash -c '[[ ! -f /root/.profile ]] && exit 0; cp -v /root/.profile $BASH_SHELL_BASE/setup/backup/root'
sudo -E -- bash -c '[[ ! -f /root/.bashrc ]] && exit 0; cp -v /root/.bashrc $BASH_SHELL_BASE/setup/backup/root'
sudo -E -- bash -c '[[ ! -f /root/.bash_profile ]] && exit 0; cp -v /root/.bash_profile $BASH_SHELL_BASE/setup/backup/root'
sudo -E -- bash -c '[[ ! -f /root/.bash_logout ]] && exit 0; cp -v /root/.bash_logout $BASH_SHELL_BASE/setup/backup/root'
fi
set +o errexit
echo backup of system shell files complete!
exit 0