简体   繁体   English

Ubuntu服务器上的MySQL自动备份

[英]Mysql Auto Backup on ubuntu server

After months of trying to get this to happen I found a shell script that will get the job done. 经过几个月的尝试,我找到了可以完成工作的shell脚本。

Heres the code I'm working with 这是我正在使用的代码

#!/bin/bash
### MySQL Server Login Info ###
MUSER="root"
MPASS="MYSQL-ROOT-PASSWORD"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/backup/mysql"
GZIP="$(which gzip)"
### FTP SERVER Login info ###
FTPU="FTP-SERVER-USER-NAME"
FTPP="FTP-SERVER-PASSWORD"
FTPS="FTP-SERVER-IP-ADDRESS"
NOW=$(date +"%d-%m-%Y")

### See comments below ###
### [ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/* ###
[ ! -d "$BAK" ] && mkdir -p "$BAK"

DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BAK/$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

lftp -u $FTPU,$FTPP -e "mkdir /mysql/$NOW;cd /mysql/$NOW; mput /backup/mysql/*; quit" $FTPS

Everything is running great, however there are a few things I'd like to fix but am clueless when it comes to shell scripts. 一切运行良好,但是我想修复一些问题,但是对于shell脚本却一无所知。 I'm not asking anyone to write it. 我不是要别人写它。 Just some pointers. 只是一些指针。 First of all the /backup/mysql directory on my server stacks the files everytime it backs up. 首先,服务器上的/ backup / mysql目录在每次备份时都会将文件堆叠在一起。 Not to big of a deal. 没什么大不了的。 But after a year of nightly backups it might get a little full. 但是,在每晚备份一年之后,它可能会变得有点满。 So id like it to clear that directory after uploading. 所以id喜欢它,以便在上传后清除该目录。 Also I don't want to overload my hosting service with files so id like it to clear the remote servers dir before uploading. 另外我也不想让我的托管服务超载文件ID这样的文件,以便在上传之前清除远程服务器目录。 Lastly I would like it to upload to a subdirectory on the remote server such as /mysql 最后,我希望它上传到远程服务器上的子目录,例如/ mysql

Why reinvent the wheel? 为什么要重新发明轮子? You can just use Debian 's automysqlbackup package (should be available on Ubuntu as well). 您可以只使用Debianautomysqlbackup软件包(也应该在Ubuntu上可用)。

As for cleaning old files the following command might be of help: 至于清理旧文件,以下命令可能会有所帮助:

find /mysql -type f -mtime +16 -delete

Uploading to remote server can be done using scp (1) command; 可以使用scp (1)命令上传到远程服务器;
To avoid password prompt read about SSH public key authentication 为避免出现密码提示,请阅读有关SSH公钥身份验证的信息

Take a look at Backup , it allows you to model your backup jobs using a Ruby DSL, very powerful. 看一下Backup ,它使您可以使用功能非常强大的Ruby DSL对备份作业进行建模。

Support multiple DBs and most popular online storages, and lots of cool features. 支持多个数据库和最流行的在线存储,以及许多很酷的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM