-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_dump.sh
34 lines (26 loc) · 932 Bytes
/
db_dump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
# Load environment variables from .env file
if [[ -f .env ]]; then
export $(cat .env | grep -v '^#' | xargs)
fi
# Get the current date and time
TIMESTAMP=$(date +%Y%m%d%H%M%S)
# Database credentials
DB_USER=root
DB_PASSWORD=$MYSQL_ROOT_PASSWORD
DB_NAME=$DB_NAME
# Create the backup folder if it doesn't exist
BACKUP_FOLDER="/db_backup"
mkdir -p $BACKUP_FOLDER
# Dump the database to an SQL file
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME --single-transaction --quick > $BACKUP_FOLDER/backup_$TIMESTAMP.sql
# Compress the SQL file into a tar.gz archive
tar -czf $BACKUP_FOLDER/backup_$TIMESTAMP.tar.gz $BACKUP_FOLDER/backup_$TIMESTAMP.sql
# Remove the uncompressed SQL file
rm $BACKUP_FOLDER/backup_$TIMESTAMP.sql
# Keep only the 10 most recent backup files
cd $BACKUP_FOLDER
backup_files=$(ls -tp | grep -v '/$' | tail -n +11)
if [ -n "$backup_files" ]; then
echo "$backup_files" | xargs -d '\n' rm --
fi