标签 shell 下的文章

最近遇到了很多停更的 github 仓库,作者已明显放弃了维护。当我们需要修改软件包源代码的时候,就麻烦了。如果自己 fork 后发布重新发布新包,维护成本比较高。对于这种情况,更高效的办法是通过打 patch 的方式来修正软件包的代码。

下面介绍一下具体的实现办法:

为单个文件生成补丁

复制源文件,并修改复制后的文件内容,然后运行 diff 命令:

diff -up path/to/source.ext path/to/source.copy.ext > filename.patch

这条命令会产生类似如下的输出, 你将它重定向到一个文件中, 这个文件就是patch.

patch.png

参数详解:
-u 显示有差异行的前后几行(上下文), 默认是前后各3行, 这样, patch中带有更多的信息
-p 显示代码所在的c函数的信息

为多个文件生成补丁

diff -uprN path/to/source/ path/to/source_copy/ > patch

这条命令对比了 path/to/source/ 和 path/to/source_copy/ 两个目录下的所有源码差异.

参数详解:

-r 递归地对比一个目录和它的所有子目录(即整个目录树).
-N 如果某个文件缺少了, 就当作是空文件来对比。如果不使用本选项, 当diff发现旧代码或者新代码缺少文件时, 只简单的提示缺少文件。如果使用本选项, 会将新添加的文件全新打印出来作为新增的部分。

打补丁

我们将所有生成的 patch 文件放到 patches 目录后就可以使用脚本批量打补丁了。

#/bin/bash

# patch all file in patches dir
for i in $(find ./patches -name '*.patch');
do
  patch -N -p0 < $i > /dev/null 2>&1 &
done

安装
brew install bash-completion

查看
brew info bash-completion

添加配置

Add the following line to your ~/.bash_profile:
  [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

然后根据提示将配置文件加入 .bash_profile

查看原文

Automatic Snapshots for Google (gcloud) Compute Engine

Bash script for Automatic Snapshots and Cleanup on Google Compute Engine. Requires no user input!

Inspiration (and the installation instructions) taken from AWS script aws-ec2-ebs-automatic-snapshot-bash

How it works

gcloud-snapshot.sh will:

  • Determine the Instance ID of the Google Compute Engine server on which the script runs
  • Get all the Disk IDs attached to that instance
  • Take a snapshot of each Disk
  • The script will then delete all associated snapshots taken by the script for the Instance that are older than 7 days (optional: default snapshot retention can be changed by using -d flag)

Prerequisites

  • cURL must be installed
  • The VM must have the sufficient gcloud permissions, including "compute" set to "enabled":

    [    http://stackoverflow.com/questions/31905966/gcloud-compute-list-networks-error-some-requests-did-not-succeed-insufficie#31928399](http://stackoverflow.com/questions/31905966/gcloud-compute-list-networks-error-some-requests-did-not-succeed-insufficie#31928399)
  • The version of gcloud is up to date: gcloud components update

Installation

ssh on to the server you wish to have backed up

Install Script: Download the latest version of the snapshot script and make it executable:

cd ~
wget https://raw.githubusercontent.com/jacksegal/google-compute-snapshot/master/gcloud-snapshot.sh
chmod +x gcloud-snapshot.sh
sudo mkdir -p /opt/google-compute-snapshot
sudo mv gcloud-snapshot.sh /opt/google-compute-snapshot/

To manually test the script:

sudo /opt/google-compute-snapshot/gcloud-snapshot.sh

Setup CRON: You should then setup a cron job in order to schedule a daily backup. Example cron for Debian based Linux:

0 5 * * * root /opt/google-compute-snapshot/gcloud-snapshot.sh >> /var/log/cron/snapshot.log 2>&1

Please note: the above command sends the output to a log file: /var/log/cron/snapshot.log - instructions for creating & managing the log file are below.

Manage CRON Output: You should then create a directory for all cron outputs and add it to logrotate:

  • Create new directory:
sudo mkdir /var/log/cron 
  • Create empty file for snapshot log:
sudo touch /var/log/cron/snapshot.log
  • Change permissions on file:
sudo chgrp adm /var/log/cron/snapshot.log
sudo chmod 664 /var/log/cron/snapshot.log
  • Create new entry in logrotate so cron files don't get too big :
sudo nano /etc/logrotate.d/cron
  • Add the following text to the above file:
/var/log/cron/*.log {
    daily
    missingok
    rotate 14
    compress
    notifempty
    create 664 root adm
    sharedscripts
}

Snapshot Retention

By default snapshots will be kept for 7 days, however they can be kept for longer / shorter, by using the the -d flag:

Usage: ./snapshot.sh [-d <days>]

Options:

   -d  Number of days to keep snapshots. Snapshots older than this number deleted.
       Default if not set: 7 [OPTIONAL]

在默认情况下,linux终端输出的文字并不是彩色的。那么,如果想让linux输出彩色文本,那么应该怎么做呢? `echo -e
"e[1;颜色代码m显示的文字e[0m"`

  • 红色:31
  • 绿色:32
  • 黄色:33
  • 蓝色:34
  • 洋红:35
  • 青色:36
  • 白色:37
  • 黑色:38