幸福

如人饮水,冷暖自知

CentOS下自动备份db与web的shell脚本

backup_dbfile.sh

作用:备份数据库文件

#!/bin/sh
DATE=`date +’%Y-%m-%d’`
backup_dir=”/backupfile/dbfile”
db_dir=”/usr/local/mysql”
if [ ! -d $backup_dir/$DATE ]; then
mkdir -p $backup_dir/$DATE
fi
users=”root”
for user in ${users}; do
cd $db_dir
tar zcvf $backup_dir/$DATE/db1_$DATE.tar.gz db1
if [ -e $backup_dir/$DATE/db1_$DATE.tar.gz ]; then
echo ” `date ‘+%Y-%m-%d %H:%M:%S’` the db file backup is OK ” >> /backupfile/log/db_file_log
else
echo ” `date ‘+%Y-%m-%d %H:%M:%S’` the db file backup is ERROR ” >> /backupfile/log/db_file_log
fi
done

 

backup_dbsql.sh

作用:备份数据库导出SQL文件

#!/bin/sh
DATE=`date +’%Y-%m-%d’`
backup_dir=/backupfile/dbfile
if [ ! -d $backup_dir/$DATE ]; then
mkdir -p $backup_dir/$DATE
fi
mysqldump -h localhost -u root -pabcdef db1>$DATE.sql
mv $DATE.sql $backup_dir/$DATE

if [ -e $backup_dir/$DATE/$DATE.sql ]; then
echo ” `date ‘+%Y-%m-%d %H:%M:%S’` the db sql backup is OK ” >> /backupfile/log/db_sql_log
else
echo ” `date ‘+%Y-%m-%d %H:%M:%S’` the db sql backup is ERROR ” >> /backupfile/log/db_sql_log
fi

 

backup_web.sh

作用:备份web项目

#!/bin/sh
DATE=`date +’%Y-%m-%d’`
backup_dir=”/backupfile/webfile”
web_dir=”/usr/local/html/web”
if [ ! -d $backup_dir/$DATE ]; then
mkdir -p $backup_dir/$DATE
fi
users=”root”
for user in ${users}; do
cd $web_dir
tar zcvf $backup_dir/$DATE/web1_$DATE.tar.gz web1
if [ -e $backup_dir/$DATE/web1_$DATE.tar.gz ]; then
echo ” `date ‘+%Y-%m-%d %H:%M:%S’` the web file backup is OK ” >>/backupfile/log/web_log
else
echo ” `date ‘+%Y-%m-%d %H:%M:%S’` the web file backup is ERROR ” >>/backupfile/log/web_log
fi
done

添加计划任务

[root@db /]# crontab -e
0 23 * * * /myshell/backup_dbfile.sh 
0 23 * * * /myshell/backup_dbsql.sh
0 23 * * * /myshell/backup_web.sh

 

 



去打赏

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

点赞

发表评论

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