-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmysql_bak.sh
70 lines (66 loc) · 2.35 KB
/
mysql_bak.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
#!/bin/bash
#Author [email protected]
MAXIMUM_BACKUP_FILES=10 #
BACKUP_PATH="/opt/data_bak" #
BACKUP_FOLDER_PREFIX="db" #
DB_HOSTNAME="127.0.0.1" #mysql
DB_USERNAME="root" #mysql
DB_PASSWORD="" #mysql
DATABASES=(
"saleasy"
)
#=========
echo "Bash Database Backup Tool"
cd $BACKUP_PATH
echo "cd into backup folder path"
#CURRENT_DATE=$(date +%F)
CURRENT_DATE=$(date +%F) #
BACKUP_FOLDER="${BACKUP_FOLDER_PREFIX}_${CURRENT_DATE}" #
mkdir $BACKUP_FOLDER #
#
count=0
while [ "x${DATABASES[count]}" != "x" ];do
count=$(( count + 1 ))
done
echo "[+] ${count} databases will be backuped..."
#
for DATABASE in ${DATABASES[@]};do
echo "[+] Mysql-Dumping: ${DATABASE}"
echo -n " Began: ";echo $(date)
if $(mysqldump -h ${DB_HOSTNAME} -u${DB_USERNAME} -p${DB_PASSWORD} ${DATABASE} > "${BACKUP_FOLDER}/${DATABASE}.sql");then
echo " Dumped successfully!"
else
echo " Failed dumping this database!"
fi
echo -n " Finished: ";echo $(date)
done
echo
echo "[+] Packaging and compressing the backup folder..."
tar -cv ${BACKUP_FOLDER} | bzip2 > ${BACKUP_FOLDER}.tar.bz2 && rm -rf $BACKUP_FOLDER
BACKUP_FILES_MADE=$(ls -l ${BACKUP_FOLDER_PREFIX}*.tar.bz2 | wc -l)
BACKUP_FILES_MADE=$(( $BACKUP_FILES_MADE - 0 ))
#
echo
echo "[+] There are ${BACKUP_FILES_MADE} backup files actually."
#,
if [ $BACKUP_FILES_MADE -gt $MAXIMUM_BACKUP_FILES ];then
REMOVE_FILES=$(( $BACKUP_FILES_MADE - $MAXIMUM_BACKUP_FILES ))
echo "[+] Remove ${REMOVE_FILES} old backup files."
#
ALL_BACKUP_FILES=($(ls -t ${BACKUP_FOLDER_PREFIX}*.tar.bz2))
SAFE_BACKUP_FILES=("${ALL_BACKUP_FILES[@]:0:${MAXIMUM_BACKUP_FILES}}")
echo "[+] Safeting the newest backup files and removing old files..."
FOLDER_SAFETY="_safety"
if [ ! -d $FOLDER_SAFETY ]
then mkdir $FOLDER_SAFETY
fi
for FILE in ${SAFE_BACKUP_FILES[@]};do
mv -i ${FILE} ${FOLDER_SAFETY}
done
rm -rf ${BACKUP_FOLDER_PREFIX}*.tar.bz2
mv -i ${FOLDER_SAFETY}/* ./
rm -rf ${FOLDER_SAFETY}
fi
#crontab
# crontab -e
# 0 05 * * * /opt/data_bak/mysql_bak.sh