Skip to content

Commit

Permalink
Backup your MySQL databases over FTP
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Jul 11, 2020
1 parent 8dfdb2b commit 11a2d43
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Nginx与Lua编写脚本的基本构建块是指令执行顺序的图
* [SHELL脚本小技巧](/Nginx-Rtmp/Shell_script.md)
* [Mysql 自动备份脚本安全加锁机制](/Nginx-Rtmp/backup_mysql.sh)
* [PHP和Shell 脚本如何很好的搭配](/PHP/php-shell_run.md)
* [通过FTP备份MySQL数据库](/Shell/Backup-MySQL-FTP.md)

## 微信公众号

Expand Down
74 changes: 74 additions & 0 deletions Shell/Backup-MySQL-FTP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
### your favorite editor, create the script file
```
vim backupdb.sh
```

```shell
#!/bin/bash

############### Infos - Edit them accordingly ########################

DATE=`date +%Y-%m-%d_%H%M`
LOCAL_BACKUP_DIR="/backups"
DB_NAME="database_name"
DB_USER="root"
DB_PASSWORD="root_password"

FTP_SERVER="111.111.111.111"
FTP_USERNAME="ftp-user"
FTP_PASSWORD="ftp-pass"
FTP_UPLOAD_DIR="/upload"

LOG_FILE=/backups/backup-DATE.log

############### Local Backup ########################

mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME | gzip > $LOCAL_BACKUP_DIR/$DATE-$DB_NAME.sql.gz

############### UPLOAD to FTP Server ################

ftp -nv $FTP_SERVER << EndFTP
user "$FTP_USERNAME" "$FTP_PASSWORD"
binary
cd $FTP_UPLOAD_DIR
lcd $LOCAL_BACKUP_DIR
put "$DATE-$DB_NAME.sql.gz"
bye
EndFTP

############### Check and save log, also send an email ################

if test $? = 0
then
echo "Database Successfully Uploaded to the Ftp Server!"
echo -e "Database Successfully created and uploaded to the FTP Server!" | mail -s "Backup from $DATE" [email protected]

else
echo "Error in database Upload to Ftp Server" > $LOG_FILE
fi
```
完成脚本编辑并保存文件后,我们可以通过以下命令使文件可执行:
```
chmod +x backupdb.sh
```
您现在可以通过在终端中输入进行测试。
```
/backups/backupdb.sh
```
完成执行后,键入ls -a以查看数据库是否已备份。还要确认它是否已发送到您的FTP服务器。

>如果到目前为止一切正常,我们可以使用Crontab使它每天运行。
## Crontab

您可以使用以下命令编辑crontab:
```
crontab -e
```
这将打开一个文本编辑器,您可以在其中输入每份工作的时间表并在新行上输入。

因此,在编辑器中,键入或粘贴以下行:
```
30 02 * * * /backups/backupdb.sh
```
上面的示例将在每月的每天的02:30 am运行/backups/backupdb.sh。当然,您可以根据需要更改时间。

0 comments on commit 11a2d43

Please sign in to comment.