-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.sh
64 lines (52 loc) · 2.36 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
DB_BACKUP_PATH=/data/
CURRENT_DATE=$(date +%F_%T)
echo "Dump mysql db for $DB_NAME... "
mysql --version
IGNORE=""
for table in $IGNORE_TABLES; do
IGNORE+="--ignore-table=$DB_NAME.$table "
done
if test -n "${ONLY_TABLE-}"; then
echo "🚧 Creating backup only for table $ONLY_TABLE"
SQL="SET group_concat_max_len = 10240;"
SQL="${SQL} SELECT GROUP_CONCAT(table_name separator ' ')"
SQL="${SQL} FROM information_schema.tables WHERE table_schema='${DB_NAME}'"
SQL="${SQL} AND table_name LIKE '$ONLY_TABLE'"
echo $SQL
TBLIST=`mysql -h "$DB_HOST" -u $DB_USER -p"$DB_PASS" -AN -e"${SQL}"`
mysqldump -h "$DB_HOST" -u $DB_USER -p"$DB_PASS" $DB_NAME $TBLIST --verbose > $DB_BACKUP_PATH/$DB_NAME-$CURRENT_DATE.sql
elif [[ "${IGNORE_TABLES}" ]]; then
echo "🚧 Ignoring table $IGNORE_TABLES"
mysqldump -h "$DB_HOST" -u $DB_USER -p"$DB_PASS" $DB_NAME $IGNORE --verbose | gzip > $DB_BACKUP_PATH/$DB_NAME-$CURRENT_DATE-2.sql.gz
echo "🚧 Uploading mysql dump ($DB_NAME-$CURRENT_DATE.sql.gz) to s3 ..."
aws s3 --endpoint=https://$S3_URL cp $DB_BACKUP_PATH/$DB_NAME-$CURRENT_DATE-2.sql.gz s3://${S3_BUCKET}/db/
else
echo "✅Creating backup for entire database"
mysqldump -h "$DB_HOST" -u $DB_USER -p"$DB_PASS" $DB_NAME --verbose | gzip > $DB_BACKUP_PATH/$DB_NAME-$CURRENT_DATE-730.sql.gz
echo "🚧 Uploading mysql dump ($DB_NAME-$CURRENT_DATE.sql.gz) to s3 ..."
aws s3 --endpoint=https://$S3_URL cp $DB_BACKUP_PATH/$DB_NAME-$CURRENT_DATE-730.sql.gz s3://${S3_BUCKET}/db/
fi
echo "✅ Backup finished successfully"
deleted="false"
echo "⚠️ Check for files older than X days ..."
cd /data
currentDate=$(date +%s)
aws s3 --endpoint=https://$S3_URL ls $S3_BUCKET/db/ | while read -r line; do
fileName=$(echo $line | awk '{print $4}')
createdAt=$(echo "$line" | awk '{print $4}' | awk -F'[-_.]' '{print $2"-"$3"-"$4" "$5}')
createdAt=$(date -d "$createdAt" +%s)
fileAge=$(( ($currentDate - $createdAt) / (24*60*60) ))
# Extract the number of days from the file name
retentionDays=$(echo $fileName | awk -F'[-.]' '{print $(NF-2)}')
# Check if the file is older than the specified number of days
if [[ $fileAge -ge $retentionDays ]]; then
deleted="true"
echo "🚨 Deleting file $fileName"
aws s3 --endpoint=https://$S3_URL rm s3://$S3_BUCKET/db/$fileName
fi
done
if [[ $deleted == "false" ]]; then
echo "✅ Nothing to delete"
fi
echo "👋 Bye"