Files
mysql-bkup/src/mysql_bkup.sh

201 lines
5.4 KiB
Bash
Raw Normal View History

2023-12-23 22:21:15 +01:00
#!/usr/bin/env bash
2023-12-17 13:30:07 +01:00
set -e
TIME=$(date +%Y%m%d_%H%M%S)
MY_SQL_DUMP=/usr/bin/mysqldump
arg0=$(basename "$0" .sh)
blnk=$(echo "$arg0" | sed 's/./ /g')
export OPERATION=backup
2023-12-19 06:37:25 +01:00
export STORAGE=local
export STORAGE_PATH=/backup
export S3_PATH=/mysql-bkup
2023-12-17 19:26:42 +01:00
export TIMEOUT=60
2023-12-17 19:22:09 +01:00
export FILE_COMPRESION=true
2023-12-17 13:30:07 +01:00
usage_info()
{
echo "Usage: \\"
echo " $blnk Backup: mysql_bkup -o backup -d s3 \\"
echo " $blnk Restore: mysql_bkup -o restore -s s3 -f my_db.sql \\"
2023-12-19 06:37:25 +01:00
echo " $blnk [-o|--operation] [{-f|--file} ] [{-s|--storage} ] [{-h|--help} ] \\"
2023-12-17 13:30:07 +01:00
}
version_info()
{
2023-12-17 13:53:07 +01:00
echo "Version: $VERSION"
2023-12-17 13:30:07 +01:00
exit 0
}
usage()
{
exec 1>2 # Send standard output to standard error
usage_info
exit 0
}
error()
{
echo "$arg0: $*" >&2
exit 0
}
help()
{
echo
2023-12-17 20:30:26 +01:00
echo " -o |--operation -- Set operation (default: backup)"
2023-12-19 06:37:25 +01:00
echo " -s |--storage -- Set storage (default: local)"
echo " -f |--file -- Set file name "
2023-12-19 06:43:48 +01:00
echo " |--path -- Set s3 path, without file name"
2023-12-22 06:04:35 +01:00
echo " -d |--dbname -- Set database name "
2023-12-17 20:30:26 +01:00
echo " -p |--port -- Set database port (default: 3306)"
echo " -t |--timeout -- Set timeout (default: 120s)"
echo " -h |--help -- Print this help message and exit"
2023-12-19 06:37:25 +01:00
echo " -V |--version -- Print version information and exit"
2023-12-17 13:30:07 +01:00
exit 0
}
flags()
{
while test $# -gt 0
do
case "$1" in
(-o|--operation)
shift
[ $# = 0 ] && error "No operation specified - restore or backup"
export OPERATION="$1"
shift;;
2023-12-22 06:04:35 +01:00
(-d|--dbname)
2023-12-17 13:30:07 +01:00
shift
2023-12-22 06:04:35 +01:00
[ $# = 0 ] && error "No database name specified"
export DB_NAME="$1"
2023-12-17 13:30:07 +01:00
shift;;
2023-12-19 06:37:25 +01:00
(-s|--storage)
2023-12-17 13:30:07 +01:00
shift
2023-12-19 06:37:25 +01:00
[ $# = 0 ] && error "No storage specified - local or s3 | default local"
export STORAGE="$1"
2023-12-17 13:30:07 +01:00
shift;;
(-f|--file)
shift
[ $# = 0 ] && error "No file specified - file to restore"
export FILE_NAME="$1"
shift;;
2023-12-19 06:37:25 +01:00
(--path)
shift
[ $# = 0 ] && error "No s3 path specified - s3 path without file name"
export S3_PATH="$1"
shift;;
2023-12-17 19:22:09 +01:00
(-db|--database)
shift
[ $# = 0 ] && error "No database name specified"
2023-12-22 06:04:35 +01:00
export DB_NAME="$1"
2023-12-17 19:22:09 +01:00
shift;;
2023-12-17 19:24:25 +01:00
(-p|--port)
shift
[ $# = 0 ] && error "No database name specified"
export DB_PORT="$1"
shift;;
2023-12-17 13:30:07 +01:00
(-t|--timeout)
shift
[ $# = 0 ] && error "No timeout specified"
export TIMEOUT="$1"
shift;;
(-h|--help)
help;;
(-V|--version)
version_info;;
(--)
help;;
(*) usage;;
esac
done
}
backup()
{
2023-12-23 22:21:15 +01:00
if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then
echo "Please make sure all required environment variables are set "
2023-12-17 13:30:07 +01:00
else
2023-12-19 06:37:25 +01:00
## Test database connection
2023-12-22 06:04:35 +01:00
mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} -e"quit"
2023-12-19 06:37:25 +01:00
2023-12-17 13:30:07 +01:00
## Backup database
2023-12-22 06:04:35 +01:00
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} | gzip > ${STORAGE_PATH}/${DB_NAME}_${TIME}.sql.gz
2023-12-23 22:21:15 +01:00
echo "$TIME: ${DB_NAME}_${TIME}.sql.gz" | tee -a "${STORAGE_PATH}/history.txt"
2023-12-17 13:30:07 +01:00
echo "Database has been saved"
fi
2023-12-19 06:37:25 +01:00
exit 0
2023-12-17 13:30:07 +01:00
}
restore()
{
2023-12-23 22:21:15 +01:00
if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then
echo "Please make sure all required environment variables are set "
2023-12-17 13:30:07 +01:00
else
## Restore database
2023-12-19 06:37:25 +01:00
if [ -f "${STORAGE_PATH}/$FILE_NAME" ]; then
if gzip -t ${STORAGE_PATH}/$FILE_NAME; then
2023-12-22 06:04:35 +01:00
zcat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
2023-12-17 19:22:09 +01:00
else
2023-12-22 06:04:35 +01:00
cat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
2023-12-17 19:22:09 +01:00
fi
2023-12-17 13:30:07 +01:00
echo "Database has been restored"
else
2023-12-19 06:37:25 +01:00
echo "Error, file not found in ${STORAGE_PATH}/${FILE_NAME}"
2023-12-17 13:30:07 +01:00
fi
fi
exit
}
s3_backup()
{
mount_s3
backup
}
s3_restore()
{
mount_s3
restore
}
mount_s3()
{
2023-12-23 22:21:15 +01:00
if [[ -z $ACCESS_KEY ]] || [[ -z $SECRET_KEY ]]; then
2023-12-17 13:30:07 +01:00
echo "Please make sure all environment variables are set "
echo "BUCKETNAME=$BUCKETNAME \nACCESS_KEY=$nACCESS_KEY \nSECRET_KEY=$SECRET_KEY"
else
2023-12-17 19:22:09 +01:00
echo "$ACCESS_KEY:$SECRET_KEY" | tee /etc/passwd-s3fs
chmod 600 /etc/passwd-s3fs
echo "Mounting Object storage in /s3mnt .... "
if [ -z "$(ls -A /s3mnt)" ]; then
s3fs $BUCKETNAME /s3mnt -o passwd_file=/etc/passwd-s3fs -o use_cache=/tmp/s3cache -o allow_other -o url=$S3_ENDPOINT -o use_path_request_style
2023-12-19 06:37:25 +01:00
if [ ! -d "/s3mnt$S3_PATH" ]; then
mkdir -p /s3mnt$S3_PATH
fi
2023-12-17 19:22:09 +01:00
else
echo "Object storage already mounted in /s3mnt"
fi
2023-12-19 06:37:25 +01:00
export STORAGE_PATH=/s3mnt$S3_PATH
2023-12-17 13:30:07 +01:00
fi
}
flags "$@"
# ?
if [ $OPERATION != 'backup' ]
then
2023-12-19 06:37:25 +01:00
if [ $STORAGE != 's3' ]
2023-12-17 13:30:07 +01:00
then
echo "Restore from local"
restore
else
echo "Restore from s3"
s3_restore
fi
else
2023-12-19 06:37:25 +01:00
if [ $STORAGE != 's3' ]
2023-12-17 13:30:07 +01:00
then
echo "Backup to local destination"
backup
else
2023-12-19 06:37:25 +01:00
echo "Backup to s3 storage"
2023-12-17 13:30:07 +01:00
s3_backup
fi
fi