131 lines
2.9 KiB
Bash
Executable file
131 lines
2.9 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
# Configuration
|
|
# Files will be named BASE_DIR/PREFIXEbasenameSUFFIXE
|
|
BASE_DIR="/var/backups/bdd"
|
|
MYSQL_EXTRA_ARGS=""
|
|
MYSQLDUMP_EXTRA_ARGS="--add-drop-table --routines --events"
|
|
PREFIXE="sauv_mysql_"
|
|
SUFFIXE=_$( date +%Y%m%d-%H%M%S ).sql.gz
|
|
NB_DAYS_BEFORE_DELETION="30"
|
|
BASES_EXCLUDED="information_schema
|
|
performance_schema"
|
|
|
|
# Small note for a backup of a random directory in crontab
|
|
# test -d /var/backups/www && cd /var/www && tar -czf /var/backups/www/www_$( date +\%Y\%m\%d-\%H\%M\%S ).tar.gz ./ && find /var/backups/www -maxdepth 1 -name 'www_*' -mtime +10 -delete
|
|
|
|
# Stop at first non-catched error
|
|
set -e
|
|
|
|
#
|
|
# Help function
|
|
#
|
|
usage() {
|
|
TMP_BASES_EXCLUDED="$( echo "$BASES_EXCLUDED" | tr '\n' " " )"
|
|
cat <<EOF
|
|
Usage :
|
|
$0 [ -x mysql_extra_arg ] [ -X mysqldump_extra_args ] [ -d nb_days_before_deletion ] [ -B base_dir ] [ -p prefix ] [ -z -e base_excluded ] [ base base...]
|
|
|
|
Example :
|
|
$0 -e mysql -e information_schema -e performance_schema -d 10
|
|
|
|
Default values :
|
|
mysql_extra_args: "$MYSQL_EXTRA_ARGS" (for ex.: "--defaults-extra-file=/var/backup/my.cnf" )
|
|
mysqldump_extra_args: "$MYSQLDUMP_EXTRA_ARGS" (for ex.: "--defaults-extra-file=/var/backup/my.cnf --add-drop-table" )
|
|
nb_days_before_deletion : "$NB_DAYS_BEFORE_DELETION" (if <0 deactivate)
|
|
base_dir : "$BASE_DIR"
|
|
prefix : "$PREFIXE"
|
|
base_excluded : "$TMP_BASES_EXCLUDED"
|
|
base base ... : by default, take every base shown by SHOW DATABASES
|
|
EOF
|
|
}
|
|
|
|
# Lower script priority
|
|
renice -n 20 $$ >/dev/null 2>&1
|
|
|
|
#
|
|
# Parameters management
|
|
#
|
|
while getopts hd:B:p:ze:x:X: f; do
|
|
case "$f" in
|
|
'h')
|
|
usage
|
|
exit
|
|
;;
|
|
|
|
'd')
|
|
NB_DAYS_BEFORE_DELETION="$OPTARG"
|
|
;;
|
|
|
|
'B')
|
|
BASE_DIR="$OPTARG"
|
|
;;
|
|
|
|
'p')
|
|
PREFIXE="$OPTARG"
|
|
;;
|
|
|
|
'z')
|
|
BASES_EXCLUDED=""
|
|
;;
|
|
|
|
'e')
|
|
if [ -z "$BASES_EXCLUDED" ]; then
|
|
BASES_EXCLUDED="$OPTARG"
|
|
else
|
|
BASES_EXCLUDED="$( printf "%s\n%s" "$BASES_EXCLUDED" "$OPTARG" )"
|
|
fi
|
|
;;
|
|
|
|
'x')
|
|
MYSQL_EXTRA_ARGS="$OPTARG"
|
|
;;
|
|
|
|
'X')
|
|
MYSQLDUMP_EXTRA_ARGS="$OPTARG"
|
|
;;
|
|
|
|
\?)
|
|
usage
|
|
exit 1
|
|
;;
|
|
|
|
esac
|
|
done
|
|
shift $( expr $OPTIND - 1 )
|
|
|
|
# Initial checks
|
|
if [ ! -d "$BASE_DIR" ]; then
|
|
echo "ERREUR : répertoire de sauvegarde inexistant : $BASE_DIR ." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$@" ]; then
|
|
# Listing des bases à sauvegarder
|
|
BASES="$( mysql $MYSQL_EXTRA_ARGS -Bse "SHOW DATABASES;" )"
|
|
else
|
|
BASES="$@"
|
|
fi
|
|
|
|
# Loop on bases' names
|
|
# We try to have a little fun here : MySQL can be quite laxist
|
|
# on base names, so we use <newline> as a separator. Quite a gymnastic
|
|
# ensue :)
|
|
printf "%s\n" "$BASES" | while read BASENAME; do
|
|
IFS="
|
|
"
|
|
for EXCLUDED_NAME in $BASES_EXCLUDED; do
|
|
if [ "$BASENAME" = "$EXCLUDED_NAME" ]; then
|
|
unset IFS
|
|
continue 2
|
|
fi
|
|
done
|
|
unset IFS
|
|
mysqldump $MYSQLDUMP_EXTRA_ARGS "$BASENAME" | gzip -c > "$BASE_DIR/$PREFIXE$BASENAME$SUFFIXE"
|
|
done
|
|
|
|
|
|
# Deletion of old backups
|
|
if [ "$NB_DAYS_BEFORE_DELETION" -gt "0" ]; then
|
|
find $BASE_DIR -maxdepth 1 -name "$PREFIXE*" -mtime +$NB_DAYS_BEFORE_DELETION -delete
|
|
fi
|