[生产] Shell 将网站的全量数据备份到本电脑并运行 (Mac 版)

介绍

基本信息

作者:朱明宇
名称:将网站的全量数据备份到本电脑并运行(Mac 版)
作用:将一台 Web 服务器上的网页文件和数据库数据全量备份到本地 Mac 主机,再通过本地的 Mac 主机 Web 服务将它跑起来

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本
4. 可以设置定时任务让他定期自动执行,也就是(sudo crontab -e)

脚本分割线里的变量

1. user=root #登录 Web 服务器的用户,请确保这个用户有创建缓存备份目录的权限
2. ip=8.8.8.8 # Web 服务器的 IP
3. dbadmin=root #Web 服务器备份 MariaDB 数据库的数据库用户
4. dbpassword=”‘123456′” #Web 服务器备份MariaDB 数据库的用户的密码
5. path=/backup #本地主机放置备份的目录
6. filename=backup #备份文件的中间名
7. cache=/cache #Web服务器用来缓存备份的目录,请确保这个目录没有其他用途,且能被登录 Web 服务器的用户创建
8. html=/web-file #本地主机存放网页文件的目录,请确保当前的 Mac 系统用户对他有写和执行权限否则请更改网页文件的目录

注意

1. 此脚本执行前必须要先保证执行脚本的主机能无秘钥远程这台 Web 服务器
2. 请保证本机的 MariaDB 并没有设置密码
3. 此脚本执行前必须要先保证执行脚本的主机 MAMP 平台已经搭建好了
4. 本地 MairaDB 的密码为空
5. 此脚本备份的网页文件是 WordPress

脚本

#!/bin/bash

####################### Separator ########################

user=root
ip=8.8.8.8
dbadmin=root
dbpassword="'123456'"
path=/backup
filename=backup
cache=/cache
html=/web-file

####################### Separator ########################

date=$(date +%Y-%m-%d-%H)

ping -c3 -i0.8 $ip > /dev/null

if [ $? -eq 0 ];then

        ssh $user@$ip "mkdir $cache &> /dev/null"
        ssh $user@$ip "mysqldump -u$dbadmin -p"$dbpassword" -A > $cache/$filename-$date.sql"
        ssh $user@$ip "cd /usr/local/nginx/html/
        tar -cvf $cache/$filename-$date.tar.gz .[!.]* * &> /dev/null"

        mkdir -p $path/$date &> /dev/null
        scp $user@$ip:$cache/$filename-$date.sql $path/$date
        scp $user@$ip:$cache/$filename-$date.tar.gz $path/$date
        ssh $user@$ip "rm -rf $cache\*"

fi

