Linux shell命令 批量删除子目录下的文件,保留指定数量的最新文件
[ 2023/07/26 16:26 | by 刘新修 ]
C#代码
- #!/bin/bash
- #保留文件数
- ReservedNum=2
- #要删除文件的,父级目录
- FileDir=/usr/docker/springCloud/project/
- #循环子目录列表
- for element in `ls $FileDir`
- do
- # 拼接成完成目录 (父目录路径/子目录名)
- dir_or_file=$FileDir$element
- # 获取子目录下的指定文件格式的总数量
- FileNum=$(ls -l $dir_or_file | grep 'keda-' |wc -l)
- # 输出 删除前的 指定格式的文件数量
- echo $element "jar num:" $FileNum
- # 如果 数量大于保留数量,那就循环删除 文件
- while(( $FileNum > $ReservedNum))
- do
- OldFile=$(ls -rt $dir_or_file | grep 'keda-'| head -1)
- echo $element "Delete jar:"$OldFile
- rm -rf $dir_or_file/$OldFile
- let "FileNum--"
- done
- done
- # 删除完后,再循环输出一下 每个子目录下的文件数量
- for element in `ls $FileDir`
- do
- dir_or_file=$FileDir$element
- FileNum=$(ls -l $dir_or_file | grep 'keda-' |wc -l)
- echo $element "jar num:" $FileNum
- done
纯前端让浏览器下载pdf文件
[ 2023/07/18 10:20 | by 刘新修 ]
XML/HTML代码
- <!--
- * @Description: npage.vue
- * @Version: 1.0
- * @Author: LiuXia
- * @Date: 2022-08-01 20:32:06
- * @LastEditors: Jesse Liu
- * @LastEditTime: 2023-07-17 20:38:42
- -->
- <template>
- <div class="wscn-http404-container">
- <h1 style="line-height:300px; text-align:center">npage.vue (新测试文件)</h1>
- <div>time:</div>
- </div>
- </template>
- <script>
- import { parseTime } from '@/utils'
- import { downloadFile } from "@/utils/getFile";
- import { fileLinkToStreamDownload , urlFile } from '@/utils/down'
- export default {
- name: 'PageTest',
- computed: {
- message() {
- return 'PageTest'
- },
- parseTime: parseTime
- },
- methods:{
- downfile(filePath){
- //https://bbwx.kaitaiming.com/image/group1/M00/24/AE/rBBlxWS0vaqAR8ltAAGsWlc-FnY677.pdf
- // 后端上传文件返回请求的filePath地址
- console.log( filePath, 3333333 );
- // 获取文件名
- let fileName = urlFile(filePath, 1);
- // 获取文件后缀
- let fileExtension = urlFile(filePath, 2);
- // url下载
- fileLinkToStreamDownload(filePath,fileName,fileExtension);
- }
- },
- mounted() {
- this.downfile('http://localhost:9527/image/group1/M00/24/AE/rBBlxWS0vaqAR8ltAAGsWlc-FnY677.pdf');
- this.$store.dispatch("case/casesList/downloadFile",{}).then((res)=>{
- let data = res.data;
- let headers = res.headers;
- /*** 调用下载文件 ***/
- //downloadFile(data, headers)
- })
- },
- }
- </script>
- <style lang="scss" scoped>
- </style>
JavaScript代码
- /*
- * @Description: down.js
- * @Author: Jesse Liu
- * @Date: 2023-02-06 16:27:18
- * @LastEditors: Jesse Liu
- * @LastEditTime: 2023-07-17 20:20:05
- */
- export function fileLinkToStreamDownload(url, fileName, type) {
- let reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$/;
- if (!reg.test(url)) {
- throw new Error("传入参数不合法,不是标准的文件链接");
- } else {
- let xhr = new XMLHttpRequest();
- xhr.open('get', url, true);
- xhr.setRequestHeader('Content-Type', `application/${type}`);
- xhr.responseType = "blob";
- xhr.onload = function () {
- if (this.status == 200) {
- //接受二进制文件流
- console.log(this)
- var blob = this.response;
- const blobUrl = window.URL.createObjectURL(blob);
- // 这里的文件名根据实际情况从响应头或者url里获取
- const a = document.createElement('a');
- a.href = blobUrl;
- a.download = fileName;
- a.click();
- window.URL.revokeObjectURL(blobUrl);
- }
- }
- xhr.send();
- }
- }
- // 获取url中需要的数据 type 1: 获取文件名 2:获取后缀 3:获取文件名+后缀 4:获取文件前缀
- export function urlFile(url, type) {
- let filename = url.substring(url.lastIndexOf('/') + 1)
- switch (type){
- case 1: return filename; break;
- case 2: return filename.substring(filename.lastIndexOf(".") + 1); break;
- case 3: return filename.substring(0, filename.lastIndexOf(".")); break;
- case 4: return url.substring(0, url.lastIndexOf('/') + 1)
- }
- }
SHELL 获取目录
C#代码
- ### 提取目录中的名称
- las_dir=$(basename "$PWD")
- cur_dir=$(dirname $(pwd))
- lat_dir=$(basename "$cur_dir")
docker自动导入镜像
[ 2023/06/03 11:40 | by 刘新修 ]
vi /sh/scp-docker.sh
#### 同步远程的docker镜像自动装载
C#代码
- cd /data/backup &&
- scp root@172.16.207.22:/data/backup/* ./ &&
- docker stop nginx-nodejs-redis:v2b &&
- docker rm nginx-nodejs-redis:v2b &&
- docker stop jenkins-rsync:v2b &&
- docker rm jenkins-rsync:v2b &&
- docker rmi nginx-nodejs-redis:v2b &&
- docker rmi jenkins-rsync:v2b &&
- docker image load -i jenkins-rsync-v2b.tgz &&
- docker image load -i nginx-nodejs-redis-v2b.tgz &&
- rm -rf /data/apps &&
- mkdir -p /data/apps/nginx/htdocs &&
- mkdir -p /data/apps/redis/htdocs &&
- mkdir -p /data/apps/rsync/www &&
- mkdir -p /data/apps/nodejs/htdocs/nest/KTM/dist &&
- mkdir -p /data/apps/nodejs/htdocs/nest/KTM/public &&
- mkdir -p /data/apps/nodejs/htdocs/nest/do_nest/public/www &&
- mkdir -p /data/apps/nodejs/htdocs/nest/other &&
- docker run \
- -u root \
- -p 80:80 \
- -p 8080:80 \
- -p 3000:3000 \
- -p 6379:6379 \
- -v /data/apps/nginx/htdocs:/data/nginx/htdocs \
- -v /data/apps/redis/htdocs:/data/redis/htdocs \
- -v /data/apps/rsync:/data/rsync \
- -v /data/apps/nodejs/htdocs/nest/KTM/dist:/data/nodejs/htdocs/nest/KTM/dist \
- -v /data/apps/nodejs/htdocs/nest/KTM/public:/data/nodejs/htdocs/nest/KTM/public \
- -v /data/apps/nodejs/htdocs/nest/do_nest/public/www:/data/nodejs/htdocs/nest/do_nest/public/www \
- -v /data/apps/nodejs/htdocs/nest/other:/data/nodejs/htdocs/nest/other \
- --privileged=true \
- --name=nginx-nodejs-redis-v2b \
- -d --restart=always nginx-nodejs-redis:v2b &&
- docker run -p 4440:8080 -p 5000:5000 -p 8730:873 \
- -u root \
- --name=jenkins-rsync-v2b \
- --privileged=true \
- -v /etc/localtime:/etc/localtime \
- -v /data/apps/jenkins_home:/data/jenkins \
- -v /data/apps/rsync/www:/data/rsync/www \
- -d --restart=always jenkins-rsync:v2b
### 重启定时器服务
chmod 755 /sh/scp-docker.sh
systemctl restart crond
### 编辑和查看定时器配置
crontab -e
crontab -l
### 每天的2:10重启smb
10 2 * * * /sh/scp-docker.sh
docker容器备份成镜像结合定时器自动执行
[ 2023/06/03 11:34 | by 刘新修 ]
C#代码
- ### 把docker容器提交成新的镜像
- cd /data/backup
- ### 备份Jenkins容器
- docker commit -a "Jesse Liu" -m "v20230602" jenkins-rsync-v2 jenkins-rsync:v2b
- docker save -o jenkins-rsync-v2b.tar jenkins-rsync:v2b
- ### 备份Nginx容器
- docker commit -a "Jesse Liu" -m "v20230602" nginx-nodejs-redis-v2 nginx-nodejs-redis:v2b
- docker save -o nginx-nodejs-redis-v2b.tar nginx-nodejs-redis:v2b
- docker commit -a "Jesse Liu" -m "v20230602" jenkins-rsync-v2 jenkins-rsync:v2b
- docker save -o jenkins-rsync-v2b.tar jenkins-rsync:v2b
- docker commit -a "Jesse Liu" -m "v20230602" nginx-nodejs-redis-v2 nginx-nodejs-redis:v2b
- docker save -o nginx-nodejs-redis-v2b.tar nginx-nodejs-redis:v2b
- ### SHELL脚本依次压缩导出tgz文件
- cd /data/backup &&
- rm -rf jenkins-rsync-v2b.tgz &&
- rm -rf nginx-nodejs-redis-v2b.tgz &&
- docker rmi jenkins-rsync:v2b &&
- docker rmi nginx-nodejs-redis:v2b &&
- docker commit -a "Jesse Liu" -m "v20230602" jenkins-rsync-v2 jenkins-rsync:v2b &&
- docker image save jenkins-rsync:v2b > jenkins-rsync-v2b.tgz &&
- docker commit -a "Jesse Liu" -m "v20230602" nginx-nodejs-redis-v2 nginx-nodejs-redis:v2b &&
- docker image save nginx-nodejs-redis:v2b > nginx-nodejs-redis-v2b.tgz
- ### SHELL脚本依次解压导入docker镜像
- docker image load -i jenkins-rsync-v2b.tgz
- docker image load -i nginx-nodejs-redis-v2b.tgz
- ### 编辑和查看定时器配置
- crontab -e
- crontab -l
- 每星期六的晚上11 : 00 pm重启smb
- 0 23 * * 6 /etc/init.d/smb restart
- 每天的0:30重启smb
- 30 0 * * * /etc/init.d/smb restart
- 30 0 * * * root /sh/docker-backup.sh
- 一分钟执行一次
- */1 * * * * root /sh/test.sh
- */1 * * * * root /sh/test.sh
- ### 重启定时器服务
- chmod 755 /sh/test.sh
- chmod 755 /sh/docker-backup.sh
- chmod 755 /sh/scp-docker.sh
- systemctl restart crond
### 本地机器生成密钥
ssh-keygen -t rsa
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:PeMmQH0e2HL+jxZjYJ6/8Hej+ANJ06gm+sUIhzCLNho root@localhost
The key's randomart image is:
+---[RSA 3072]----+
| |
| . o |
| o . + = o |
| . +.. Bo= . |
|E+ . o..SoOoo |
|o.. ooo+o*+ |
|. ..+o+ooo |
| . .o o++...|
| .. o==+..|
+----[SHA256]-----+
[root@localhost ~]#
进入主机A ~/.ssh/ 目录,会发现生成了一对密钥
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/id_rsa.pub.
### A将公钥文件中的内容复制到 ==> 远程服务器端B
~/.ssh/id_rsa.pub
~/.ssh/authorized_keys
## 步骤如下
[root@localhost .ssh]# pwd
/root/.ssh
[root@localhost .ssh]# ll
total 12
-rw------- 1 root root 891 Jun 1 14:52 id_rsa
-rw-r--r-- 1 root root 228 Jun 1 14:52 id_rsa.pub
-rw-r--r-- 1 root root 172 Jun 1 22:28 known_hosts
[root@localhost .ssh]# vi /root/.ssh/authorized_keys
### 粘贴id_rsa.pub内的字符串到这个文件,对于OpenSSH来说,我们需要将公钥加入到~/.ssh/authorized_keys中。每个公钥一行,千万注意不要换行,否则可能不能正常工作。
### 修改文件权限
chmod 600 /root/.ssh/authorized_keys
### 修改/etc/ssh/sshd_config 文件
修改/etc/ssh/sshd_config 文件,将RSAAuthentication 和 PubkeyAuthentication 后面的值都改成yes
将PasswordAuthentication yes 修改成 PasswordAuthentication no
### 重启SSHD
/etc/init.d/sshd restart
docker对已经启动的容器添加目录映射(挂载目录)
[ 2023/06/03 11:27 | by 刘新修 ]
查看某个容器的详细信息
docker inspect 657f23bba0c6
进入默认容器的配置文件目录
C#代码
- cd /var/lib/docker/containers/657f23bba0c6ddc002c0b2f57c23edd36be742f9777f5ed9b755538085c419b0
我们将要修改的文件就是config.v2.json和hostconfig.json
修改config.v2.json
调出冒号命令,输入如下(这里注意,shift+:),%!python -m json.tool
====================================
C#代码
- "MountPoints": {
- ...,
- "/data/rsync": {
- "Destination": "/data/rsync",
- "Driver": "",
- "Name": "",
- "Propagation": "rprivate",
- "RW": true,
- "SkipMountpointCreation": false,
- "Source": "/data/apps/rsync",
- "Spec": {
- "Source": "/data/apps/rsync",
- "Target": "/data/rsync",
- "Type": "bind"
- },
- "Type": "bind"
- }
- }
修改hostconfig.json
调出冒号命令,输入如下(这里注意,shift+:),%!python -m json.tool
C#代码
- "AutoRemove": false,
- "Binds": [
- "/data/apps/nginx/htdocs:/data/nginx/htdocs",
- "/data/apps/redis/htdocs:/data/redis/htdocs",
- "/data/apps/nodejs/htdocs/nest/other:/data/nodejs/htdocs/nest/other",
- "/data/apps/nodejs/htdocs/nest/do_nest/public/www:/data/nodejs/htdocs/nest/do_nest/public/www",
- "/data/apps/nodejs/htdocs/nest/KTM/dist:/data/nodejs/htdocs/nest/KTM/dist",
- "/data/apps/nodejs/htdocs/nest/KTM/public:/data/nodejs/htdocs/nest/KTM/public",
- "/data/apps/rsync:/data/rsync"
- ],
先关闭docker服务
C#代码
- systemctl start docker
- systemctl stop docker
- systemctl stop docker.socket
- systemctl enable docker.service
rsync 常用的同步命令
[ 2023/06/03 11:12 | by 刘新修 ]
C++代码
- ### 由本地向服务器推送
- rsync -avz --port=8730 dist.tar.gz liuxinxiu@127.0.0.1::test
- rsync -avzP --port=8730 dist.tar.gz dynamicAssets.json jenkins@172.16.207.22::work-litigation
- ### 由服务器向本地下载
- rsync -avz liuxinxiu@127.0.0.1::test /test/111
- rsync -avz jenkins@172.16.207.22::work-litigation /var/www/html/mirrors/frontend/injured/work-litigation
- ### 创建软连接(前边是存储源——后边是软链接)
- ln -s /data/apps/rsync/www/release /data/apps/nginx/htdocs/release
以下是具体是例子:
C#代码
- ### 由本地向服务器推送
- cd /data/apps/nginx/htdocs/$projectPath/upload
- rsync -avzP --port=8730 dist.tar.gz dynamicAssets.json jenkins@172.16.207.22::$las_dir
- ### 由本地向服务器推送
- cd /data/apps/nginx/htdocs/$projectPath/upload
- rsync -avzP --port=8730 dist.tar.gz dynamicAssets.json jenkins@172.16.207.22::release/$projectPath &&
- cd /data/apps/rsync/www/release/$lat_dir &&
- scp -r $pat_dir root@10.10.9.99:/var/www/html/mirrors/frontend/$pat_dir &&
- curl http://10.10.9.99/frontend/$projectPath
mysql 新建用户并授权 & 更新用户密码
[ 2023/02/23 10:35 | by 刘新修 ]
SQL代码
- insert into mysql.user(Host,User,Password) values ("%","admin",password("cnmo"));
- CREATE USER 'admin'@'localhost' IDENTIFIED BY 'cnmo';
- GRANT privileges ON *.* TO 'admin'@'localhost';
- GRANT ALL privileges on *.* to 'admin'@'localhost' identified by 'cnmo';
- FLUSH PRIVILEGES;
更新用户密码
SQL代码
- use mysql # 连接权限数据库
- update user set password=password('521') where user='root' and host='localhost'; # 改密码
- flush privileges; # 刷新权限
[MySQL] 注意MySQL5.1不支持utf8mb4 Error 1115: Unknown character set: 'utf8mb4'
[ 2023/02/23 08:48 | by 刘新修 ]
MYSQL5.5支持utf8mb4,MYSQL5.1并不支持“utf8mb4”。
当连接数据库时 , 如果指定了utf8mb4 , 那么会出现的错误代码为:
Error 1115: Unknown character set: 'utf8mb4'
会出现utf8mb4不兼容的情况。
所以如果想使用utf8mb4存储表情emoji字符 , 请尽快升级