2019年11月23日 星期六

在MAC(LINUX)利用指令手動檢測USB硬碟讀寫速度

買了2個不同廠牌的32G USB外接碟,但我很好奇到底USB 3.0 的速度到底有多快?標榜的廣告內容也該來核實一下。

不用下載或安裝任何工具,利用MAC/LINUX內定指令dd,即可為我們測試磁碟讀寫速度。
廠牌1:S32(比較貴20%價格)
廠牌2:BANQ

測試寫入200M大小的檔案10次

time dd if=/dev/random of=/Volumes/S32/test1.img bs=200m count=10 && time dd if=/dev/random of=/Volumes/BANQ/test1.img bs=200m count=10

測試寫入結果 

10+0 records in
10+0 records out
2097152000 bytes transferred in 208.953879 secs (10036435 bytes/sec)
dd if=/dev/random of=/Volumes/S32/test1.img bs=200m count=10  0.00s user 7.33s system 3% cpu 3:30.02 total
[2] 35498
[2]  - 35498 done       sync
10+0 records in
10+0 records out
2097152000 bytes transferred in 235.384624 secs (8909469 bytes/sec)
dd if=/dev/random of=/Volumes/BANQ/test1.img bs=200m count=10  0.00s user 7.35s system 3% cpu 3:55.79 total

測試讀取 

把input file(if)與output file(of)互換,就由寫入變成了讀取測試。
time dd of=/dev/null if=/Volumes/S32/test1.img bs=512 count=20 &&  time dd of=/dev/null if=/Volumes/BANQ/test1.img bs=512 count=20

測試讀取結果

20+0 records in
20+0 records out
10240 bytes transferred in 0.003003 secs (3410057 bytes/sec)
dd of=/dev/null if=/Volumes/S32/test1.img bs=512 count=20  0.00s user 0.01s system 37% cpu 0.020 total
20+0 records in
20+0 records out
10240 bytes transferred in 0.012002 secs (853192 bytes/sec)
dd of=/dev/null if=/Volumes/BANQ/test1.img bs=512 count=20  0.00s user 0.00s system 17% cpu 0.026 total 
 自己親測就可以明白,一分錢一分貨,沒有錯。

2018年12月30日 星期日

Welcome file

Welcome to StackEdit!

Hi! I’m your first Markdown file in StackEdit. If you want to learn about StackEdit, you can read me. If you want to play with Markdown, you can edit me. Once you have finished with me, you can create new files by opening the file explorer on the left corner of the navigation bar.

Files

StackEdit stores your files in your browser, which means all your files are automatically saved locally and are accessible offline!

Create files and folders

The file explorer is accessible using the button in left corner of the navigation bar. You can create a new file by clicking the New file button in the file explorer. You can also create folders by clicking the New folder button.

Switch to another file

All your files are listed in the file explorer. You can switch from one to another by clicking a file in the list.

Rename a file

You can rename the current file by clicking the file name in the navigation bar or by clicking the Rename button in the file explorer.

Delete a file

You can delete the current file by clicking the Remove button in the file explorer. The file will be moved into the Trash folder and automatically deleted after 7 days of inactivity.

Export a file

You can export the current file by clicking Export to disk in the menu. You can choose to export the file as plain Markdown, as HTML using a Handlebars template or as a PDF.

Synchronization

Synchronization is one of the biggest features of StackEdit. It enables you to synchronize any file in your workspace with other files stored in your Google Drive, your Dropbox and your GitHub accounts. This allows you to keep writing on other devices, collaborate with people you share the file with, integrate easily into your workflow… The synchronization mechanism takes place every minute in the background, downloading, merging, and uploading file modifications.

There are two types of synchronization and they can complement each other:

  • The workspace synchronization will sync all your files, folders and settings automatically. This will allow you to fetch your workspace on any other device.

    To start syncing your workspace, just sign in with Google in the menu.

  • The file synchronization will keep one file of the workspace synced with one or multiple files in Google Drive, Dropbox or GitHub.

    Before starting to sync files, you must link an account in the Synchronize sub-menu.

Open a file