rm -rf $html/*
tar -xvf $path/$date/$filename-$date.tar.gz -C $html/
mysql -u$dbadmin < $path/$date/$filename-$date.sql
rm -rf $html/index.html

sed -i "" s/"'DB_USER', '.*'"/"'DB_USER', 'root'"/ $html/wp-config.php
sed -i "" s/"'DB_PASSWORD', '.*'"/"'DB_PASSWORD', ''"/ $html/wp-config.php
sed -i "" s/"'DB_HOST', '.*'"/"'DB_HOST', '127.0.0.1:3306'"/ $html/wp-config.php
mysqladmin -uroot password ''

[娱乐] Shell 问候

介绍

基本信息

作者:朱明宇
名称:问候
作用:问候当前的登录用户并且显示时间

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本
3. 可以设置开机运行此脚本

脚本

#!/bin/bash

time=`date +%H`

if [ $time -lt 10 ];then
        z="早上好,$USER"
elif [ $time -ge 10 -a $time -lt 13 ];then
        z="中午好,$USER"
elif [ $time -ge 13 -a $time -lt 18 ];then
        z="下午好,$USER"
else
        z="晚上好,$USER"
fi

echo -e "\033[34m$z\033[0m"
echo -e "当前的时间是`date +%Y-%m-%d-%H-%M-%S`"

[娱乐] Shell 病毒 (无法被杀死)

介绍

基本信息

作者:朱明宇
名称:病毒(无法被杀死)
作用:产生一个永远运行不会被杀死的进程,除非关机

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本

脚本

#!/bin/bash

trap 'echo ;echo -e "\033[35m你杀不死我的,没有人可以杀死我,哇哈哈!";sleep 5;

for i in {1..10}
do
        echo -e "\033[34m我又开始运行了,哇哈哈!\033[0m"
done ;sleep 2' 2

while :
do
        for i in `seq 1 3`
        do
                echo -en "\033[3$[i]m 我是病毒,我将会永远运行,哇哈哈!     \033[0m"
                sleep 0.01
        done
        echo
done

[工具] Shell 批量实现其他多台电脑可以免密码 SSH 本电脑

介绍

基本信息

作者:朱明宇
名称:批量实现其他多台电脑可以免密码 SSH 本电脑
作用:批量将其他多台电脑的公钥拷贝给本电脑

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

1. nm=192.168.4.0 #网段,网段必须为 C 类网段,请保证格式和前 3 个网络位一定正确
2. sip=51 #起始 ip,起始 IP 地址,IP 地址 的范围是 0-255
3. lip=51 #结束 ip ,结束 IP 地址,IP 地址 的范围是 0-255
4. pd=123456 #其他服务器的 root 远程登录密码
5. lpd=Taren1 #本机的 root 远程登录密码
6. lh=192.168.4.254 #本机 IP 地址

注意

1. 此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器
2. 必须以 root 用户执行本脚本

脚本

#!/bin/bash

####################### Separator ########################

nm=192.168.4.0 #Network segment. The network segment must be a class C network segment. Please ensure that the format and the first three network bits are correct
sip=51 #Starting IP, starting IP address. The range of IP address is 0-255
lip=51 #End IP, end IP address. The range of IP address is 0-255
pd=123456 #Root remote login password of other servers
lpd=Taren1 #Root remote login password of this machine
lh=192.168.4.254 #Native IP address

####################### Separator ########################

fnm=${nm%.*}

set timeout 3
rpm -q expect

if [ $? -ne 0 ];then
        yum -y install expect &> /dev/null
fi

for i in `seq $sip $lip`
do
        expect << EOF
        spawn ssh root@$fnm.$i
        expect "(yes/no)?"                               {send "yes\r" } 
        expect "password:"                               {send "$pd\r" } 
        expect "#"                                       {send "ssh-keygen\r"}
        expect "(/root/.ssh/id_rsa):"                    {send "\r"}
        expect "(empty for no passphrase):"              {send "\r"}
        expect "passphrase again:"                       {send "\r"}
        expect "#"                                       {send "\r"}
        EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "password:"                               {send "$pd\r" } 
expect "#"                                       {send "ssh-keygen\r"}
expect "(/root/.ssh/id_rsa):"                    {send "\r"}
expect "(empty for no passphrase):"              {send "\r"}
expect "passphrase again:"                       {send "\r"}
expect "#"                                       {send "\r"}
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "(yes/no)?"                               {send "yes\r" } 
expect "#"                                       {send "ssh-keygen\r"}
expect "(/root/.ssh/id_rsa):"                    {send "\r"}
expect "(empty for no passphrase):"              {send "\r"}
expect "passphrase again:"                       {send "\r"}
expect "#"                                       {send "\r"}
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "#"                                       {send "ssh-keygen\r"}
expect "(/root/.ssh/id_rsa):"                    {send "\r"}
expect "(empty for no passphrase):"              {send "\r"}
expect "passphrase again:"                       {send "\r"}
expect "#"                                       {send "\r"}
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "password:"                      { send "$pd\r" } 
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "(yes/no)?"                      { send "yes\r" } 
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "password:"                      { send "$pd\r" } 
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "(yes/no)?"                      { send "yes\r" } 
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

echo "$fnm.$i has been finished"

done

[工具] Shell 批量实现本电脑免密码 SSH 多个服务器

介绍

基本信息

作者:朱明宇
名称:批量实现本电脑免密码 SSH 多个服务器
作用:将主本机的 SSH 公钥批量拷贝给其他多个远程服务器

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

1. nm=192.168.4.0 #网段,网段必须为 C 类网段,请保证格式和前 3 个网络位一定正确
2. sip=50 #起始 IP 地址,IP 地址 的范围是 0-255
3. lip=57 #结束 IP 地址,IP 地址 的范围是 0-255
4. pd=123456 #其他服务器的 root 远程登录密码

脚本

#!/bin/bash

####################### Separator ########################

nm=192.168.4.0
sip=50
lip=57
pd=123456

####################### Separator ########################

fnm=${nm%.*}

set timeout 3
rpm -q expect

if [ $? -ne 0 ];then
        yum -y install expect &> /dev/null
fi

cat /root/.ssh/id_rsa &> /dev/null

if [ $? -ne 0 ];then

        expect << EOF
        spawn ssh-keygen
        expect "(/root/.ssh/id_rsa):"                    {send "\r"}
        expect "(empty for no passphrase):"              {send "\r"}
        expect "passphrase again:"                       {send "\r"}
        expect "#"                                       {send "\r"}
        EOF

fi

for i in `seq $sip $lip`
do
        echo $fnm.$i
        ping -c3 -i0.3 -w1 $fnm.$i &> /dev/null
        if [ $? -ne 0 ];then
                echo "$fnm.$i can't be connected"
                continue
        fi

        expect << EOF
        spawn ssh-copy-id $fnm.$i
        expect "? "                                      {send "yes\r"}
        expect "password:"                               {send "$pd\r"}
        expect "#"                                       {send "\r"}
        EOF

        expect << EOF
        spawn ssh-copy-id $fnm.$i
        expect "password:"                               {send "$pd\r"}
        expect "#"                                       {send "\r"}
        EOF

        echo "$fnm.$i has been finished"
done