58 lines
1.5 KiB
Bash
58 lines
1.5 KiB
Bash
#!/bin/bash
|
|
|
|
# Configuration
|
|
DB_HOST="remote_server_address"
|
|
DB_USER="database_username"
|
|
DB_PASS="database_password"
|
|
DB_NAME="database_name"
|
|
DB_PORT="3306"
|
|
|
|
# Git repository path
|
|
REPO_PATH="/path/to/your/git/repository"
|
|
|
|
# Backup directory inside the repository
|
|
BACKUP_DIR="${REPO_PATH}/database_backups"
|
|
|
|
# Backup filename with timestamp
|
|
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
|
BACKUP_FILE="${BACKUP_DIR}/backup_${DB_NAME}_${TIMESTAMP}.sql"
|
|
|
|
# Create the backup directory if it doesn't exist
|
|
mkdir -p "${BACKUP_DIR}"
|
|
|
|
# Perform the database backup
|
|
echo "Starting backup of ${DB_NAME} from ${DB_HOST}..."
|
|
mysqldump -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" > "${BACKUP_FILE}"
|
|
|
|
# Check if backup was successful
|
|
if [ $? -eq 0 ]; then
|
|
echo "Database backup completed successfully: ${BACKUP_FILE}"
|
|
|
|
# Compress the backup file
|
|
gzip "${BACKUP_FILE}"
|
|
BACKUP_FILE="${BACKUP_FILE}.gz"
|
|
echo "Backup file compressed: ${BACKUP_FILE}"
|
|
|
|
# Add the backup to Git
|
|
cd "${REPO_PATH}"
|
|
git add "${BACKUP_FILE}"
|
|
|
|
# Commit the changes
|
|
git commit -m "Database backup: ${DB_NAME} - ${TIMESTAMP}"
|
|
|
|
# Push to remote repository
|
|
git push origin main
|
|
|
|
# Check if Git operations were successful
|
|
if [ $? -eq 0 ]; then
|
|
echo "Backup successfully pushed to Git repository."
|
|
else
|
|
echo "Error: Failed to push backup to Git repository."
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "Error: Database backup failed."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Backup process completed." |