You can open a file from Google Drive, Dropbox or GitHub by opening the Synchronize sub-menu and clicking Open from. Once opened in the workspace, any modification in the file will be automatically synced.

Save a file

You can save any file of the workspace to Google Drive, Dropbox or GitHub by opening the Synchronize sub-menu and clicking Save on. Even if a file in the workspace is already synced, you can save it to another location. StackEdit can sync one file with multiple locations and accounts.

Synchronize a file

Once your file is linked to a synchronized location, StackEdit will periodically synchronize it by downloading/uploading any modification. A merge will be performed if necessary and conflicts will be resolved.

If you just have modified your file and you want to force syncing, click the Synchronize now button in the navigation bar.

Note: The Synchronize now button is disabled if you have no file to synchronize.

Manage file synchronization

Since one file can be synced with multiple locations, you can list and manage synchronized locations by clicking File synchronization in the Synchronize sub-menu. This allows you to list and remove synchronized locations that are linked to your file.

Publication

Publishing in StackEdit makes it simple for you to publish online your files. Once you’re happy with a file, you can publish it to different hosting platforms like Blogger, Dropbox, Gist, GitHub, Google Drive, WordPress and Zendesk. With Handlebars templates, you have full control over what you export.

Before starting to publish, you must link an account in the Publish sub-menu.

Publish a File

You can publish your file by opening the Publish sub-menu and by clicking Publish to. For some locations, you can choose between the following formats:

  • Markdown: publish the Markdown text on a website that can interpret it (GitHub for instance),
  • HTML: publish the file converted to HTML via a Handlebars template (on a blog for example).

Update a publication

After publishing, StackEdit keeps your file linked to that publication which makes it easy for you to re-publish it. Once you have modified your file and you want to update your publication, click on the Publish now button in the navigation bar.

Note: The Publish now button is disabled if your file has not been published yet.

Manage file publication

Since one file can be published to multiple locations, you can list and manage publish locations by clicking File publication in the Publish sub-menu. This allows you to list and remove publication locations that are linked to your file.

Markdown extensions

StackEdit extends the standard Markdown syntax by adding extra Markdown extensions, providing you with some nice features.

ProTip: You can disable any Markdown extension in the File properties dialog.

SmartyPants

SmartyPants converts ASCII punctuation characters into “smart” typographic punctuation HTML entities. For example:

ASCII HTML
Single backticks 'Isn't this fun?' ‘Isn’t this fun?’
Quotes "Isn't this fun?" “Isn’t this fun?”
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

KaTeX

You can render LaTeX mathematical expressions using KaTeX:

The Gamma function satisfying Γ(n)=(n1)!nN\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N is via the Euler integral

Γ(z)=0tz1etdt . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.

You can find more information about LaTeX mathematical expressions here.

UML diagrams

You can render UML diagrams using Mermaid. For example, this will produce a sequence diagram:

AliceBobJohnHello Bob, how are you?How about you John?I am good thanks!I am good thanks!Bob thinks a longlong time, so longthat the text doesnot fit on a row.Checking with John...Yes... John, how are you?AliceBobJohn

And this will produce a flow chart:

Link text
Square Rect
Circle
Round Rect
Rhombus

2017年6月13日 星期二

FreeBSD安裝GitLab

enter image description here

前言

環境參數:

  • OS:FreeBSD i386 , 10.3-RELEASE-p11
  • CPU:Intel(R) Core(TM)2 Duo CPU @ 3.00GHz
  • RAM:4096 MB
Gitlab需要以下的系統元件

要正常運作 Gitlab ,依照文件所載明,系統上需安裝以下的元件並能正常運行。

  1. Packages / Dependencies
  2. Ruby
  3. System Users
  4. Database
  5. Redis
  6. GitLab
  7. Nginx

安裝必要套件

更新系統符合utf-8

$ pkg update
$ pkg upgrade

安裝系統套件

以下的指令是一次下達完成,當然也可以一個個的下(略過已經安裝或不想升級的套級)

$ pkg install sudo bash icu cmake pkgconf git nginx node ruby ruby22-gems logrotate redis postgresql95-server postgresql95-contrib postfix krb5 gmake go

