这个是我在工作中用到的bash脚本,在此记录下,如有问题麻烦指出!
#!/bin/bash
#Back up the entire tomcat8080-dist to the /backup directory
backuppath=/mnt/tomcat8080-dist/webapps/
backpath=/backup/backup-tomcat8080-dist/
action=/backup/backup-tomcat8080-dist/
APPNAME=dist
BACKUPNAME1=dist
BACKUPNAME2=spongod-module-admin
BACKUPNAME3=spongod-module-interface
for i in 1,2,3
do
echo "" >>/backup/log/backup.log
done
echo "this backup start!" >>/backup/log/backup.log
#echo "----STRAT BACKUP TIME:`date '+%Y%m%d-%H%M'`" >>/backup/log//backup.log
if [[ -n $backpath ]] && [[ -n $backuppath ]];then
echo "--------STARTUP BACKUP TIME:`date '+%Y-%m-%d-%H-%M'`-------" >>/backup/log/backup.log
cd /mnt/tomcat8080-dist/ 2>> /backup/log/backup.log
time=`date '+%Y-%m-%d-%H-%M'`
tar czf /backup/backup-tomcat8080-dist/$APPNAME-$time\.tar.gz webapps/$BACKUPNAME1/ webapps/$BACKUPNAME2/ webapps/$BACKUPNAME3/ 2>>/backup/log/BackupFailed.log
if [ $? -eq 0 ];then
echo -e "----\033[32m BACKUP SUCCESSFUL\033[0m-----------------------" >>/backup/log/backup.log
echo "--------BACKUP END TIME is:`date '+%Y-%m-%d-%H-%M'`--------" >>/backup/log/backup.log
echo "This backup ends" >>/backup/log/backup.log
for i in 1,2,3
do
echo "" >>/backup/log/backup.log
done
else
echo -e "----\033[31m Backup failed, please try again \033[0m -------------" >>/backup/log/BackupFailed.log
echo "--------Failed Time:`date '+%Y-%m-%d-%H-%M'`-----------" >>/backup/log/BackupFailed.log
fi
fi
#Delete files from three days away
echo "" >>/backup/log/delete-backup.log
echo "start deleted file" >>/backup/log/delete-backup.log
find $action -mtime +3 -type f |xargs rm -f
if [ $? -eq 0 ];then
echo "----------File deleted successfully--------------" >>/backup/log/delete-backup.log
else
echo "----------File deletion failed-------------------" >>/backup/log/delete-backup.log
echo "" >>/backup/log/delete-backup.log
fi