forked from ameir/mysqlbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackupmysql.sh
More file actions
102 lines (83 loc) · 2.85 KB
/
backupmysql.sh
File metadata and controls
102 lines (83 loc) · 2.85 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /bin/bash
# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
#----------------------Start of Script------------------#
function die () {
echo >&2 "$@"
exit 1
}
if ! type -p 7z > /dev/null; then
echo "Please install 7z first"
exit 1
fi
CONFIG=${1:-`dirname $0`/backupmysql.conf}
[ -f "$CONFIG" ] && . "$CONFIG" || die "Could not load configuration file ${CONFIG}!"
# check of the backup directory exists
# if not, create it
if [ ! -d $BACKDIR ]; then
echo -n "Creating $BACKDIR..."
mkdir -p $BACKDIR
echo "done!"
fi
if [ $DUMPALL = "y" ]; then
echo -n "Creating list of all your databases..."
DBS=`mysql -h $HOST --user=$USER --password=$PASS -Bse "show databases;"`
echo "done!"
fi
echo "Backing up MySQL databases..."
for database in $DBS; do
echo -n "Backing up database $database..."
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-$database-$DATE-mysqlbackup.sql
echo "Compressing database $database..."
7z a -t7z -m0=LZMA -mx=5 $BACKDIR/$SERVER-$database-$DATE-mysqlbackup.sql.7z $BACKDIR/$SERVER-$database-$DATE-mysqlbackup.sql
echo "Removing uncompressed database dump..."
rm $BACKDIR/$SERVER-$database-$DATE-mysqlbackup.sql
echo "done!"
done
# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS
if [ $MAIL = "y" ]; then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net. \n\n"
BODY=$BODY`cd $BACKDIR; for file in *$DATE-mysqlbackup.sql.7z; do md5sum ${file}; done`
ATTACH=`for file in $BACKDIR/*$DATE-mysqlbackup.sql.7z; do echo -n "-a ${file} "; done`
echo -e "$BODY" | mutt -s "$SUBJECT" $ATTACH -- $EMAILS
if [[ $? -ne 0 ]]; then
echo -e "ERROR: Your backup could not be emailed to you! \n";
else
echo -e "Your backup has been emailed to you! \n"
fi
fi
if [ $DELETE = "y" ]; then
OLDDBS=`cd $BACKDIR; find . -name "*-mysqlbackup.sql.7z" -mtime +$DAYS`
REMOVE=`for file in $OLDDBS; do echo -n -e "delete ${file}\n"; done` # will be used in FTP
cd $BACKDIR; for file in $OLDDBS; do rm -v ${file}; done
if [ $DAYS = "1" ]; then
echo "Yesterday's backup has been deleted."
else
echo "The backups from $DAYS days ago and earlier have been deleted."
fi
fi
if [ $FTP = "y" ]; then
echo "Initiating FTP connection..."
cd $BACKDIR
ATTACH=`for file in *$DATE-mysqlbackup.sql.7z; do echo -n -e "put ${file}\n"; done`
for KEY in "${!FTPHOST[@]}"; do
echo -e "\nConnecting to ${FTPHOST[$KEY]} with user ${FTPUSER[$KEY]}..."
ftp -nvp <<EOF
open ${FTPHOST[$KEY]}
user ${FTPUSER[$KEY]} ${FTPPASS[$KEY]}
tick
mkdir ${FTPDIR[$KEY]}
cd ${FTPDIR[$KEY]}
$REMOVE
$ATTACH
quit
EOF
done
echo -e "FTP transfer complete! \n"
fi
echo "Your backup is complete!"