設定gem

$ gem install bundler --no-ri --no-rdoc

Fetching: bundler-1.14.3.gem (100%)
Bundler and RubyGems.org are free for anyone to use, but maintaining them costs more than $25,000 USD every month. Help us cover those costs so that we can keep the gem ecosystem free for everyone: https://ruby.to/support-bundler
Successfully installed bundler-1.14.3
1 gem installed

設定 /etc/rc.conf

$ vi /etc/rc.conf

# Core services
sshd_enable="YES"
ntpd_enable="YES"
ntpd_sync_on_start="YES"

# GitLab services
redis_enable="YES"
postgresql_enable="YES"
gitlab_enable="YES"

# Web server
nginx_enable="YES"

# Postfix/Sendmail
postfix_enable="YES"
sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"

設定 Git

新增git相關使用者及組群

直接以管理者身份進行定比方便,su -lsudo -s

# 新增使用者git
$ pw add user -n git -m -s /usr/local/bin/bash -c "GitLab"

# 新增群組名稱:redis並將使用者git加入群組內.
$ pw groupmod redis -m git

設定 Postgres 資料庫

# Postgre db初始化(第一次執行)
$ service postgresql initdb

# 啟動Postgres db
$ service postgresql start

# 以 pgsql 使用者登入系統
$ su - pgsql

# 連接Postgres database
$ psql -d template1

連接Postgres資料庫後,進行下列設定:

‘template1=#’是系統提示符號,不需要輸入.

# 設定pg_trgm 關聯性
template1=# CREATE EXTENSION IF NOT EXISTS pg_trgm;

# 新增GitLab使用者:git
template1=# CREATE USER git CREATEDB;

# 建立GitLab專用資料庫並授權予git使用者 production database & grant all privileges on database
template1=# CREATE DATABASE gitlabhq_production OWNER git encoding='UTF8';

# 退出資料庫
template1=# \q

設定 Redis

先備份原始設定/usr/local/etc/redis.conf

$ cp /usr/local/etc/redis.conf /usr/local/etc/redis.conf.orig

redis.conf相關設定

修正 redis.conf的 port預設值,變更為0:

$ vi /usr/local/etc/redis.conf

# Accept connections on the specified port, default is 6379.
    # If port 0 is specified Redis will not listen on a TCP socket.
    port 0

Redis socket設定,

確定設定檔/usr/local/etc/redis.conf最後三行中的unixsocket對應值:

aof-rewrite-incremental-fsync yes
unixsocket /usr/local/var/run/redis/redis.sock
unixsocketperm 770

如果沒有這行設定,則利用下面指令達成:

echo 'unixsocket /usr/local/var/run/redis/redis.sock' | sudo tee -a /usr/local/etc/redis.conf

再來,確認權限:

aof-rewrite-incremental-fsync yes
unixsocket /usr/local/var/run/redis/redis.sock
unixsocketperm 770

如果沒有這行設定,則利用下面指令達成:

echo 'unixsocketperm 770' | sudo tee -a /usr/local/etc/redis.conf

設定相關目錄,

$ mkdir -p /usr/local/var/run/redis
$ chown redis:redis /usr/local/var/run/redis
$ chmod 755 /usr/local/var/run/redis

啟動 redis

$ sudo service redis restart

安裝並設定 GitLab

#### 先改變路徑至git的家目錄
$ cd /home/git

#### 複制GitLab source
$ sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 8-10-stable gitlab

#### 改變路徑至剛才複制的gitlab
$ cd /home/git/gitlab

#### 複制 GitLab 設定檔
$ sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
# 編輯gitlab.yml
$ sudo -u git -H vim config/gitlab.ym
  1. host: git.mydomain #設定為自己的gitlab server網址名稱
  2. bin_path: /usr/local/bin/git #設定git的路徑
  3. 將所有的/home/git,變更實際系統路徑/usr/home/git, 視使用者家目錄的初始設定來彈性變更, 符號連結性的目錄名稱是無法運行的,故要變更為實際路徑.

