-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-mysql-backup.sh
executable file
·71 lines (59 loc) · 1.6 KB
/
s3-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
66
67
68
69
70
#!/bin/bash
# Simple script that compresses and copies all databases into tarballs
# and uploads them to the provided s3 bucket
#
# Thanks to: http://www.vps2.me/automatic-backup-script-linux-mysql-files-amazon-s3/
# for the basics of this script
# This script expects three arguments:
#
# - username: The database username
# - bucket: name of the S3 bucket
# - password: the database password
#
# You can also provide an optional forth argument:
#
# - backupdir: location to place backups
#
# Example usage:
#
# ./s3-folder-backup.sh /path/to/folder bucket-name [/path/to/backups]
datestring=$(date +%Y-%m-%d);
backupdir="/tmp/backups/db/";
if [ -z "$1" ]; then
echo "No username provided";
exit;
else
uname=$1;
fi
if [ -z "$2" ]; then
echo "No s3 bucket name provided";
exit;
else
bucket=$2;
fi
if [ -z "$3" ]; then
pword="";
else
pword=$3;
fi
if [ "$4" ]; then
backupdir=$4;
fi
# Make backups dir is not existing
if [ ! -d "$backupdir" ]; then
mkdir $backupdir
fi
for a in `echo 'show databases' | mysql -u${uname} -p${pword} | grep -v Database | grep -v information_schema`; do
echo "Dumping database: $a"
if [ -z "$pword" ]; then
# Password not set
mysqldump -u${uname} --create-options --complete-insert --single-transaction "${a}" > "${backupdir}${a}-${datestring}".sql;
else
# Password set
mysqldump -u${uname} -p${pword} --create-options --complete-insert --single-transaction "${a}" > "${backupdir}${a}-${datestring}".sql;
fi
done
# remove backups older than 1 days
find ${backupdir} -mtime +1 -exec rm {} \;
# sync to amazon
s3cmd sync ${backupdir} s3://${bucket}