Skip to content

Instantly share code, notes, and snippets.

@martastain
Last active December 23, 2022 19:54
Show Gist options
  • Save martastain/0dc56acfbcf126ad1a1cb28ed3eab1b8 to your computer and use it in GitHub Desktop.
Save martastain/0dc56acfbcf126ad1a1cb28ed3eab1b8 to your computer and use it in GitHub Desktop.
Server backup script
#!/bin/bash
HOST="server.whatever.org"
USER="root"
BACKUP_LOCATIONS=(
"/etc/postfix"
"/etc/fail2ban"
"/etc/nginx"
"/etc/php5"
"/etc/letsencrypt"
"/etc/ssl"
"/opt"
"/var/www"
)
BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
function error_exit {
printf "\n\033[0;31mBackup failed\033[0m\n"
cd $BASEDIR
exit 1
}
function finished {
printf "\n\033[0;92mBackup completed\033[0m\n"
cd $BASEDIR
exit 0
}
for LOCATION in ${BACKUP_LOCATIONS[@]}; do
TARGET_DIR="${BASEDIR}${LOCATION}"
if [ ! -d "${TARGET_DIR}" ]; then
mkdir -p "${TARGET_DIR}"
fi
rsync -vrze ssh --delete ${USER}@${HOST}:${LOCATION} `dirname "${TARGET_DIR}"`|| error_exit
done
finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment