前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell 脚本备份

shell 脚本备份

作者头像
上山打老虎了
发布2022-06-14 21:30:53
6700
发布2022-06-14 21:30:53
举报
文章被收录于专栏:Article

尝试写了一个 shell 脚本来备份站点,话说之前要么是懒得备份,要么是人肉备份,自从另一台vps被黑了之后,也是长记性了,虽然写的不咋地,但备份一下文件,还是绰绰有余的。

代码语言:javascript
复制
#!/bin/bash

clear
echo "+------------------------------------------------------------------------+"
echo "|          Welcome to use this shell script to backup your lnmp site        |"
echo "+------------------------------------------------------------------------+"
echo "|                      Follow tha tips and enjoy it                         |"
echo "+------------------------------------------------------------------------+"
echo "|          For more information please visit https://www.noxxxx.com         |"
echo "+------------------------------------------------------------------------+"


need_compress=(
    '/home/wwwroot'    #需要备份的目录
    '/home/wwwlogs'
)
backupDir='/home/backup'

if [ ! -d "$backupDir" ]; then
    mkdir "$backupDir"
    echo "mkdir on $backupDir"
else
    echo "[ $backupDir ] has been created please check it otherwise the backup file will overwrite the same name file"
    exit
fi

for i in ${need_compress[@]} 
do
    sleep 1

    name=`echo $i | awk -F "/" '{print $NF}'`
    cd $i && cd ../
    echo "starting compress direction"
    tar -czf ${name}".tar.gz" $name 

    echo "move tar.gz file to"${backupDir}
    mv ${name}".tar.gz" $backupDir
done

DB_HOSTNAME="localhost"              #mysql所在主机的主机名
DB_USERNAME="root"                   #mysql登录用户名
DB_PASSWORD="root"                 #mysql登录密码
DATABASES=(
            "pic"                    #备份的数据库名
)


for DATABASE in ${DATABASES[@]};do
    echo "[+] Mysql-Dumping: ${DATABASE}"
    echo -n "   Began:  ";echo $(date)
    if $(mysqldump -h ${DB_HOSTNAME} -u${DB_USERNAME} -p${DB_PASSWORD} ${DATABASE} > "${backupDir}/${DATABASE}.sql");then
        echo "  Dumped successfully!"
    else
        echo "  Failed dumping this database!"
    fi
        echo -n "   Finished: ";echo $(date)
done


# echo "export mysql database--> $database"

# mysqldump -u $user -p $password $database > ${database}".sql"


# read -p "Enter the absolute path (default /home/wwwroot/): " path
# echo "your path is: $path"
# read -p "Continue to enter more path (y/n): "
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年07月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档