接著,以root執行下列命令:

設定Gitlab Workhorse

$ cd /home/git
$ sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
$ sudo -u git -H git checkout v0.7.9
$ sudo -u git -H gmake

初始化GitLab資料庫

初始化資料庫,並開啟進階功能。
先確定config.yml的redis.sock路徑是否正確:

$ vi /home/git/gitlab/config/resque.yml
production: unix:/usr/local/var/run/redis/redis.sock

否則在建立資料表時會出現錯誤訊息:

rake aborted!
Errno::ENOENT: No such file or directory - connect(2) for /usr/local/usr/local/var/run/redis/redis.sock
/data/git/gitlab/vendor/bundle/ruby/2.2/gems/redis-3.2.2/lib/redis/connection/ruby.rb:180:in `connect_nonblock'
/data/git/gitlab/vendor/bundle/ruby/2.2/gems/redis-3.2.2/lib/redis/connection/ruby.rb:180:in `connect'
$ cd /home/git/gitlab
$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
# 輸入 'yes' 建立資料表。
# 完成後會出現訊息:'Administrator account created:'

Administrator 密碼設定及變更方式

  1. *第一次登入 GitLab 時會強制變更預設密碼。
  2. 如果想預先設定密碼,可在安裝時以參數GITLAB_ROOT_PASSWORD設定:
$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword

開機自動執行 GitLab

以root權限下載 FreeBSD的命令腳本,再置於* /usr/local/etc/rc.d*目錄下即可。

$ sudo cp ~git/gitlab/lib/support/init.d/gitlab /etc/init.d/gitlab
$ sudo chmod 555 /usr/local/etc/rc.d/gitlab

檢查設定並編譯執行檔

設定 GitLab 環境變數,

$ cd /home/git/gitlab
$ sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
        System information
    System:     
    Current User:   git
    Using RVM:  no
    Ruby Version:   2.2.6p396
    Gem Version:    2.6.4
    Bundler Version:1.14.3
    Rake Version:   10.5.0
    Sidekiq Version:4.1.4

    GitLab information
    Version:    8.10.13
    Revision:   c593548717
    Directory:  /data/git/gitlab
    DB Adapter: postgresql
    URL:        http://git.twbest.biz
    HTTP Clone URL: http://git.twbest.biz/some-group/some-project.git
    SSH Clone URL:  git@git.twbest.biz:some-group/some-project.git
    Using LDAP: no
    Using Omniauth: no

    GitLab Shell
    Version:    3.2.1
    Repository storage paths:
    - default:  /data/git/repositories/
    Hooks:      /data/git/gitlab-shell/hooks/
    Git:        /usr/local/bin/git
順利看到成果後,就可以進行下一步的編譯工作,這會花一點時間,先喝杯咖啡,等會兒再回來看,依我的機器大概跑了10分鐘。

$ sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production

# 啟動Sidekiq
$ sudo -u git -H RAILS_ENV=production bin/background_jobs start

啟動 GitLab

Start GitLab service
來到這個階段,總算大功完成,接下來就以git使用者權限啟動 GitLab 服務,否則會出現錯誤訊息而致無法啟動。

$ sudo -u git /usr/local/etc/rc.d/gitlab start
GitLab is not running.
Starting GitLab Unicorn
Starting GitLab Sidekiq
Starting gitlab-workhorse
.
The GitLab Unicorn web server with pid 49327 is running.
The GitLab Sidekiq job dispatcher with pid 49364 is running.
The gitlab-workhorse with pid 49349 is running.
GitLab and all its components are up and running.

設定Nginx

在FreeBSD下,Nginx 的預設值是沒有啟用 gzip_static 模組,所以要手動啟用。

$ sudo mkdir -p /usr/local/etc/nginx/conf.d
$ sudo mkdir -p /usr/local/etc/nginx/ssl
$ sudo mkdir -p /var/log/nginx

# SSL
$ sudo fetch -o /usr/local/etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab-ssl

# HTTP
$ sudo fetch -o /usr/local/etc/nginx/conf.d/gitlab.conf https://gitlab.com/gitlab-org/gitlab-ce/raw/master/lib/support/nginx/gitlab

