Skip to content
Newer
Older
100755 56 lines (42 sloc) 1.16 KB
b036b8b @revolunet initial commit
authored
1 #!/bin/sh
2
3 #
4 # this simple script backup any local MySQL db to ./backups or directory given as 1st arg
5 # uses the user name `backup` and password stored in MYSQL_PWD file
6 # Dbs and tables are saved separately
7 # just call this via a cron script
8 #
9
10
11 MYDIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
12 OUTDIR="$MYDIR/backups";
13
14 if [ -n "$1" ]
15 then
16 OUTDIR=$1;
17 fi
18
e2a8418 @revolunet fix path
authored
19 cd $MYDIR
20
b036b8b @revolunet initial commit
authored
21 if [ ! -e MYSQL_PWD ]
22 then
23 echo "ERROR: Please create a 'MYSQL_PWD' file with MySQL backup user password!";
24 exit
25 fi
26
27 DATE=$(date +%Y%m%d%H%M);
28 OUTDIR="$OUTDIR/$DATE";
29
30 MYSQL_USER=backup;
31 MYSQL_PWD=`cat MYSQL_PWD`;
32
33 echo "BACKUP to $OUTDIR";
34 mkdir -p $OUTDIR >& /dev/null;
35
36 MYSQL="$(which mysql)"
37 MYSQLDUMP="$(which mysqldump)"
38
39 DBS="$($MYSQL -h 127.0.0.1 -u$MYSQL_USER -p$MYSQL_PWD -Bse 'show databases')"
40 for DATABASE in $DBS
41 do
42 TABLES="$($MYSQL -h 127.0.0.1 -u$MYSQL_USER -p$MYSQL_PWD $DATABASE -Bse 'show TABLES')"
43 for TABLE in $TABLES
44 do
45 FILENAME="$OUTDIR/$DATABASE__$TABLE"__$DATE.sql;
46 $MYSQLDUMP -h 127.0.0.1 -u $MYSQL_USER -p$MYSQL_PWD $DATABASE "$TABLE" > "$FILENAME";
47 if [ -e "$FILENAME" ]
48 then
49 tar -czvf $FILENAME.tgz $FILENAME >& /dev/null;
50 rm $FILENAME;
51 fi
52 done;
53 done;
54
55
Something went wrong with that request. Please try again.