update mysql_backup and pg_backup scripts
This commit is contained in:
parent
f4b38e685a
commit
9cca36ecc0
2 changed files with 248 additions and 68 deletions
168
mysql_backup.sh
168
mysql_backup.sh
|
@ -1,6 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
SCRIPT_NAME=${0##*/}
|
||||
VER=0.1
|
||||
PROJECT="https://git.gresse.net/Ops/Backup"
|
||||
SCRIPT_NAME=mysql_backup.sh
|
||||
|
||||
SCRIPT_FILENAME=`basename "$0"`
|
||||
BACKUP_DIR=/tmp/backup
|
||||
|
||||
#LOGIN
|
||||
MYSQL_SERVER=localhost
|
||||
|
@ -18,8 +23,6 @@ TARGET_WEEKDAY=1
|
|||
# Ex : "|mydb001|mydb002"
|
||||
EGREP_EXCLUDE_DB=""
|
||||
|
||||
BACKUP_DIR=$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )
|
||||
|
||||
LATEST_TIMESTAMP=$(date +"%Y%m%d-%HH%M")
|
||||
DAILY_TIMESTAMP=$(date +"%Y%m%d")
|
||||
|
||||
|
@ -71,32 +74,27 @@ function backup() {
|
|||
echo_date "Backup finished" >>${LOG_FILE}
|
||||
}
|
||||
|
||||
function usage() {
|
||||
echo "./mysql_backup.sh"
|
||||
echo " -h --help : Show help"
|
||||
echo " -i : Install"
|
||||
echo " -l : List"
|
||||
echo " -u : Uninstall"
|
||||
echo ""
|
||||
}
|
||||
|
||||
function install() {
|
||||
echo Install ${SCRIPT_NAME} to ${BACKUP_DIR}
|
||||
echo "Starting Installation to directory '${BACKUP_DIR}'"
|
||||
if [ -e ${BACKUP_DIR} ] ; then
|
||||
_err "${BACKUP_DIR} already exists"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -n " -> Create directory ${BACKUP_DIR}/daily"
|
||||
mkdir -p ${BACKUP_DIR}/daily
|
||||
mkdir -p ${BACKUP_DIR}/data/daily
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Create directory ${BACKUP_DIR}/weekly"
|
||||
mkdir -p ${BACKUP_DIR}/weekly
|
||||
mkdir -p ${BACKUP_DIR}/data/weekly
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Create directory ${BACKUP_DIR}/monthly"
|
||||
mkdir -p ${BACKUP_DIR}/monthly
|
||||
mkdir -p ${BACKUP_DIR}/data/monthly
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Create directory ${BACKUP_DIR}/latest"
|
||||
mkdir -p ${BACKUP_DIR}/latest
|
||||
mkdir -p ${BACKUP_DIR}/data/latest
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Create directory ${BACKUP_DIR}/logs"
|
||||
|
@ -107,6 +105,19 @@ function install() {
|
|||
mkdir -p ${BACKUP_DIR}/conf
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Install script to ${BACKUP_DIR}"
|
||||
cp ${SCRIPT_FILENAME} ${BACKUP_DIR}/${SCRIPT_NAME}
|
||||
chmod +x ${BACKUP_DIR}/${SCRIPT_NAME}
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Change Backup Directory in installed script"
|
||||
sed -i -r "s!^BACKUP_DIR=.*!BACKUP_DIR=$BACKUP_DIR!" ${BACKUP_DIR}/${SCRIPT_NAME}
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Install Crontab"
|
||||
crontab -l | { cat; echo "30 22 * * * ${BACKUP_DIR}/${SCRIPT_NAME} -r >/dev/null 2>&1"; } | crontab -
|
||||
echo ": Done"
|
||||
|
||||
echo " -> Install Finished :)"
|
||||
}
|
||||
|
||||
|
@ -127,33 +138,16 @@ function list() {
|
|||
}
|
||||
|
||||
function uninstall() {
|
||||
read -r -p "Are you sure to uninstall? [y/N]" confirm_uninstall
|
||||
read -r -p "Are you sure to uninstall ${BACKUP_DIR}? [y/N]" confirm_uninstall
|
||||
case ${confirm_uninstall} in
|
||||
[yY])
|
||||
echo "=== === === Uninstall all === === ==="
|
||||
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}/daily"
|
||||
rm -Rf ${BACKUP_DIR}/daily
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}"
|
||||
rm -Rf ${BACKUP_DIR}
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}/weekly"
|
||||
rm -Rf ${BACKUP_DIR}/weekly
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}/monthly"
|
||||
rm -Rf ${BACKUP_DIR}/monthly
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}/latest"
|
||||
rm -Rf ${BACKUP_DIR}/latest
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}/logs"
|
||||
rm -Rf ${BACKUP_DIR}/logs
|
||||
echo ": Done"
|
||||
|
||||
echo -n " -> Remove directory ${BACKUP_DIR}/conf"
|
||||
rm -Rf ${BACKUP_DIR}/conf
|
||||
echo -n " -> Remove crontab (TODO)"
|
||||
echo ": Done"
|
||||
|
||||
echo " -> Uninstall Finished :)"
|
||||
|
@ -165,36 +159,76 @@ function uninstall() {
|
|||
esac
|
||||
}
|
||||
|
||||
function echo_date() {
|
||||
date +"[%Y%m%d-%H:%M:%S] ${1}"
|
||||
function _info() {
|
||||
date +"[INFO] ${1}"
|
||||
}
|
||||
|
||||
if [ "$#" -ge 1 ]; then
|
||||
case $1 in
|
||||
-h | --help)
|
||||
usage
|
||||
exit
|
||||
function _err(){
|
||||
echo "[ERROR] ${1}"
|
||||
}
|
||||
|
||||
function _process(){
|
||||
while [ ${#} -gt 0 ]; do
|
||||
case "${1}" in
|
||||
--help | -h)
|
||||
showhelp
|
||||
return
|
||||
;;
|
||||
--version | -v)
|
||||
version
|
||||
return
|
||||
;;
|
||||
--install | -i)
|
||||
if [ -n "${2}" ]; then
|
||||
BACKUP_DIR=${2}
|
||||
fi
|
||||
install
|
||||
return
|
||||
;;
|
||||
--uninstall | -u)
|
||||
if [ -n "${2}" ]; then
|
||||
BACKUP_DIR=${2}
|
||||
fi
|
||||
uninstall
|
||||
return
|
||||
;;
|
||||
--list | -l)
|
||||
list
|
||||
return
|
||||
;;
|
||||
--run | -r)
|
||||
backup
|
||||
return
|
||||
;;
|
||||
*)
|
||||
echo "Unknown parameter : ${1}"
|
||||
return 1
|
||||
;;
|
||||
-i)
|
||||
install
|
||||
exit
|
||||
;;
|
||||
-u)
|
||||
uninstall
|
||||
exit
|
||||
;;
|
||||
-l)
|
||||
list
|
||||
exit
|
||||
;;
|
||||
*)
|
||||
echo "ERROR: unknown parameter \"$PARAM\""
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
else
|
||||
backup
|
||||
exit
|
||||
esac
|
||||
shift
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
version() {
|
||||
echo "${SCRIPT_NAME} (${PROJECT}) - Version ${VER}"
|
||||
}
|
||||
|
||||
function showhelp() {
|
||||
echo "Usage: mysql_backup.sh [commands]
|
||||
Commands:
|
||||
-h, --help Show this help message.
|
||||
-v, --version Show version info.
|
||||
-i, --install <directory> Install to the specific direcotry.
|
||||
-u, --uninstall <directory> Uninstall to the specific direcotry.
|
||||
-l, --list List backup
|
||||
-r, --run Run backup of all databases
|
||||
"
|
||||
}
|
||||
|
||||
function main() {
|
||||
[ -z "$1" ] && showhelp && return
|
||||
_process "$@"
|
||||
}
|
||||
|
||||
main "$@"
|
146
pg_backup.sh
Executable file
146
pg_backup.sh
Executable file
|
@ -0,0 +1,146 @@
|
|||
#!/usr/bin/env sh
|
||||
#
|
||||
# Backup a Postgresql database into a daily file.
|
||||
#!/usr/bin/env bash
|
||||
|
||||
VER=0.1
|
||||
SCRIPT_NAME="backup.sh"
|
||||
PROJECT="https://github.com/PierNola/BackupPg/"
|
||||
|
||||
SCRIPT_FILENAME=`basename "$0"`
|
||||
BACKUP_DIR=/tmp/backup
|
||||
DATA_DIR=${BACKUP_DIR}/data
|
||||
ARCH_DIR=${BACKUP_DIR}/archives
|
||||
LOG_DIR=${BACKUP_DIR}/log
|
||||
|
||||
DAY_TO_KEEP_DUMP=14
|
||||
DAY_TO_KEEP_LOG=14
|
||||
|
||||
DATE=$(date +"%Y%m%d-%H%M")
|
||||
LOG_FILE=${LOG_DIR}/${DATE}-backup.log
|
||||
|
||||
backup() {
|
||||
_date_echo "Starting PostgreSQL backup" >${LOG_FILE}
|
||||
|
||||
DATABASES=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'` 2>>${LOG_FILE}
|
||||
for DB in ${DATABASES}; do
|
||||
GZ_DMP_FILE=${DATA_DIR}/${DB}.dmp.gz
|
||||
ARCH_GZ_DMP_FILE=${ARCH_DIR}/${DATE}-${DB}.dmp.gz
|
||||
|
||||
_date_echo "Starting backup of database '${DB}'" >>${LOG_FILE}
|
||||
pg_dump -Upostgres -v ${DB} -F p 2>>${LOG_FILE} | gzip >${GZ_DMP_FILE}
|
||||
|
||||
_date_echo "Creating backup archives of database '${DB}'" >>${LOG_FILE}
|
||||
cp ${GZ_DMP_FILE} ${ARCH_GZ_DMP_FILE}
|
||||
done
|
||||
|
||||
_date_echo "Prune all archives backup older then ${DAY_TO_KEEP_DUMP} days" >>${LOG_FILE}
|
||||
find ${ARCH_DIR} -type f -maxdepth 1 -mtime +${DAY_TO_KEEP_DUMP} -name "*.gz" -delete 2>&1 >>${LOG_FILE}
|
||||
|
||||
_date_echo "End PostgreSQL backup" >>${LOG_FILE}
|
||||
|
||||
# prune old logs
|
||||
find ${LOG_DIR} -type f -maxdepth 1 -mtime +${DAY_TO_KEEP_LOG} -name "*.log" -delete
|
||||
}
|
||||
|
||||
_vacuum_db() {
|
||||
if [ "`date +%d`" = "17" ]; then
|
||||
echo "`date` : vacuum full analyze"
|
||||
/usr/bin/psql $dbname -c "vacuum full analyze;"
|
||||
else
|
||||
echo "`date` : vacuum"
|
||||
/usr/bin/psql $dbname -c "vacuum;"
|
||||
fi
|
||||
}
|
||||
|
||||
_reindex_db() {
|
||||
if [ "`date +%u`" = "7" ]; then
|
||||
/usr/bin/reindexdb -a
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
install() {
|
||||
_date_echo "Starting Installation to directory '${BACKUP_DIR}'"
|
||||
if [ -e ${BACKUP_DIR} ] ; then
|
||||
_err "${BACKUP_DIR} already exists"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create all directories
|
||||
mkdir -p ${BACKUP_DIR}
|
||||
mkdir ${BACKUP_DIR}/data
|
||||
mkdir ${BACKUP_DIR}/log
|
||||
mkdir ${BACKUP_DIR}/archives
|
||||
|
||||
# Install script to backup directory
|
||||
cp ${SCRIPT_FILENAME} ${BACKUP_DIR}/${SCRIPT_NAME}
|
||||
chmod +x ${BACKUP_DIR}/${SCRIPT_NAME}
|
||||
|
||||
# Change Backup Directory in installed script
|
||||
sed -i -r "s!^BACKUP_DIR=.*!BACKUP_DIR=$BACKUP_DIR!" ${BACKUP_DIR}/${SCRIPT_NAME}
|
||||
|
||||
# Install Cron Job
|
||||
crontab -l | { cat; echo "30 22 * * * ${BACKUP_DIR}/${SCRIPT_NAME} -r >/dev/null 2>&1"; } | crontab -
|
||||
}
|
||||
|
||||
_date_echo() {
|
||||
date +"[%Y-%m-%d %H:%M] ${1}"
|
||||
}
|
||||
|
||||
_err(){
|
||||
echo "[ERROR] ${1}"
|
||||
}
|
||||
|
||||
_process(){
|
||||
while [ ${#} -gt 0 ]; do
|
||||
case "${1}" in
|
||||
--help | -h)
|
||||
showhelp
|
||||
return
|
||||
;;
|
||||
--version | -v)
|
||||
version
|
||||
return
|
||||
;;
|
||||
--install | -i)
|
||||
if [ -n "${2}" ]; then
|
||||
BACKUP_DIR=${2}
|
||||
fi
|
||||
install
|
||||
return
|
||||
;;
|
||||
--run | -r)
|
||||
backup
|
||||
return
|
||||
;;
|
||||
*)
|
||||
echo "Unknown parameter : ${1}"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
}
|
||||
|
||||
version() {
|
||||
echo "${PROJECT} - Version ${VER}"
|
||||
}
|
||||
|
||||
showhelp() {
|
||||
version
|
||||
echo "Usage: $SCRIPT_NAME [commands]
|
||||
Commands:
|
||||
-h, --help Show this help message.
|
||||
-v, --version Show version info.
|
||||
-i, --install <directory> Install to the specific direcotry.
|
||||
-r, --run Run backup of all databases
|
||||
"
|
||||
}
|
||||
|
||||
main() {
|
||||
[ -z "$1" ] && showhelp && return
|
||||
_process "$@"
|
||||
}
|
||||
|
||||
main "$@"
|
Loading…
Reference in a new issue