首页 > 解决方案 > 每天在 Google 云之外进行 MySQL 备份

问题描述

我们只是尝试了带有故障转移的 google cloud SQL 2nd generation,它非常简单和令人惊奇,似乎我们不必再担心我们的数据库了。但是,为了更安全我们的数据,我们决定最好有一些不在谷歌内部托管的备份,虽然这不是必需的,但我们在 IBM 云中有帐户,他们有像 Amazon S3 这样的 IBM 云对象存储,我们认为可能是备份的好地方。现在我们计划在 IBM 数据中心的 VPS(Linux 服务器)上执行此操作,以 root 连接到 Google Cloud SQL 并执行 mysqldump 将所有数据转储到 1 个或多个文件(可能在 tar、gz 中),然后我们使用我们的 S3 备份脚本上传到 IBM 存储。

当我们得到您的答复或当我们弄清楚时,我们将发布我们的解决方案和步骤。

标签: sqlgoogle-cloud-platformcloud

解决方案


好的,我很高兴我可以在这里做一些贡献,如果有人想做我想做的事情,请在复制之前点击这些链接,另外,如果你有更好的方法,欢迎帮助修理它。检查链接: https ://github.com/lumerit/s3-shell-backups https://knowledgelayer.softlayer.com/procedure/connecting-cos-s3-using-s3cmd 我的脚本基于: https://gist .github.com/JProffitt71/9044744 https://andycroll.com/development/backing-up-mysql-databases-remotely-using-cron-and-ssh/

第一个脚本,您可以将其命名为“deleteold”并记住 chmod 755

#!/bin/bash
# Usage: ./deleteold "dir-of-your-bucketname" "-30 day"
# Usage-sample-2: ./deleteold "dir-of-your-bucketname" "-500 minutes"

# Pls put your bucketname in s3://here

s3cmd ls s3://my-database-backups/$1/ | while read -r line;
  do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
### 28800 sec is the time difference between our server & IBM COS
createData=`expr $createData + 28800`
olderThan=`date -d"$2" +%s`
if [[ $createDate -lt $olderThan ]]
  then
    fileName=`echo $line|awk {'print $4'}`
    echo $fileName
    if [[ $fileName != "" ]]
      then
        echo $fileName
        s3cmd del "$fileName"
      fi
  fi
 done;

第二个脚本

#!/bin/sh

#### BEGIN CONFIGURATION ####

##Account name SLOSC3xx982-4

# set dump directory variables
SRCDIR='/tmp/s3backups'
DESTDIR='google'
BUCKET='my-database-backups'

# database access details
HOST=‘x0x.x9.x4.98'
PORT='3306'
USER='root'
PASS='5xxxyyzznIoi8XXP'
NOW=`date +%Y-%m-%d_%H%M_`

#### END CONFIGURATION ####

# make the temp directory if it doesn't exist and cd into it
mkdir -p $SRCDIR
cd $SRCDIR

# dump each database to its own sql file and upload it to s3
for DB in $(mysql -h$HOST -P$PORT -u$USER -p$PASS -BNe 'show databases' | grep -Ev 
'mysql|information_schema|performance_schema')
do
mysqldump -h$HOST -P$PORT -u$USER -p$PASS --quote-names --create-options --force --set- 
gtid-purged=OFF $DB > $DB.sql
tar -czf $NOW$DB.tar.gz $DB.sql
/usr/bin/s3cmd put $SRCDIR/$NOW$DB.tar.gz s3://$BUCKET/$DESTDIR/ --reduced-redundancy
files="$files $DB"
done

# remove all files in our source directory
cd
rm -f $SRCDIR/*.sql
rm -f $SRCDIR/*.tar.gz
## remove older files
/root/backups3/deleteold "google" "-3 day"
#/root/backups3/deleteold "google" "-490 minutes"

### Mail me ###
EMAILID=“your-name@gmail.com"

mail -s "Backups Report $NOW" $EMAILID << END
Success backup cloud SQL from $HOST to IBM COS $BUCKET
The following databases were attempted backed up
$files
END

将这两个脚本放在 /root/backups3 后(你可以把它放在任何你喜欢的地方),设置 crontab,每天执行第二个脚本,这样它就会使用 mysqldump 将你所有的数据库转储到一个 tar.gz 文件然后上传到S3(IBM COS 或 Aws S3,随你喜欢)。完成后您会收到一封电子邮件,它还会调用脚本 1 根据您想要删除的天数或分钟数删除旧文件。


推荐阅读