編輯/usr/local/etc/nginx/nginx.conf 設定檔:

$ sudo vi /usr/local/etc/nginx/nginx.conf
# 加入下列二行,載入相關模組。
# Nginx modules
load_module /usr/local/libexec/nginx/ngx_mail_module.so;
load_module /usr/local/libexec/nginx/ngx_stream_module.so;

編輯/usr/local/etc/nginx/conf.d/gitlab.conf*設定檔:

# 置換YOUR_SERVER_FQDN為自己的FQDN網址
  server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com

測試 Nginx的組態設定是否正常?

$ sudo nginx -t
nginx: the configuration file           /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

看到測試成功了,就可以重新啟動 Nginx 服務。

$ sudo service nginx start

完成安裝 GitLab

最後,再次確認GitLab的設定一切正常,

cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

enter image description here
依照畫面指示命令,來修復該修正或更改之處。接著在瀏覽器訪問Nginx設定 /usr/local/etc/nginx/conf.d/gitlab.conf中所設定的YOUR_SERVER_FQDN網址(http://your_server_fqdn)即可登入GitLab,首次登入按本例則必需要變更root(登入名稱)的密碼。

參考來源

https://github.com/gitlabhq/gitlabhq/blob/7-6-stable/doc/install/installation.md

本文大網

2017年5月24日 星期三

Box推出新功能 Box Note

enter image description here
Box 這老牌的雲端儲存,提供免費的網路硬碟服務,免費帳號有10 GB可使用,在使用經驗上:速度算快,但客戶端同步的CPU耗用較高。
當然不能只靠免費的儲存在經營,所以Box推出了新功能:Box Note,只要以原帳號登入後即可使用,主要的特點有:

1. 線上多人一同編輯

這個功能很好用,尤其是在列清單或在團隊腦力激盪時更有加分效果。以往我們在列清單重點時,常遇到幾點困難:

1. 通常只能自己列好項目後再傳送給其它成員閱讀或修改。
2. 更改過的項目,也不知道是誰變更?當然如果使用Word的修訂記錄功能也可以追蹤。
3. 在思考或腦力激盪時無法共同作業,大多由一人負責列表或書寫。

使用 Box Note後,以上的問題就通通解決了。果然好的產品就是站在如何為人類解決問題的角度,希望這個產品能發展成功。

2. 分享容易

提供了多種版本可供下戴安裝,目前還沒找到 Linux 版本,要在 Linux 上使用或許只能用 Wine。

官方影片介紹:

2017年5月22日 星期一

syslog 與 newsyslog 的設定方式

環境參數:
* 工作:設定 tinyproxy.log整合於syslog並自動壓縮 (logrotate)
* OS:FreeBSD i386 , 10.3-RELEASE-p11
* CPU:Intel(R) Core(TM)2 Duo CPU @ 3.00GHz
* RAM:4096 MB

整合於syslog與壓縮歸檔

常常在發生問題或需要資訊時,log檔就變的很重要;但是平時它卻是個佔用空間的元凶。如果你還沒遇上過動輒幾GB的寵然大檔,可能未曾心動,當然不會心痛。
因為一旦空間被吃掉,很多工作就停擺,雖然現在的儲存空間的成本大大降低,不過我們還是養成好習慣,把log整理,方便查詢追踪也不用擔心空間被佔用太多。
日後應該建置 Log Server 來統一存檔,但今天先把syslog與newsyslog ( Log Rotate )搞定!

tinyproxy

tinyproxy是套輕便的 Proxy 服務,簡單的安裝說明:

$ pkg install tinyproxy

設定檔存放在 /usr/local/etc/tinyproxy.conf
在設定檔中的Log設定,為了要整合在syslog中,也就是將log交給系統處理而不是tinyproxy自行寫入,設定如下:

User nobody
Group nobody
Port 8889
Listen 10.11.11.2
Bind 10.11.11.2
Timeout 600
DefaultErrorFile "/usr/local/share/tinyproxy/default.html"
StatFile "/usr/local/share/tinyproxy/stats.html"
# LogFile "/var/log/tinyproxy.log"
#
# Syslog: Tell tinyproxy to use syslog instead of a logfile.  This
# option must not be enabled if the Logfile directive is being used.
# These two directives are mutually exclusive.
# 整合在syslog中,則下行是必要的。如果要單獨交給tinyproxy寫入,則取消這行,改由 LogFile 取代之。
Syslog On
LogLevel Info
# 這個PidFile很重要,在newsyslog.conf中會用到
PidFile "/var/run/tinyproxy.pid"
MaxClients 30
MinSpareServers 5
MaxSpareServers 30
StartServers 10
MaxRequestsPerChild 0
# 允許來源存取IP
Allow 192.168.1.0/24
ViaProxyName "tinyproxy"
ConnectPort 443
ConnectPort 563
ConnectPort 22000

設定 syslog.conf

$ vi /etc/syslogd.conf
# 加入這二行,代表將tinyproxy執行序相關資訊寫入 /var/log/tinyproxy.log
!tinyproxy
*.*                                             /var/log/tinyproxy.log

如果還沒建立 /var/log/tinyproxy.log,可以用二種方式:
1. $ touch /var/log/tinyproxy.log
2. 在 newsyslog.conf中設定即可。

設定 newsyslog.conf

man newsyslog.conf 可知newsyslog的格式設定為:
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]

