我的备份脚本有一个奇怪的问题。
首先,这是备份脚本:
###########################################################
# CODE BEGINS HERE
###########################################################
#!/bin/bash
# SERVER DATA BACKUP V1.0
#########################
# BACKUP CONFIG
#########################
#Configure the month, date, and day
YEAR=`date +"%Y"` # 2014
MONTH=`date +"%m"` # 10
TODAY=`date +%Y-%m-%d` # 2008-11-14
TODAY_DATE=`date +%d` # 14
# directories to backup to
DATA_BACKUP_PARENT_DIR=/home/user/backups
# Apache Document Root Directory
APACHE_DOCROOT=/var/www/
# MySQL Database config
DB_IP="localhost"
DB_USER="auser" #
DB_PASS="apass" #
PUBLIC_HTML=/home/user/public_html
# Config Dir
CONFIG_DIR="/etc/" # <-- Change this to the path data directory
# Mail report address
EMAIL_ID="[email protected]"
###########################################################
# DATA BACKUP
###########################################################
# Create and go into backup directory
cd $DATA_BACKUP_PARENT_DIR
if [ ! -d "$TODAY" ]; then
mkdir $TODAY
fi
cd $TODAY
# Database backup
databases=`mysql --user=$DB_USER --password=$DB_PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
echo "Backing up databases"
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump --force --opt --user=$DB_USER --password=$DB_PASS $db > $db.db
tar -zcvf $db.tar.gz $db.db
rm $db.db
fi
done
echo "Backing up public_html folder"
#Home folder backup
tar -zcf public_html.tar.gz ${PUBLIC_HTML} --exclude='.*' --exclude=/home/user/public_html/software
# /etc backup
#tar -zcf etc.tar.gz /etc
echo "Backing up Apache wwww doc root folder"
# Application and Websites backup
tar -zcf Apache_Doc_Root.tar.gz ${APACHE_DOCROOT}
###########################################################
# Send out mail notifications
###########################################################
# Mail notify
du -shc $DATA_BACKUP_PARENT_DIR/$TODAY/* | mail -s "Backup of server done " ${EMAIL_ID}
###########################################################
# CODE ENDS HERE
#
当我从控制台手动运行脚本时,命令运行顺利并创建所有适当的文件夹。确认邮件中写道:
250M /home/user/backups/2014-11-19/Apache_Doc_Root.tar.gz
152K /home/user/backups/2014-11-19/mysql.tar.gz
487M /home/user/backups/2014-11-19/public_html.tar.gz
201M /home/user/backups/2014-11-19/mydb.tar.gz
240K /home/user/backups/2014-11-19/my_site.tar.gz
76K /home/user/backups/2014-11-19/teo_wordpress.tar.gz
937M total
我已经用 cron 安排了这个脚本。这些脚本在指定时间运行,但仅备份部分文件夹。具体来说,它仅备份 apache 文档根目录和 public_html。相应的电子邮件说:
250M /home/teohaik/backups/2014-11-29/Apache_Doc_Root.tar.gz
487M /home/teohaik/backups/2014-11-29/public_html.tar.gz
737M total
对此有什么想法吗?
先感谢您!
答案1
查看您的 cron 配置文件。
也许您的 cron 作业以特定用户身份运行,该用户无法访问所有内容。
编写日志文件也是一个聪明的主意,这样您就可以看到脚本在 cron 运行时到底做了什么。