This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mysql_backup.sh
executable file
·65 lines (53 loc) · 1.69 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
62
63
64
65
#!/bin/sh
inifile=db.ini
username=$(sed -n 's/.*username *= *\([^ ]*.*\)/\1/p' < $inifile | sed -e 's/^"//' -e 's/"$//')
password=$(sed -n 's/.*password *= *\([^ ]*.*\)/\1/p' < $inifile | sed -e 's/^"//' -e 's/"$//')
dbname=$(sed -n 's/.*dbname *= *\([^ ]*.*\)/\1/p' < $inifile | sed -e 's/^"//' -e 's/"$//')
# List of databases to be backed up separated by space
dblist="$dbname"
# Directory for backups
backupdir=/var/dpla_omeka/mysql_dumps
# Number of versions to keep
numversions=4
# Full path for MySQL hotcopy command
# Please put credentials into /root/.my.cnf
#hotcopycmd=/usr/bin/mysqlhotcopy
hotcopycmd="/usr/bin/mysqldump -u $username -p$password --lock-tables --databases"
echo $hotcopycmd
# Create directory if needed
mkdir -p "$backupdir"
if [ ! -d "$backupdir" ]; then
echo "Invalid directory: $backupdir"
exit 1
fi
# Hotcopy begins here
echo "Dumping MySQL Databases..."
RC=0
for database in $dblist; do
echo
echo "Dumping $database ..."
# mv "$backupdir/$database.gz" "$backupdir/$database.0.gz" 2> /dev/null
# filename="logfile.`date '+%m%d%Y'`.log"
echo `date '+%m%d%Y'`
$hotcopycmd $database | gzip > "$backupdir/$database.`date '+%Y-%m-%d-%H%M%S'`.gz"
RC=$?
if [ $RC -gt 0 ]; then
continue;
fi
# Rollover the backup directories
# rm -fr "$backupdir/$database.$numversions.gz" 2> /dev/null
# i=$numversions
# while [ $i -gt 0 ]; do
# mv "$backupdir/$database.`expr $i - 1`.gz" "$backupdir/$database.$i.gz" 2> /dev/null
# i=`expr $i - 1`
# done
done
if [ $RC -gt 0 ]; then
echo "MySQL Dump failed!"
exit $RC
else
# Hotcopy is complete. List the backup versions!
ls -l "$backupdir"
echo "MySQL Dump is complete!"
fi
exit 0