$ vi /etc/newsyslog.conf
# 加入底下這行
/var/log/tinyproxy.log  644  10    100  *     JC  /var/run/tinyproxy.pid 1

參數說明:

  1. 644是檔案屬性。
  2. 10 則代表保留10份歸檔 (即/var/log/tinyproxy.log現行記錄外,再加上 /var/log/tinyproxy.log.0.bz2 ~ /var/log/tinyproxy.log.9.bz2共有11個記錄檔)
  3. 100則代表當檔案大小為100k時就進行歸檔壓縮。
  4. 100後面的*號代表不限時間,只要檔案超過100k就壓縮歸檔。
  5. C 代表 /var/log/tinyproxy 若不存在則自動建立此檔。
  6. J 代表 歸檔時以 bzip2格式壓縮,gzip則使用Z
  7. /var/log/tinyproxy.pid是newsyslog在歸檔後會重啟這個PID。
  8. 最後一個1代表 /var/log/tinyproxy.pid只有1個執行緒;有時候像squid proxy會有多個,此時則需設定相同的數量。

重啟 syslog服務

$ service syslogd restart

$ killall -HUP syslogd

大工完成,看看log檔就可收到成效:

$ tail /var/log/tinyproxy.log
參考來源:

1.https://www.newsyslog.org/manual.html

本文大網

2017年5月18日 星期四

利用 sitecopy 經由FTP同步網站

enter image description here

環境參數:

  • OS:FreeBSD i386 , 10.3-RELEASE-p11
  • CPU:Intel(R) Core(TM)2 Duo CPU @ 3.00GHz
  • RAM:4096 MB
  • sitecopy :0.16.6

sitecopy 自動同步網站內容

安裝 sitecopy

FreeBSD:

$ pkg install sitecopy-0.16.6_4

Ubuntu:

$ apt-get install sitecopy

建立 sitecopy 檔案快照目錄及設定檔

sitecopy啟用前,需建立檔案快照資料庫( .sitecopyrc)及編輯相關設定( /usr/local/etc/sitecopy/~)才能正常運行。

建立快照資料庫的目錄(自訂路徑即可)

.sitecopy 是用來儲存sitecopy比對檔案異同時的資料庫,一般建立在設定目錄下,當然可依自己規劃來設定。

$ mkdir /usr/local/etc/.sitecopy
$ chmod 0700 /usr/local/etc/.sitecopy

sitecopyrc 設定檔

本例以mydomain.conf為設定檔範例,網址相對則為 mydomain,遠端虛擬主機IP為1.2.3.4。以這些資料來進行設定:

