-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxtradir.sh
86 lines (76 loc) · 2.01 KB
/
xtradir.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# Run xtrabackup and save either in folder [NUMBER]A or [NUMBER]B.
# If successful, delete the other folder (A or B).
# Configuration file ~/.my.conf:
#
# [xtrabackup]
# user=root
# password=[PASSWORD]
ME=$(basename "$0")
MY_DIR="/mnt/usb_wd_4tb_crypt/backup"
function usage {
returnCode="$1"
echo
echo -e "Usage:
$ME -d <DIR> -n <NUMBER>] [-h]
-d <DIR> Backup directory (Default: '$MY_DIR')
-n <NUMBER> Folder number
[-h] Displays help (this message)"
echo
exit "$returnCode"
}
while getopts "d:n:h" opt; do
case $opt in
d)
MY_DIR="$OPTARG"
;;
n)
MY_NUMBER="$OPTARG"
;;
h)
usage 0
;;
*)
usage 1
;;
esac
done
command -v xtrabackup >/dev/null 2>&1 || { echo >&2 "!!! Error !!! xtrabackup it's not installed."; exit 1; }
if ((MY_NUMBER >= 1)); then
echo "Folder number: $MY_NUMBER"
else
echo "Folder number for backup missing"
exit 3
fi
if [ -d "$MY_DIR" ]; then
MY_DIR=${MY_DIR%/}
echo "Backup dir: $MY_DIR"
else
echo "Backup dir missing or directory '$MY_DIR' does not exists."
exit 2
fi
MY_BACKUP_DIR_A="$MY_DIR""/""$MY_NUMBER""A"
MY_BACKUP_DIR_B="$MY_DIR""/""$MY_NUMBER""B"
if [ -d "$MY_BACKUP_DIR_A" ]; then
mkdir -p "$MY_BACKUP_DIR_B"
echo "Backup to: $MY_BACKUP_DIR_B"
if xtrabackup --backup --slave-info --safe-slave-backup --target-dir="$MY_BACKUP_DIR_B"; then
echo "Success. Delete old backup."
rm -rf "$MY_BACKUP_DIR_A"
else
echo "!!! Error !!! Delete current and errored backup."
rm -rf "$MY_BACKUP_DIR_B"
exit 9
fi
else
mkdir -p "$MY_BACKUP_DIR_A"
echo "Backup to: $MY_BACKUP_DIR_A"
if xtrabackup --backup --slave-info --safe-slave-backup --target-dir="$MY_BACKUP_DIR_A"; then
echo "Success. Delete old backup."
rm -rf "$MY_BACKUP_DIR_B"
else
echo "!!! Error !!! Delete current and errored backup."
rm -rf "$MY_BACKUP_DIR_A"
exit 9
fi
fi