[工具] Shell 将远程服务器的 LNMP 备份在本地复原

介绍

基本信息

作者:朱明宇
名称:将远程服务器的 LNMP 备份还原到本地
作用:将远程服务器的 LNMP 备份还原到本地

使用方法

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

脚本分割线里的变量

1. tmppath=/cache #本地用于备份数据的目录
2. webpath=/usr/share/nginx/html #本地用于存放网站文件的目录
3. key=”~/.ssh/eternalcenter” #本地私钥
4. tmpfile=tmpfile.txt #用于存储记录的文件
5. dbuser=ec #网站在数据库中的用户
6. ruser=eternalcenter #用于远程服务器的用户
7. rhost=eternalcenter.com #远程服务器
8. rcache=”/cache” #远程服务器用于备份数据的目录

注意

1. 本地需要已经搭建好 LNMP 平台
2. 用于远程服务器的用户,需要能免密钥 ssh 远程服务器,且对于本地用于备份数据的目录和远程服务器用于备份数据的目录拥有读和执行的权限
3. 执行此脚本的用户需要有 sudo systemctl 权限
4. 脚本 ”mysql -uroot -p’eternalcenter’ ec < $sqlfile“ 中 “eternalcenter“ 是指本地 MariaDB 数据库 root 用户的密码,需要修改成本地 MariaDB 数据库的 root 用户密码

脚本

#!/bin/bash

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

tmppath=/cache
webpath=/usr/share/nginx/html
key="~/.ssh/eternalcenter"
tmpfile=tmpfile.txt
dbuser=ec

ruser=eternalcenter
rhost=eternalcenter.com
rcache="/cache"

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

nowdirectory=`pwd`

a=`ssh -i $key $ruser@$rhost "du -s $rcache" | awk '{print $1}'`
sleep 10
b=`ssh -i $key $ruser@$rhost "du -s $rcache" | awk '{print $1}'`

if [ $a -eq 0 ];then
	echo "no file"
fi

if [ $a -ne $b ];then
        echo "backup is running now"
        exit
fi

c=0

if [ -f $tmpfile ];then
        c=`cat $tmpfile`
fi

if [ $a -eq $c ];then
        echo "no new file"
        exit
fi

echo $a > $tmpfile

