backup specially with shell script or python

需求: 备份目录,只备份新加的文件,不备份减少的文件

分析: 通常备份目录用 rsync 就可以达到目的,但开发同学怕误操作,删除文件,于是提出这个 只增不减 的需求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
backUp(){
local sourceDir=$1 #需要备份的文件/目录

local destDir=$2 #备份目的地

if [ -d "$sourceDir" ] #备份目录
then

#如果目录尚未备份,则创建此目录
if [ ! -d "$destDir/$sourceDir" ]
then
echo -e "创建目录\t$sourceDir"
mkdir -p "$destDir/$sourceDir"
fi

#对目录中对每一个文件,递归调用此函数进行备份
for var in $(ls "$sourceDir")
do
backUp "$sourceDir/$var" "$destDir"
done
else #备份文件
#如果文件在尚未备份,则直接复制
if [ ! -f "$destDir/$sourceDir" ]
then
echo -e "备份文件\t$sourceDir"
cp "$sourceDir" "$destDir/$sourceDir"
else

#如果文件已存在,则比较两个文件对最后修改时间,如果文件已修改,则更新文件
lastModifi_backup=$(date -r "$destDir/$sourceDir" +%s)
lastModifi_current=$(date -r "$sourceDir" +%s)
if [ "$lastModifi_current" -gt "$lastModifi_backup" ]
then
echo -e "更新文件\t$sourceDir"
cp "$sourceDir" "$destDir/$sourceDir"
fi
fi
fi
}
backUp "$1" "$2"

运行脚本: ./backupSpecially.sh "/date/originData" "/date/backup_dir"

发现运行shell脚本有问题,于是大神帮我写了个python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python2.7
# -*- encoding:utf-8 -*-
import os,time,filecmp

def differentFileCopy(sourcePath, targetPath):
if not os.path.exists(sourcePath):
return
if not os.path.exists(targetPath):
os.makedirs(targetPath)
for fileName in os.listdir(sourcePath):
absourcePath = os.path.join(sourcePath, fileName)
abstargetPath = os.path.join(targetPath, fileName)
if os.path.isdir(absourcePath):
if not os.path.exists(abstargetPath):
os.makedirs(abstargetPath)
differentFileCopy(absourcePath, abstargetPath)
if os.path.isfile(absourcePath):
if not os.path.exists(abstargetPath):
print 'copy %s to %s' %(fileName,abstargetPath)
os.system('cp'+' '+absourcePath+' '+targetPath)
if not filecmp.cmp(absourcePath,abstargetPath):
print 'copy %s to %s' %(fileName,abstargetPath)
os.system('cp'+' '+absourcePath+' '+targetPath)


if __name__ == '__main__':
startTime = time.clock()
sourcePath = r"/date/originData"
targetPath = r"/date/Data_backupDir"
differentFileCopy(sourcePath, targetPath)
endTime = time.clock()
time_mi = endTime // 60
time_s = endTime // 1 % 60
time_ms = ((endTime * 100) // 1) % 100
print("总用时:%02.0f:%02.0f:%2.0f" % (time_mi, time_s, time_ms))

Q Movie List

Q Movie List

1.《夜以继日》(2018)

原名:Asako I & II
导演:滨口龙介
主演: 东出昌大 / 唐田英里佳

Asako_I_II

夜以继日 男女主

两个相貌相同的男人,一个拥有着迷人的魅力,随性的坏男孩;一个是朴实无华,处处为别人考虑的体贴男孩。恋爱中的理想和现实的选择被表现淋漓尽致,深入人心。没有对错,明白什么是生活,什么是可贵的。

日本电影总把三观不太正的想法表现得很有道理的样子 😓

2. 《骡子》(2018)

原名:The Mule‎
导演:克林特·伊斯特伍德
主演: 克林特·伊斯特伍德

Mule

男主在公路上

男主与妻子去世前的对话:

> Mary: You were the love of my life, and the pain of my life. I need you to know, it’s all the world to me that you’re here.
> Earl: I love you, Mary.
> Mary: More today than yesterday?
> Earl: But not as much as tomorrow.

自己的生活还是家庭?男主一生大部分时间都用在自己的黄花菜上,或者自己的老兵俱乐部等,家人对他无不责备。但是最后男主在妻子的最后一次请求中放下手中的活儿(运送毒品),选择回到妻子身边,也得到了女儿的原谅。

### 3.《来的都是客》(1990)
导演:马绍惠
主演: 赵本山、巩汉林

Whoever Comes Is a Guest
90年代,年轻的两位男主

上世纪90年代的一部喜剧、剧情片,反讽当时的吃喝风。故事情节流畅、很贴近生活。我最早知道赵本山是春晚的小品上,但没有想到过他出演了很多电影,而且是他年轻力壮的时候,当年的赵本山脸上还没有褶子,演技也在线。

适合长辈观看,放人开怀大笑、精神放松,把我妈逗乐了。

4. 《回到被爱的每一天》 (2015)

导演、编剧: 何平
主演: 王佳佳、张译

The Promised Land

海报

一部较文艺、朴素的电影,没有大起大落,只是慢慢的讲述一个故事,看的时候要有耐心。电影的色调,搭配拍摄地的风景,使画面看起来有一种平静的美。大城市、小家乡,都是年轻人的选择,但什么样的选择更好只有自己的内心才能感觉得到。在商业电影盛行的前几年,这样纯粹的电影算是难得一见的吧。

5. 《只有芸知道》(2019)

导演: 冯小刚
主演: 黄轩,杨采钰,徐帆

only cloud knows

简单的新婚

爱情片,人物不多,风景很美,一件件往事慢慢回忆给我们,两个人在异国他乡找到彼此,又相伴十几年,都愿意为对方付出。记录一句台词,“有了房子,有了生意,就是给她最大的安全感。”
“其实房子和生意都不能给我安全感,只有你可以。” “我想和Simon换个活法”

林太的武汉话地道有味,也是在少见的异国他乡爱情片中(印象中北京话、四川话更多)。而此次的疫情恐怕让全中国都熟悉了一下武汉话,也让全世界都了解了武汉这个地名。

才发现这是2019年的电影,但似乎市场上影响不大,可能是影院排片较少,也幸好影片中商业化较少。另外,黄轩的演技值得肯定,以前一直觉得他眼神有问题,但是这部电影挺好的。

6. 《丈夫得了抑郁症》(2011)

导演: 佐佐部清
主演: 宫崎葵,堺雅人

G0uDJA.png

因为抑郁症,男主感到很自责

一场抑郁症,伴随是中长期的,身陷其中的男主处于一中特殊的痛苦、无力的生活中。随着时间的推移,作为妻子的女主也终于认识到生活的巨大改变。同时,自身也承担了更多事情:做饭、打扫、买菜、努力工作等等。男主从最开始为了妻子而治病,改变为为了自己而治病。

女主厚着脸皮请求编辑给予工作机会,也同时说出了丈夫得了抑郁症的事情。而变得释怀,不介意他人的想法。

最后两人在“同窗会”上的一段发言算是一年的总结,也让两人对婚姻有了更深的认识和理解。

生活甜蜜幸福的时候、职场得意的时候,不妨也踩一下刹车,或者停车,看一下反面类型的电影,给予自己冷静下来的机会,审时度势,未雨绸缪。

当然,电影中有很多地方并不值得严谨地推敲,现实中这样美好的结局恐怕凤毛菱角。但个人觉得不影响电影想表达的内容,以及给人以正能量的暗示。

有很多台词还是值得回味的,有点儿生活哲学、人生哲学的样子,比如
“能一直完好无所便是价值所在”
“如果努力很痛苦,那就别努力了”
“人不努力也是可以的”
“不去想忧郁症的原因,而是想它的意义。”

其他话题:婚礼同学会,这个同学会挺有意思的,就是在同一个教堂结婚的夫妇组成的同学;失业保险

mktemp command

最适合存储临时数据的位置是/tmp, mktemp 命令可以为临时文件或目录创建唯一的名字

Create a temporary file or directory.

1
2
3
4
5
6
7
8
9
10
11
12
mktemp

Create a temporary file or directory.

- Create an empty temporary file and return the absolute path to it:
mktemp

- Create a temporary directory and return the absolute path to it:
mktemp -d

- Create a temporary file with a specified suffix:
mktemp --suffix ".txt"

1.create temporary file (创建临时目录)

1
2
3
$filename=`mktemp`
echo $filename
/tmp/tmp.8xefliegnD9

2. create temporary directory (创建临时目录)

1
2
3
dirname=`mktemp -d`
echo $dirname
tmp.NJ8dhf6wk
  • If you just want to generate a filename and don’t want to create the actual file or directory, you can(如果仅仅是想生成文件名,不希望创建实际的文件或目录,可以):
    mktemp -u

  • Create a temporary filename based on the tempplate(基于模板创建临时文件名)

    1
    2
    3
    4
    5
    6
    ➜  qr_blog mktemp study.XXX
    study.UWT
    ➜ qr_blog mktemp study.XXX
    study.Usm
    ➜ qr_blog mktemp study.XXX
    study.M6a

Mac OS 与linux用法略有区别,请视情况使用

Docker clean

1.Cleanup exited processes:

docker rm $(docker ps -q -f status=exited)

3.Cleanup dangling volumes:

docker volume rm $(docker volume ls -qf dangling=true)

3.Cleanup dangling images:

docker rmi $(docker images –filter “dangling=true” -q –no-trunc)

more methods go to below link ↓

More Ref:

bash : set command

set 命令教程

set 命令用来修改 Shell 环境的运行参数,也就是可以定制环境

介绍常用的参数

1. set -u

执行脚本的时候,如果遇到不存在的变量,Bash 默认忽略它。

set -u = set -o nounset

2. set -x

默认情况下,脚本执行后,屏幕只显示运行结果,没有其他内容。如果多个命令连续执行,它们的运行结果就会连续输出。有时会分不清,某一段内容是什么命令产生的。

set -x = set -o xtrace

3. set -e

使得脚本只要发生错误,就终止执行。
set -e = set -o errexit

4. set -o pipefail

set -e有一个例外情况,就是不适用于管道命令。

所谓管道命令,就是多个子命令通过管道运算符(|)组合成为一个大的命令。Bash 会把最后一个子命令的返回值,作为整个命令的返回值。也就是说,只要最后一个子命令不失败,管道命令总是会执行成功,因此它后面命令依然会执行,set -e就失效了。

1
2
3
4
5
#!/usr/bin/env bash
set -eo pipefail

foo | echo a
echo bar

echo bar没有执行 ,否则在不用 pipefail 的情况下,会继续执行 echo bar

End

set命令的上面这四个参数,一般都放在一起使用。

1
2
3
4
5
6
# 写法一
set -euxo pipefail

# 写法二
set -eux
set -o pipefail

More Ref:

Shell output/Terminal color

change dispaly color on the terminal

The tput utility uses the terminfo database to make the values of terminal-depen-dent capabilities and information available to the shell (see sh(1)), to initialize or reset the terminal, or return the long name of the requested terminal type.

tput Color Capabilities:

  • tput setab [0-7] – Set a background color using ANSI escape
  • tput setb [0-7] – Set a background color
  • tput setaf [0-7] – Set a foreground color using ANSI escape
  • tput setf [0-7] – Set a foreground color

Color Code for tput:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White

bash script example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash

set -e

reset=$(tput sgr0)

green=$(tput setaf 2 )
blue=$(tput setaf 25)
red=$(tput setaf 1;tput bold)

info() {
printf "${blue}➜ %s${reset}\n" "$@"
}
error() {
printf "${red}× %s${reset}\n" "$@"
}
success() {
printf "${green}✔ %s${reset}\n" "$@"
}

infoWithTime(){
printf "${blue}➜ %s${reset}\n" "$@ | now: `date +'%F %T'`"
}

info "setup ,,info......"

error "haha,error..."

success "old color"

printf $(tput setaf 2;tput bold)'color show\n' $(tput sgr0)

printf $(tput setf 2;tput bold)'color show\n' $(tput sgr0)

jenkins date timezone update

方法1

打开 【系统管理】->【脚本命令行】运行下面的命令

1
System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 'Asia/Shanghai')

