shell-base/setup/backup.sh

32 lines
2.0 KiB
Bash
Raw Normal View History

#!/bin/bash
[[ ! $(groups | grep sudo) ]] && echo this script must be run by a user with sudo privileges && exit 1
[[ ! $BASH_SHELL_BASE ]] && echo "set BASH_SHELL_BASE before running this script" && exit 2 || echo backing up shell files...
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