$ touch /usr/local/etc/sitecopyrc/mydomain.conf
$ chmod 600 /usr/local/etc/sitecopyrc/mydomain.conf
$ vi /usr/local/etc/sitecopyrc/mydomain.conf

# sitecopyrc : mydomain
#----------------------------------------------------------
site mydomain # 網址1設定
   server 1.2.3.4 # FTP Server IP
   protocol ftp
   url http:/mydomain
   username username
   password password
   local /var/www/mydomain # 本機檔案路徑路
   remote ~/public_html/mydomain # 遠端(FTP)路徑
   permissions all
    # 排除 temp, cache 目錄底下檔案不上傳更新或下戴
    # 含有底線 _ 的檔案或資料夾也不更新
   exclude temp
   exclude cache
   exclude *_*
site newdomain # 網址2設定
   server 1.2.3.4 # FTP Server IP
   protocol ftp
   url http:/newdomain
   username username
   password password
   local /var/www/newdomain # 本機檔案路徑路
   remote ~/public_html/newdomain # 遠端(FTP)路徑
   permissions all

輸入vi指令: wq存檔離開編輯界面。

檢查本地端快照資料

$ sitecopy -r /usr/local/etc/sitecopyrc/mydomain.conf -p /usr/local/etc/.sitecopy mydomain
sitecopy: Error: No storage file for `mydomain'.
sitecopy: Use --init, --catchup or --fetch to create a storage file.
sitecopy: Skipping site `mydomain'.
sitecopy: No valid sites specified.
Try `sitecopy --help' for more information.

由上可知資料庫尚未建立,所以需要先初始化

初始化 ( -i,即 –initialize)

$ sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -i  mydomain
sitecopy: Initializing site `mydomain' (on 1.2.3.4 in ~/public_html/mydomain/)
sitecopy: All the files and directories are marked as NOT updated remotely.

本地端 FTP 同步上傳到遠端主機

一開始我們要先確定本地端的檔案都已經上傳至虛擬主機目錄下,方法有很多種,選擇自己熟悉方便的即可。

1.打包後自行上傳,利用 tar -zcvf 指令壓縮打包後上傳再解壓,可以節省一點時間。
2.利用ftp軟體直接目錄對目錄慢慢上傳,簡單但耗時。
3..利用 sitecopy 指令上傳 ( -u ,即 –update),練習指令的好機會,利用(-u,即 –update)參數直接更新遠端檔案:

$ sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -u  mydomain

測試新增本地端檔案後,再更新遠端檔案

驗証是否在本地端新增檔案後,會自動上傳到遠端?

$ echo "new file" > /var/www/mydomain/new.html
$ sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -u  mydomain
sitecopy: Updating site `mydomain' (on 1.2.3.4 in ~/public_html/mydomain/)
Uploading new.html: [.] done. (100% finished)
Setting permissions on new.html: done.
sitecopy: Update completed successfully.

成功上傳了。

遠端 FTP 同步下戴到本地端主機

  1. 先列出遠端的檔案(-f,即 –fetch)
$ sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -f  mydomain
sitecopy: Fetching site `mydomain' (on 1.2.3.4 in ~/public_html/mydomain/)
File: new.html - size 11
File: index.html - size 10
sitecopy: Fetch completed successfully.

結果顯示遠端目前有2個檔案:new.html及index.html, 本地端(/var/www/mydomain)則無任何檔案。

  1. 將本地端更新為遠端檔案狀態(-s, 即–synchronize)
$ sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -s  mydomain
sitecopy: Synchronizing site `mydomain' (on 1.2.3.4 in ~/public_html/mydomain/)
Downloading index.html: [.] done.
Setting permissions on index.html: done.
Downloading new.html: [.] done.
Setting permissions on new.html: done.
sitecopy: Synchronize completed successfully.

成功將遠端檔案下載回本地端。

注意 -s 這個指令會完全將本地端同步成遠端檔案,也就是任何其它存在本地端但卻不存在遠端的檔案會被刪除。

測試:繼上個步驟,如果本地端不小心異動檔案後,想同步為遠端檔案?

$ touch > /var/www/mydomain/new.html #不小心更改了new.html這佪檔案
$ sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -s  mydomain
sitecopy: Synchronizing site `mydomain' (on 1.2.3.4 in ~/public_html/mydomain/)
Downloading new.html: [.] done.
sitecopy: Synchronize completed successfully.

