#!/bin/bash
#auto ftp bakup
#Db need to be backup,separate by space
db="test"
#ftp account
ftphost="192.168.1.111"
ftpuser=test
ftppass=test
#mysql account
DBUser=root
DBPasswd=123456
BackupPath=/home/robin/bak/
array=($db)
for ((i=0;i
do
{
NewFile="$BackupPath"db_${array[$i]}_$(date +%y%m%d).tar.gz
DumpFile="$BackupPath"db_${array[$i]}_$(date +%y%m%d).sql
OldFile="$BackupPath"db_${array[$i]}_$(date +%y%m%d --date='1 days ago').tar.gz
#LogFile="$BackupPath"db$(date +%y%m%d --date='1 days ago').txt
echo "---------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "---------------------------"
#删除
if [ -f $OldFile ]; then
rm -f $OldFile
echo "[$OldFile]Delete Old File Success!"
fi
#新
if [ -f $NewFile ]; then
echo "[$NewFile] exist, don't backup! "
else
/usr/bin/mysqldump -u$DBUser -p$DBPasswd ${array[$i]} > $DumpFile
sleep 5
tar czvf $NewFile $DumpFile
rm $DumpFile -rf
echo "[$NewFile] backup sucessfully"
fi
echo "open ftp"
ftp -i -in <
open $ftphost 21
user $ftpuser $ftppass
mput *.tar.gz
bye
!
}
done
echo "backup complete"