shell 自动备份db及文件

添加配置文件的shell
[python]
#!/bin/bash
thisdir=$(cd "$(dirname "$0")"; pwd)
read -p "Input your database login name:" dbuser
echo $dbuser
read -p "Input your database password:" dbpwd
echo $dbpwd
read -p "Input your database name:" dbname
echo $dbname
read -p "Input your mail:" email
echo $email
read -p "Input your backup web directory(Like:/home/wwwroot/default):" fullpath
echo $fullpath

if [ $fullpath == "clear" ]; then
fullpath="null"
fi
echo $dbuser $dbpwd $dbname $email $fullpath >> $thisdir/backup.conf
[/python]

下面是备份的shell
[python]
#!/bin/bash
thisdir=$(cd "$(dirname "$0")"; pwd)
backupdir=$thisdir"/backup/"
if [ ! -x $backupdir ]; then
mkdir $backupdir
fi
#echo "—————Progran is Runing——————" >> $thisdir/log
#load data to start backup
while read dbuser dbpwd dbname mail fullpath
do
Now=$(date +"%Y-%m-%d-%H-%M-%S")
filename=$dbname-$Now.sql
savefile=$backupdir$filename
echo "—————Backup Start——————" >> $thisdir/log
echo $Now >> $thisdir/log
if [ $fullpath != "NULL" ]; then
echo "directory is:"$fullpath
#check directory exists
if [ ! -x $fullpath ]; then
echo "directory:"$fullpath" does not exist"
echo "backup exit;"

else
cd $fullpath
echo "Find directory:"$fullpath;
echo "Start Backup Database:"$dbname>>$thisdir/log
echo "Create backup file:"$filename>>$thisdir/log
#Start Backup database
mysqldump -u$dbuser -p$dbpwd $dbname>$filename
echo "Start Backup Web directory:"$fullpath
echo "Create Zip File"$dbname-$Now.zip
zip -r $dbname-$Now.zip ./*
echo "Start move backup file to backup directory"
mv $dbname-$Now.zip $backupdir
echo "Moving Success"
echo "delete "$dbname"backup file in "$fullpath
rm -f $dbname-$Now.sql
fi
else
#Start Backup database
echo "Unset web directory">>$thisdir/log
echo "Start Backup Database:"$dbname>>$thisdir/log
echo "Create backup file:"$filename>>$thisdir/log
mysqldump -u$dbuser -p$dbpwd $dbname>$savefile

echo "Backup Database:"$dbname" Success">>$thisdir/log
echo "Send mail to "$mail>>$thisdir/log
php $thisdir/mail/index.php $mail $savefile $filename >> $thisdir/log
fi
echo "—————-Backup End——————-" >> $thisdir/log
done < $thisdir/backup.conf
#删除七天前的文件
oldtime=$(date +%Y-%m-%d -d "7 days ago")
rm -f $backupdir*$oldtime*.zip > $thisdir/log
[/python]

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据