备份脚本
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#!/bin/bash #Author:ZhangGe #Des:Backup database and webfile. #Date:2014-8-28 TODAY=`date +%u` if [ -z $1 ];then echo Needed Usage arguments. Please Use --help to get more infomation. exit 1 fi test -f /etc/profile && . /etc/profile >/dev/null 2>&1 zip --version >/dev/null || yum install -y zip ZIP=$(which zip) MYSQLDUMP=$(which mysqldump) if [ "$1" == "db" ];then domain=$2 dbname=$3 mysqluser=$4 mysqlpd=$5 back_path=$6 test -d $back_path || (mkdir -p $back_path || echo "$back_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2) cd $back_path $MYSQLDUMP -u$mysqluser -p$mysqlpd $dbname --skip-lock-tables>$back_path/$domain\_db_$TODAY\.sql test -f $back_path/$domain\_db_$TODAY\.sql || (echo "MysqlDump failed! Please CheckOut Or feedback to zhangge.net..." && exit 2) $ZIP -Pmypassword -m $back_path/$domain\_db_$TODAY\.zip $domain\_db_$TODAY\.sql elif [ "$1" == "file" ];then domain=$2 site_path=$3 back_path=$4 test -d $site_path || (echo "$site_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2) test -d $back_path || (mkdir -p $back_path || echo "$back_path not found! Please CheckOut Or feedback to zhangge.net..." && exit 2) test -f $back_path/$domain\_$TODAY\.zip && rm -f $back_path/$domain\_$TODAY\.zip $ZIP -Pmypassword -9r $back_path/$domain\_$TODAY\.zip $site_path elif [ "$1" == "--help" ];then clear echo =====================================Help infomation========================================= echo 1. Use For Backup database: echo The \$1 must be \[db\] echo \$2: \[domain\] echo \$3: \[dbname\] echo \$4: \[mysqluser\] echo \$5: \[mysqlpassword\] echo \$6: \[back_path\] echo echo For example:./backup.sh db zhangge.net zhangge_db zhangge 123456 /home/wwwbackup/zhangge.net echo echo 2. Use For Backup webfile: echo The \$1 must be [\file\]: echo \$2: \[domain\] echo \$3: \[site_path\] echo \$4: \[back_path\] echo echo For example:./backup.sh file zhangge.net /home/wwwroot/zhangge.net /home/wwwbackup/zhangge.net echo =====================================End of Hlep============================================== exit 0 else echo "Error!Please Usage --help to get help infomation!" exit 2 fi |
使用说明
1、将代码中的 2 处 mypassword 改成你自定义的压缩密码(确保数据安全),并保存为 backup.sh 之后,执行 chmod +x backup.sh 赋执行权限。
2、相关命令如下:
参见 –help
1 2 |
./backup.sh db 文件名 数据库名 数据库用户名 数据库密码 备份路径 ./backup.sh file 文件名 网站路径 备份路径 |
加入计划任务
安装crontab
1 2 3 4 5 |
yum install vixie-cron yum install crontabs /bin/systemctl restart crond.service #启动服务 /bin/systemctl reload crond.service #重新载入配置 /bin/systemctl status crond.service #查看crontab服务状态 |
设为开机启动
为了方便我们可以把crontab设置成开机自启 在 /etc/rc.d/rc.local 脚本中加