[工具] Shell 将同目录下最新的某个目录里的所有文件替换到 GitHub 库里

介绍

基本信息

作者:朱明宇
名称:将同目录下最新的某个目录里的所有文件替换到 GitHub 库里
作用:将同目录下最新的某个目录里的所有文件替换到 GitHub 库里

使用方法

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

脚本分割线里的变量

1. directory=download-eternalcenter #本地的缓冲目录
2. gituser=mingyuzhu #GitHub 用户
3. gitemail=mingyu.zhu@eternalcenter.com #GitHub 邮箱
4. gitrepository=download-eternalcenter #GitHub 库
5. gitbranch=’master’ #GitHub 库的分支
6. backupfile=all #备份后的文件
7. keyword=clone #同目录下要备份目录名称的关键字

注意

需要提前安装 git,注册 GitHub,创建相应的 GitHub 库,并且创建和设置了对应的 ssh 密钥

脚本

#!/bin/bash

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

directory=download-eternalcenter
gituser=mingyuzhu
gitemail=mingyu.zhu@eternalcenter.com
gitrepository=download-eternalcenter
gitbranch='master'
backupfile=all
keyword=clone

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

backupdirectory=`ls -rtlh | grep $keyword | awk '{print $NF}' | tail -1`

sqlfile=`ls $backupdirectory | grep sql`
tarfile=`ls $backupdirectory | grep tar`

rm -rf $directory
mkdir -p $directory &> /dev/null

echo $gituser
git config --global user.email "$gitemail"
git config --global user.name "$gituser"

rm -rf $directory
mkdir -p $directory &> /dev/null
cd $directory
git init
git remote add origin git@github.com:$gituser/$gitrepository
git pull --rebase origin $gitbranch -f
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *' --prune-empty --tag-name-filter cat -- --all
git commit -m 'cleapup'
git push -u origin $gitbranch -f
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now

cd ..

rm -rf $directory
mkdir -p $directory &> /dev/null
cd $directory
git init
git remote add origin git@github.com:$gituser/$gitrepository
git pull --rebase origin $gitbranch -f
git rm *
git commit -m 'cleapup'

cd ../$backupdirectory/
tar -zcvf ../$directory/$backupfile.tar.gz *
cd ../$directory

git add *
git status
git commit -m 'upload'
git push -u origin $gitbranch -f
cd ..

[功能] HTML 下载链接

<html>
<body>
<a href="eternalcenter-2021.txt" download="eternalcenter.txt">Download test.txt</a>
</body>
</html>

(补充:这里以下载和此 html 文件在同目录下的 eternalcenter-2021.txt 文件,且下载命名为 eternalcenter.txt 为例)

[工具] 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

[FUN] HTML Mingyu Zhu’s Personal Web Page

中文

Code

<html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<head>

<title>Mingyu Zhu</title>

</head>

<body>

<div style="position:absolute; width:100%; height:100%; z-index:-1; left:0; top:0;"> 
<img src="https://filedn.eu/ltLpz9YGUS2hi1pJmklNfDb/en_us/dimfigure.JPG" height="100%" width="100%" style="position:absolute;left:0; top:0;"> 
</div>

<div style="width:300px;height:50px;float:left"> 
<form action="http://www.google.com/search" method="get"> 
<input type="text" name="q" size="20" maxlength="255" value="" /> 
<input type="submit" name="btnG" value="Google" /> 
</form>
<a href="https://zhu-mingyu.github.io" style="right: 15px; position: absolute; font-size:25px;color:red">中文 (GitHub)</a>
<a href="https://zhumingyu.com" style="right: 215px; position: absolute; font-size:25px;color:red">中文</a>
</div> 

<div style="left: 8px; position: absolute; top: 75px;font-size:45px;">Mingyu Zhu's Personal Web Page</div>

<div style="left: 800px; position: absolute; top: 300px;font-size:15px;">"Hope everyone can achieve self achievement and self happiness fairly."</div>

<a href="http://eternalcenter.com" target="_blank" style="left: 8px; position: absolute; top: 185px;font-size:20px;color:black">Eternal Center (eternalcenter.com), Mingyu Zhu's personal website</a>
<a href="https://e.pcloud.link/publink/show?code=kZukhJZUhQq66Fxs0yg5rL1LsAgrSD3ytMk" target="_blank" style="left: 8px; position: absolute; top: 215px;font-size:20px;color:black">Mingyu Zhu's personal website data for cloning</a>
<a href="https://github.com/eternalcenter-now/eternalcenter-now" target="_blank" style="left: 8px; position: absolute; top: 245px;font-size:20px;color:black">Mingyu Zhu's personal website code for cloning</a>
<a href="http://static.eternalcenter.com" target="_blank" style="left: 8px; position: absolute; top: 275px;font-size:20px;color:black">Effect display page of Mingyu Zhu's personal website</a>
<a href="https://eternalcenter-now.github.io" target="_blank" style="left: 8px; position: absolute; top: 305px;font-size:20px;color:black">Effect display page of Mingyu Zhu's personal website (GitHub)</a>
<a href="https://e.pcloud.link/publink/show?code=kZrJhJZbVIBf8jmVFzumvaYnEwPlLLc34DX" target="_blank" style="left: 8px; position: absolute; top: 335px;font-size:20px;color:black">Public personal data of Mingyu Zhu</a>

</body>

<html/>

Background Picture