Created
May 13, 2012 17:10
-
-
Save jeeb/2689320 to your computer and use it in GitHub Desktop.
Random backup script for various files on a server.
This file contains hidden or 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 | |
USER_OUTPUT_FOLDER="/home/hogehoge/backups/" | |
USER_WEBROOT="/var/www/" | |
USER_APACHE="/etc/apache2/" | |
USER_MUNIN="/etc/munin/" | |
USER_PHP="/etc/php5/" | |
DATE_PREFIX=`date +"%Y-%m-%d_%H-%M_"` | |
TAR_SETTINGS="-cvpjf" | |
WEBROOT="webroot.tar.bz2" | |
APACHE="apache.tar.bz2" | |
MUNIN="munin.tar.bz2" | |
PHP="php.tar.bz2" | |
WEBROOT_OUT="${USER_OUTPUT_FOLDER}${DATE_PREFIX}${WEBROOT}" | |
APACHE_OUT="${USER_OUTPUT_FOLDER}${DATE_PREFIX}${APACHE}" | |
MUNIN_OUT="${USER_OUTPUT_FOLDER}${DATE_PREFIX}${MUNIN}" | |
PHP_OUT="${USER_OUTPUT_FOLDER}${DATE_PREFIX}${PHP}" | |
LOGFILE="${USER_OUTPUT_FOLDER}${DATE_PREFIX}log.txt" | |
COL_R='\E[31m' | |
COL_G='\E[32m' | |
COL_Y='\E[33m' | |
COL_RESET='\E[0m' | |
info() | |
{ | |
message=${1} | |
echo -e "[${COL_G}INFO${COL_RESET}] ${message}" | |
return | |
} | |
warning() | |
{ | |
message=${1} | |
echo -e "[${COL_Y}WARNING${COL_RESET}] ${message}" | |
return | |
} | |
error() | |
{ | |
message=${1} | |
echo -e "[${COL_R}ERROR${COL_RESET}] ${message}" | |
return | |
} | |
check_output_folder() | |
{ | |
if [ ! -d "${USER_OUTPUT_FOLDER}" ]; then | |
error "Output folder ${USER_OUTPUT_FOLDER} not found!" | |
exit 1 | |
else | |
info "Output folder ${USER_OUTPUT_FOLDER} found." | |
return | |
fi | |
} | |
check_if_file_exists() | |
{ | |
file=${1} | |
if [ -f "${1}" ]; then | |
error "Output file ${file} already exists!" | |
exit 1 | |
else | |
info "Output file ${file} does not already exist." | |
return | |
fi | |
} | |
backup() | |
{ | |
file=${1} | |
out=${2} | |
tar "${TAR_SETTINGS}" "${out}" "${file}" 2>&1 >> "${LOGFILE}" | |
RETVAL=$? | |
if [ ! "${RETVAL}" -eq 0 ]; then | |
error "Tarring of ${file} failed!" | |
exit 1 | |
else | |
info "Tarring of ${file} succeeded." | |
return | |
fi | |
} | |
info "Starting backup script." | |
info "Checking stuff..." | |
check_output_folder | |
echo "" | |
info "Attempting to back up webroot." | |
check_if_file_exists "${WEBROOT_OUT}" | |
backup "${USER_WEBROOT}" "${WEBROOT_OUT}" | |
echo "" | |
info "Attempting to back up Apache settings." | |
check_if_file_exists "${APACHE_OUT}" | |
backup "${USER_APACHE}" "${APACHE_OUT}" | |
echo "" | |
info "Attempting to back up Munin settings." | |
check_if_file_exists "${MUNIN_OUT}" | |
backup "${USER_MUNIN}" "${MUNIN_OUT}" | |
echo "" | |
info "Attempting to back up PHP settings." | |
check_if_file_exists "${PHP_OUT}" | |
backup "${USER_PHP}" "${PHP_OUT}" | |
echo "" | |
echo "" | |
info "Script finished successfully, all OK." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment