Last active
December 23, 2022 19:54
-
-
Save martastain/0dc56acfbcf126ad1a1cb28ed3eab1b8 to your computer and use it in GitHub Desktop.
Server backup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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