-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_binlogs.sh
More file actions
54 lines (36 loc) · 1.4 KB
/
sync_binlogs.sh
File metadata and controls
54 lines (36 loc) · 1.4 KB
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
#####-- Script: sync_binlogs_rsync.sh
#!/bin/bash
# CONFIGURATION
BINLOG_DIR="/var/lib/mysql"
REMOTE_USER="remote_user"
REMOTE_HOST="192.168.1.100"
REMOTE_DIR="/backup/mysql/binlogs"
ARCHIVE_DIR="/var/lib/mysql/compressed_binlogs"
LOG_TRACK="/var/log/binlog_copy.log"
RETENTION_DAYS=7
# Ensure archive directory exists
mkdir -p "$ARCHIVE_DIR"
# Go to binlog directory
cd "$BINLOG_DIR" || exit 1
# Find all mysql-bin.* files except index
for BINLOG in mysql-bin.*; do
[[ "$BINLOG" == "mysql-bin.index" ]] && continue
# Compress if not already compressed
if [ ! -f "$ARCHIVE_DIR/$BINLOG.gz" ]; then
echo "$(date): Compressing $BINLOG..." >> "$LOG_TRACK"
gzip -c "$BINLOG" > "$ARCHIVE_DIR/$BINLOG.gz"
fi
done
# Rsync compressed binlogs
echo "$(date): Syncing compressed binlogs to $REMOTE_HOST..." >> "$LOG_TRACK"
### scp "$ARCHIVE_DIR" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/"
rsync -av --ignore-existing "$ARCHIVE_DIR/" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/" >> "$LOG_TRACK" 2>&1
# Optional: Delete compressed files older than RETENTION_DAYS
find "$ARCHIVE_DIR" -name "*.gz" -mtime +$RETENTION_DAYS -exec rm -f {} \;
echo "$(date): Binlog sync completed." >> "$LOG_TRACK"
##### Cron Job (Every 15 Minutes)
crontab -e
*/15 * * * * /path/to/sync_binlogs_rsync.sh
##### On Remote Server
mkdir -p /backup/mysql/binlogs
chown remote_user:remote_user /backup/mysql/binlogs