Skip to content

Instantly share code, notes, and snippets.

@beevk
Created March 14, 2024 18:02
Show Gist options
  • Save beevk/0ffdd9e0df8604e8ba6f1b397d266284 to your computer and use it in GitHub Desktop.
Save beevk/0ffdd9e0df8604e8ba6f1b397d266284 to your computer and use it in GitHub Desktop.
backup and restore DB running inside container
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 <container_name>"
echo " - container_name: Name of the Docker container running MySQL"
exit 1
}
container_name="$1"
if [ -z "$1" ]; then
echo "Error: Please provide DB Container name."
usage
fi
# Directory where you want the backup files to be placed
backupdir="$HOME/backups"
# List all of the MySQL databases that you want to backup in here,
# each separated by a space
databases="ghost"
# MySQL dump command, use the full path name here
mysqldumpcmd="docker exec $container_name mysqldump -u root"
# MySQL dump options
dumpoptions=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"
read -sp "Enter your DB Root password: " db_password
# Get the current timestamp
TS=`date +%Y%m%d%H%M%S`
# Create our backup directory if not already there
mkdir -p ${backupdir}
if [ ! -d ${backupdir} ]
then
echo "Not a directory: ${backupdir}"
exit 1
fi
# Dump all of our databases
echo "Dumping MySQL Databases"
for database in $databases; do
$mysqldumpcmd --password="${db_password}" $dumpoptions $database > ${backupdir}/${database}-${TS}.sql
done
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 <container_name> <backup_file> [database_name]"
echo " - container_name: Name of the Docker container running MySQL"
echo " - backup_file: Path to the backup file to restore"
echo " - database_name (optional): Name of the database to restore to (default: \"ghost\")"
exit 1
}
# Check if container name and backup file are provided
if [ $# -lt 2 ]; then
echo "Error: Insufficient arguments."
usage
fi
container_name="$1"
backup_file="$2"
# Check if backup file exists
if [ ! -f "$backup_file" ]; then
echo "Backup file not found: $backup_file"
exit 1
fi
# Copy backup file to container's /tmp/ directory
docker cp "$backup_file" "$container_name:/tmp/$(basename "$backup_file")"
# Introduce a delay to allow time for the file to be copied
echo "Copying $backup_file to container..."
sleep 1 # Adjust the sleep duration (in seconds) as needed
# Default database
database="ghost"
# Get database from argument if present
if [ -n "$3" ]; then
database="$3"
fi
read -rsp "Enter your DB Root password: " db_password
echo
# Restore MySQL DB
echo "Restoring MySQL Databases: $database"
file_within_container="/tmp/$(basename "$backup_file")"
mysql_cmd="/usr/bin/mysql"
if docker exec -it "$container_name" "$mysql_cmd" -u root -p"$db_password" "$database" < "$file_within_container"; then
echo "Restore completed successfully."
else
echo "Error: Failed to restore database"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment