用于每日数据库备份的Shell脚本(用于类似Redmine等的东西)

时间:2015-09-01 08:53:21

标签: mysql bash shell backup redmine

需要一个脚本解决方案来每天备份数据库并为其发送邮件。

代码为mysql数据库和所涉及的文件提供备份(如在大多数用例中)

1 个答案:

答案 0 :(得分:0)

#!/bin/bash
cat /dev/null > Body.text
DATE=`date +%b/%d/%Y`
BACKUP_DIR="/home/user/Redmine/tmp"
DB_NAME="xxx"
DB_USER="xxx"
DB_PASSWORD="XXXXXX"
Redmine_Root="/home/webuser/apps/redmine"
echo "Redmine Backup Directory is $BACKUP_DIR" >> /root/Body.text
rm -rf $BACKUP_DIR/files* $BACKUP_DIR/Redmine*

# -- MySQL
echo "`date` at Redmine's MySQL db Backup on" >> /root/Body.text
mysqldump -u $DB_USER  -p$DB_PASSWORD $DB_NAME > $BACKUP_DIR/Redmine.sql
gzip $BACKUP_DIR/Redmine.sql
rm -f $BACKUP_DIR/Redmine.sql

#........Redmine
echo "`date` at REDMINE_Files Backup " >> /root/Body.text

#Create back up for Files
echo "Backing up Redmine attachments..." 
rsync -a $Redmine_Root/files/ $BACKUP_DIR/files/

echo "Packing into single archive redmine files" 
tar -zcvf $BACKUP_DIR/redminefiles.tar $BACKUP_DIR/files
rm -rf $BACKUP_DIR/files/

#Create a single tar file
echo "Create Backup of Single File" >> /root/Body.text
cd /home/user/Redmine
tar -cvf Redmine`date +%b%d%y`.tar tmp/
tar -cvf /home/user/Redmine`date +%d%b%y`.tar /home/user/Redmine/tmp
rm -f $BACKUP_DIR/redminefiles* $BACKUP_DIR/Redmine*
rm -f $BACKUP_DIR/Redmine.sql.gz

#Cleaning Up
echo "Delete five days olderbackup" >> /root/Body.text
find /home/maitreya/Redmine/* -mtime +5 -exec rm -rf {} \;

#Sending Report
/usr/bin/mutt -e "set realname=\" Redmine-Backup\"  "   \  -s "Redmine Backup on $DATE" xy@yz.com -c yz@xy.com < /root/Body.text