-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsql_bkp.sh
More file actions
45 lines (44 loc) · 1.95 KB
/
sql_bkp.sh
File metadata and controls
45 lines (44 loc) · 1.95 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
#!/bin/sh
### System Setup ###
NOW=`date +%Y-%m-%d`
KEEPDAYS=20
localBackup=/backups/mysql
#
### SSH Info ###
SHOST="ssh.yourhost.com"
SUSER="username"
SDIR="/backup/mysql"
#
### MySQL Setup ###
MUSER="username"
MPASS="password"
MHOST="hostname"
DBS="SCHEMA1 SCHEMA2 SCHEMA3"
#
### Start MySQL Backup ###
attempts=0
for db in $DBS # for each listed database
do
echo "Start $db"
attempts=`expr $attempts + 1` # count the backup attempts
ssh -C $SUSER@$SHOST mkdir $SDIR/$NOW #create the backup dir
FILE=$SDIR/$NOW/$db.sql.gz # Set the backup filename
# Dump the MySQL and gzip it up
ssh -C $SUSER@$SHOST "mysqldump -q -u $MUSER -h $MHOST -p$MPASS $db | gzip -9 > $FILE"
echo "End $db"
done
### Make local dir with today's date
mkdir /"$localBackup"/$NOW
scp -C $SUSER@$SHOST:/$SDIR/$NOW/* /backups/mysql/$NOW # copy all the files to backup server
ssh -C $SUSER@$SHOST rm -rf $SDIR/$NOW # delete files on db server
# deleting of old files on backup
###########################################################################
################### Save last month's last backup (this month's first day) #########################
###########################################################################
dayToKeep=`date +%d`
if [ "$dayToKeep" = "01" ]; then
permanents="/"$localBackup"/permanents/"
cp -rl /"$localBackup"/$NOW $permanents
fi
# Deletes everything older than $KEEPDAYS days
find "$localBackup" -type d -path "$permanents" -prune -o -daystart -mtime +$KEEPDAYS -exec rm -rf {} \;