jenkins_run_image_01

方法2

JAVA_ARGS="-Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Shanghai -Dfile.encoding=UTF-8 -Djava.awt.headless=true"

Jenkins Dashboard with Grafana and Prometheus

1.Go to “manage Jenkins” then “manage plugins” and install “ Prometheus metrics plugin”

This plugin will expose an endpoint (default /prometheus) with metrics where a Prometheus Server can scrape data.

jenkins_dashboard_02

2.Configure Prometheus to scrape metrics from Jenkins

1
2
3
4
- job_name: ‘jenkins’
metrics_path: /prometheus
static_configs:
- targets: [‘Jenkins_Host:PORT’]

you can go to your prometheus ,find some metrics to dispaly.

jenkins_dashboard_01

3.Create a grafana Dashboard

dashboard link : https://grafana.com/dashboards/9964

then go to the grafana website import json file :
jenkins_dashboard_03

you can search in the grafana dashboard (https://grafana.com/dashboards)

4.Customize your dashboard

example:
jenkins_dashboard_04

How to Create Custom iTerm2 Window

1.Open iTerm preferences setting (打开偏好设置)

iterm_image_01

2.New add a profile (新加一个配置)

iterm_image_02

3.Setting windows name etc (定义名称,窗口高度,宽度等)

iterm_image_03

iterm_image_04

4.define hotkey (定义快捷键)

iterm_image_05

iterm_image_06

Finally 使用快捷键

一次 出现窗口,再按一次 窗口消失。如果是多屏,可以在不同屏幕下调用出来,会话内容仍保持着。

  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~