-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_backup.sh
61 lines (54 loc) · 2.11 KB
/
mysql_backup.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
#!/bin/bash
# Copyright (C) <2015> <Akshat Singh>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# This Shell Script can be used to take take MySQL automated backup and tranfer
# the dump to remote server. This script will also deletes the dumps which are
# older than 3 days. Then it will take the backup of the databases and transfer
# the dumps to the remote server.
# NOTE : For automated backup add the following entry of this script to cronjob
# Eg. : $ crontab -e
# # min hrs dom mon dow command
# 45 2 * * * sh /path/to/Script
#
# FEEL FREE TO EDIT FOR YOUR OWN CUSTOMIZED USAGE.
BACKUPDIR="/home/akshat/MySQLBKP"
cd $BACKUPDIR
find . -name '*' -mtime +3 -exec rm -R {} \;
SUFFIX=`date +%d-%m-%Y`
DBUSER="root"
DBHOST="localhost"
DBPASS='password'
DBS=`mysql -u$DBUSER -h$DBHOST -p$DBPASS -e"show databases"`
for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
FILENAME=$SUFFIX-bkp-$DATABASE.gz
mysqldump -u$DBUSER -h$DBHOST -p$DBPASS $DATABASE | gzip --best > $BACKUPDIR/$FILENAME
fi
done
## Server Location
ServerIP="XXX.XXX.XXX.XXX"
ServerLoc="/home/server/BACKUP/MySQL"
ServerUsername="server"
ServerBKPDirectory="MyBackup"
#Server Rsync
echo "========================================================"
echo "Server XXX.XXX.XXX.XXX Database Rsyncing in Progress..."
echo "========================================================"
cd $BACKUPDIR
rsync -av $BACKUPDIR/$ServerBKPDirectory/ $ServerUsername@$ServerIP:$ServerLoc