shell 脚本备份

2022-06-14 21:30:53 浏览数 (1)

尝试写了一个 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): "

0 人点赞