From 9cea374e1228442c1c6efd94fc78a0995310726c Mon Sep 17 00:00:00 2001 From: marcymeer Date: Sat, 15 Mar 2025 15:17:01 +0100 Subject: [PATCH] Bash script to backup --- backup.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..500fde4 --- /dev/null +++ b/backup.sh @@ -0,0 +1,58 @@ +#!/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." \ No newline at end of file