自動偵測到 new.html 與遠端不同,所以將遠端的 new.html 下戴回來。

設定每天凌晨2點自動同步(www權限)

$ crontab -u www -e
0   2   *   *   *   www     sitecopy -o -r /usr/local/etc/sitecopyrc/mydomain.conf  -p /usr/local/etc/.sitecopy -s  mydomain

多個網站如何更新?

依網站名稱分別在sitecopy設定檔中編輯

vi /usr/local/etc/sitecopyrc/mydomain.conf 

script檔案內容

建立一個檔名為sitecopy_update.sh的shell script,構想是達成3個功能:
1.第1個參數:”u”代表上傳網站,”s”則代表下載網站。
2.第2個參數是本地端目錄名稱。(不是網址!
3.自動寫入log檔。

例如想將本地端的目錄 newdomain(sitecopy設定檔中要記得設定) 上傳至虛擬主機,則使用以下指令:

$ sitecopy_update.sh u newdomain

所以每天自動執行上傳的crontab就可以簡化為:

$ crontab -u www -e
0   2   *   *   *   www     sitecopy_update.sh u mydomain
#!/bin/sh#!/bin/sh

# /home/jjnux/mycron/sitecopy_update.sh
#==========================================================
# 2017-5-19 , 00:01
#
# Use Sitecopy to backup the BC3 remote hosts files
#
#==========================================================

# Set Variables
PROG=sitecopy_update
SITECOPY=/usr/local/bin/sitecopy
LOGFILE=/var/log/sitecopy.log
RCFILE_DIR=/usr/local/etc/sitecopyrc
RCDIR=/data/.sitecopy

#=====================================
# Check Variables
#=====================================
WWWROOT=/data/htdocs

if [ -z $1 ] || [ -z $2 ];then
  if  [ ! $1 = "u" ] && [ ! $1 = "s" ];then
    echo " Specify update(u) or synchronize(s) for  vhost!!!"
    echo 
  echo " Usage:" `basename $0` 'method (u for update) ( s for synchornize) hostname'
    exit 0
  fi
else
# Let's Go Here
  Smethod=$1
  SITE=$2
  HTDOC=$WWWROOT/$2
  RCFILE=$RCFILE_DIR/bc3.conf
  if [ ! -d $HTDOC ] || [ ! -f $RCFILE ] ;then
  echo "WWW file or RCFILE not found!"
  exit 0
  fi
  $SITECOPY -o -r ${RCFILE}  -p ${RCDIR} -${1}  $2

# Time Stamp
echo $PROG $1`date '+%Y/%m/%d,%H:%m'` >> ${LOGFILE}
$SITECOPY -no -r ${RCFILE}  -p ${RCDIR} -${1}  $2  >> ${LOGFILE}
fi

那天想查閱更新記錄時,查看log檔即可。

$ more /var/log/sitecopy.log
參考來源:

1.http://www.manyfish.co.uk/sitecopy/2.

本文大網

2017年5月16日 星期二

如何更改 WordPress 的綁定網址

enter image description here

環境參數:

  • WordPress:v 4.2.5

編輯 config.php

找到網站根目錄下的 config.php,加入以下二行程式碼:

define('WP_HOME','http://newdomain.com');
define('WP_SITEURL','http://newdomain.com');

但這只是官方建議為了不讓你在後台主畫面中,不小心又輸入錯的新網址(也就是禁止更改的意思)

編輯 function.php

最重要的是這個動作,找到佈景主題下的 functions.php,通常位於 wp-content/themes/your-theme/functions.php,在最上方(

update_option( 'siteurl', 'http://newdomain.com' );
update_option( 'home', 'http://newdomain.com' );

接著訪問網站(舊網址)即可完成更新。先記下來備用並分享給各位。

參考來源:

1.https://codex.wordpress.org/Changing_The_Site_URL

本文大網