sqlfile=`ssh -i $key $ruser@$rhost "ls -rtlh $rcache | grep sql | tail -1" | awk '{print $NF}'`
if [ $? -eq 0 ]; then

        tarfile=`ssh -i $key $ruser@$rhost "ls -rtlh $rcache | grep tar | tail -1" | awk '{print $NF}'`
        if [ $? -eq 0 ]; then

                rm -rf $tmppath/*
                mkdir $tmppatch &> /dev/null

                echo $sqlfile
                echo $tarfile

                scp -i $key $ruser@$rhost:$rcache/$sqlfile $tmppath
                scp -i $key $ruser@$rhost:$rcache/$tarfile $tmppath

#                sudo systemctl stop nginx
#                sudo systemctl stop php-fpm

                cd $tmppath

                mysql -uroot -p'eternalcenter' -e "drop database $dbuser;"
                mysql -uroot -p'eternalcenter' -e "create database $dbuser;"
                mysql -uroot -p'eternalcenter' -e "grant all privileges on $dbuser.* to \"$dbuser\"@\"localhost\";"
                mysql -uroot -p'eternalcenter' ec < $sqlfile

                sudo rm -rf $webpath/*
                sudo tar -zxvf $tarfile -C $webpath/ &> /dev/null

#                sudo systemctl start nginx
#                sudo systemctl start php-fpm

                cd $nowdirectory

        fi
fi

[工具] Shell LNMP 没运行则重启系统 (systemctl 版)

介绍

基本信息

作者:朱明宇
名称:LNMP 没运行则重启系统
作用:LNMP 没运行则重启系统

使用方法

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

脚本

#!/bin/bash
  
systemctl status nginx | grep 'active (running)'
if [ $? -ne 0 ];then
        /usr/sbin/reboot
fi

systemctl status mariadb | grep 'active (running)'
if [ $? -ne 0 ];then
        /usr/sbin/reboot
fi

systemctl status php-fpm | grep 'active (running)'
if [ $? -ne 0 ];then
        /usr/sbin/reboot
fi

[命令] Linux 命令 uniq (文件里字符重复的管理)

案例一:uniq 命令去重

# cat test.txt 
a1
b2
c3
a1
d2
e3
a1
c3

# sort test.txt  | uniq
a1
b2
c3
d2
e3

(补充:这里以给 test.txt 文件里的字符为例)

案例二:uniq 只显示重复的行

# cat test.txt 
a1
b2
c3
a1
d2
e3
a1
c3

# sort test.txt  | uniq -d
a1
c3

(补充:这里以只显示 test.txt 文件里重复的行为例)

案例三:uniq 只显示不重复的行

# cat test.txt 
a1
b2
c3
a1
d2
e3
a1
c3

# sort test.txt  | uniq -u
b2
d2
e3

(补充:这里以只显示 test.txt 文件里不重复的行为例)

案例四:显示每个字符出现的次数

# cat test.txt 
a1
b2
c3
a1
d2
e3
a1
c3

# sort test.txt  | uniq -c
      3 a1
      1 b2
      2 c3
      1 d2
      1 e3

(补充:这里以显示 test.txt 每个字符出现的次数为例)

[步骤] Linux 加密压缩 (tar 版)

步骤一:创建测试文件

# touch test.txt

(补充:这里以创建 test.txt 文件为例)

步骤二:加密压缩文件或目录

2.1 交互式加密压缩文件或目录

# tar -zcf - test.txt | openssl des3 -salt | dd of=test1.tar.gz
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.
0+1 records in
0+1 records out
224 bytes copied, 7.04902 s, 0.0 kB/s


补充:
1) 这里以将 test.txt 文件加密压缩成 test1.tar.gz (压缩)包为例
2) 如果要以 bzip2 的格式进行压缩,则将命令中的 -zcf 换成 -jcvf 将 test1.tar.gz 换成 test1.tar.bz2
3) 如果要以 xz 的格式进行压缩,则将命令中的 -zcf 换成 -Jcvf 将 test1.tar.gz 换成 test1.tar.xz

2.2 非交互式加密压缩文件或目录

# tar -zcf - test.txt | openssl des3 -salt -f eternalcenter | dd of=test2.tar.gz
des3: Unrecognized flag f
des3: Use -help for summary.
0+0 records in
0+0 records out
0 bytes copied, 0.00376576 s, 0.0 kB/s


补充:
1) 这里以将 test.txt 文件加密压缩成 test1.tar.gz (压缩)包并且将密码设置为 eternalcenter 为例
2) 如果要以 bzip2 的格式进行压缩,则将命令中的 -zcf 换成 -jcvf 将 test1.tar.gz 换成 test2.tar.bz2
3) 如果要以 xz 的格式进行压缩,则将命令中的 -zcf 换成 -Jcvf 将 test1.tar.gz 换成 test2.tar.xz

步骤三:解压加密文件或目录

3.1 交互式解压加密文件或目录

3.1.1 删除原测试目录和里面的文件
# rm -rf test.txt

(补充:这里以删除 test.txt 文件为例)

3.1.2 交互式解压加密文件或目录
# dd if=test2.tar.gz | openssl des3 -d | tar zxf -
0+1 records in
0+1 records out
224 bytes copied, 0.000589721 s, 380 kB/s
enter des-ede3-cbc decryption password:
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.


补充:
1) 这里以解压 test2.tar.gz (压缩)包为例
2) 如果是 bzip2 格式的(压缩)包,则将命令中的 -zxf 换成 -jcvf 将 test1.tar.gz 换成 test1.tar.bz2
3) 如果是 xz 格式的(压缩)包,则将命令中的 -zxf 换成 -Jcvf 将 test1.tar.gz 换成 test1.tar.xz

3.2 非交互式解压加密文件或目录

3.2.1 删除原测试目录和里面的文件
# rm -rf test.txt

(补充:这里以删除 test.txt 文件为例)

3.2.2 非交互式解压加密文件或目录
# dd if=test1.tar.gz | openssl des3 -d -k eternalcenter | tar zxf -
0+1 records in
0+1 records out
224 bytes copied, 0.000574539 s, 390 kB/s
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.


补充:
1) 这里以解压 test1.tar.gz (压缩)包并且解压密码为 eternalcenter 为例
2) 如果是 bzip2 格式的(压缩)包,则将命令中的 -zxf 换成 -jcvf 将 test1.tar.gz 换成 test1.tar.bz2
3) 如果是 xz 格式的(压缩)包,则将命令中的 -zxf 换成 -Jcvf 将 test1.tar.gz 换成 test1.tar.xz