Linux自动备份数据库并上传到FTP服务器脚本

  脚本实现功能:自动备份服务器的Mysql/MariaDB数据库上传到指定的FTP,同时删除X天之前的备份。




  shell脚本:


#!/bin/bash
#backup the mysql/mariadb and upload to the ftpserver

###################begin#############################

#check if installed lftp
[ ! -e '/usr/bin/lftp' ] && yum -y install lftp

#start defined variable

#defined time format,there is no need to modify
now=`date +%Y%m%d_%H%M%S`
#defined a database which needs to be backed up
database=iewbnet
#defined the username connected to database
databaseuser=root
#defined the password connected to database
databasepasswd=123456
#defined the backup file saved directory
databasedir=/home/test.com/mariadbbackup/mariadbdata
#defined backup file name
backupname=iewbnet.backup
#defined the ftpserver IP or domain
ftphost=172.16.88.8
#defined the ftpserver user
ftpuser=a
#defined the ftpserver password
ftppasswd=a
#defined the backup file how number of days to be saved
delday=14

#start backup the database
mysqldump -u$databaseuser -p$databasepasswd $database|gzip > $databasedir/$backupname\-$now.gz

#start ftp
/usr/bin/lftp <<EOF
open $ftphost
user $ftpuser $ftppasswd
put $databasedir/$backupname-$now.gz
bye
EOF

#start del backupfile before delday
find $databasedir -type f -mtime +$delday -name "$backupname-*.gz" -exec rm {} \;
#######################END#############################
  脚本其实很简单,因为很多服务器中文显示会有问题,所以使用的英文注释,看起来不太直观。使用时只需要修改变量部分即可,保存备份文件的目录必须已经存在。



推广推荐

Proudly powered by WordPress | IEWB.NET 2009-2021 版权所有