72 lines
1.5 KiB
Bash
72 lines
1.5 KiB
Bash
#!/bin/bash
|
|
|
|
# This script is used to create a database dump from a Mydumper multipart dump
|
|
# Usage: ./mydumploader.sh -d <directory> -b <database> [-o <output file>] [-c]
|
|
|
|
while getopts "chd:b:o:" opt; do
|
|
case $opt in
|
|
d) dir=$OPTARG;;
|
|
b) db=$OPTARG;;
|
|
o) out=$OPTARG;;
|
|
c) compress=1;;
|
|
h) echo "Usage: $0 -d <directory> -b <database> [-o <output file>] [-c]"; exit 0;;
|
|
\?) echo "Invalid option: -$OPTARG"; exit 1;;
|
|
esac
|
|
done
|
|
|
|
# Check if the directory exists
|
|
if [ ! -d "$dir" ]; then
|
|
echo "Directory $dir does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
# Default output file
|
|
if [ -z "$out" ]; then
|
|
out="$db.sql"
|
|
fi
|
|
|
|
# Check the filetype and compression type
|
|
type=$(ls $dir/$db-schema-create.sql* | awk -F. '{print $NF}')
|
|
|
|
case $type in
|
|
sql)
|
|
reader="cat"
|
|
;;
|
|
gz)
|
|
reader="zcat"
|
|
;;
|
|
bz2)
|
|
reader="bzcat"
|
|
;;
|
|
xz)
|
|
reader="xzcat"
|
|
;;
|
|
*)
|
|
echo "Unsupported filetype."
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
echo "Dumping database: $db"
|
|
|
|
# Create output file
|
|
echo "CREATE DATABASE IF NOT EXISTS $db;" > $out
|
|
echo "USE $db;" >> $out
|
|
|
|
# Dump schema
|
|
for file in $(ls $dir | grep $db | grep 'schema' | grep -v 'create'); do
|
|
${reader} $dir/$file >> $out
|
|
done
|
|
|
|
# Dump data
|
|
for file in $(ls $dir | grep $db | grep -v 'schema'); do
|
|
${reader} $dir/$file >> $out
|
|
done
|
|
|
|
# Compress output if requested
|
|
if [ $compress ]; then
|
|
gzip $out
|
|
out="${out}.gz"
|
|
fi
|
|
|
|
echo "Database dump created: $out" |