2023/01/17
[Linux] 相見恨晚 rsync 取代 cp 的複製指令
markdown
### 問題情境
在 Linux Server 傳輸檔案,絕大多數的使用情境是可以直接用有 UI 介面的工具透過 SFTP 傳輸檔案。
但有時會遇到僅有文字介面的遠端操作環境,即先遠端到客戶主機,然後用 SSH 連線到指定的 Server。
以前只會在文字介面下用 `cp` 複製的指令,但這個指令的問題點在於:
1. 不會覆蓋已存在的檔案,若要覆蓋需加 -i (interactive),但會一個一個詢問是否要覆蓋。一兩個檔案還沒什麼,當要更新的檔案很多時,操作上有點沒效率。
2. 若在 root 的權限操作下,會蓋掉原使用者及群組的權限 (這可是大件事啊...)
### 相見恨晚的 rsync
一直以為 rsync 只是用來同步用,透過跟社群朋友交流,發現它是有更新檔案覆蓋的功效,操作上跟 cp 類似,但是最棒的是可以保留指定使用者及群組權限,還可比對各檔案差異的傳輸狀況。
我目前的操作情境是把差異更新的檔案程式先下載到主機再做更新,所以只會做複製及更新檔案的動作,不做任何刪除檔案的異動。(若遇到真需要刪檔的時候,才再行確認手動刪除)
更新操作動作如下:
```
rsync -avh /source/update_folder/ /target/project_folder/
```
常用參數:
- `-v` : verbose 模式
- `-r` : recursive 遞迴所有子目錄下的目錄及檔案
- `-a` : 相當於 `-rlptgoD` 參數,遞迴備份所有子目錄下的目錄與檔案,保留連結檔、檔案的擁有者、群組、權限以及時間戳記
- `-z` : 使用壓縮
- `-h` : 將數字以比較容易閱讀的格式輸出
對應 cp 的類似的用法則是
```
cp -ia /source/update_folder/* /target/project_folder/
```
透過 rsync 來做檔案更新操作,真的是相見恨晚呀!rsync 還有更多強大的使用情境,待日後慢慢研究啦~
同場加碼 Windows 也可以使用 rsync (工具: [cwRsync](https://itefix.net/cwrsync))
### 參考資料
- [Linux 使用 rsync 遠端檔案同步與備份工具教學與範例](https://blog.gtwang.org/linux/rsync-local-remote-file-synchronization-commands/)
2022/04/11
[Linux] 如何在 Ubuntu 20.04 升級 PHP 8.1
markdown
### 前言
PHP 7.4 即將於 2022/11/28 停止安全性支援,而 PHP 8.0 也即將於 2022/11/26 EOL (End of life),也就是今年底遲早勢必要將主機的 PHP 版本升級至 PHP 8.1。
### Server 環境及版本
- Ubuntu 20.04.4
- PHP 7.4.3
- Apache 2.4.41
2016/11/26
[CentOS] 如何在 CentOS 7 安裝使用 Visual Studio Code
自接觸客戶的 CentOS 系統, 為了要可同步並熟悉它的操作, 自己也架了一台虛擬機使用.
說實在的, 如果是個人使用的話, 我比較推薦 Ubuntu 比較人性化, 也比較不會有一些預期外的事發生.
不講別的, 在有 GUI 的主機上只有 Vim 或 gedit 來做 coding 的工具就弱掉了!
- Vim 通常是逼不得已, 或是應急小改編輯用. 真要拿來做開發用, 還是免了吧. :p
- gedit 也只是像 notepad 的功能編輯文字用
- Visual Studio Code 不僅免費用, 重要的是它跨平台的特性, 而且愈來愈好用, 開發網站, 至少這個基本的編輯器一定要有.
不廢話了~ 先來安裝 Visual Studio Code 吧!!
說實在的, 如果是個人使用的話, 我比較推薦 Ubuntu 比較人性化, 也比較不會有一些預期外的事發生.
不講別的, 在有 GUI 的主機上只有 Vim 或 gedit 來做 coding 的工具就弱掉了!
- Vim 通常是逼不得已, 或是應急小改編輯用. 真要拿來做開發用, 還是免了吧. :p
- gedit 也只是像 notepad 的功能編輯文字用
- Visual Studio Code 不僅免費用, 重要的是它跨平台的特性, 而且愈來愈好用, 開發網站, 至少這個基本的編輯器一定要有.
不廢話了~ 先來安裝 Visual Studio Code 吧!!
2016/11/18
[Linux] CentOS 7 主機遇到 file_put_contents(): failed to open stream: Permission denied 的解決方法
今日在客戶的 Linux (CentOS 7) 主機遇到從來未遇過的事, 不管權限加到多高, 擁有者, 該設定的讀寫權限全設定了。卻仍在網站的 log 看到 file_put_contents(): failed to open stream: Permission denied.
弄了一下午,終於找到原兇了,原來是 SELinux 搞的鬼!
查看 SELinux 的設定狀態
暫時關閉,不用重開機的方法
要完全關閉則需修改 /etc/sysconfig/selinux 把 SELINUX 的設定值從 enforcing 改成 disabled,存檔後再重新開啟主機即會套用設定
另外在這之前,網站曾處在 httpd 403 forbidden 的錯誤,一併記錄以免忘記 這也是因為 SELinux 在搞鬼,只要下以下指令即可
以上都是用 root 權限去設定的 感謝有一群社群技術支援的朋友們,才解開這個羅生門~
References:
弄了一下午,終於找到原兇了,原來是 SELinux 搞的鬼!
查看 SELinux 的設定狀態
sestatus
暫時關閉,不用重開機的方法
setenforce 0
要完全關閉則需修改 /etc/sysconfig/selinux 把 SELINUX 的設定值從 enforcing 改成 disabled,存檔後再重新開啟主機即會套用設定
SELINUX=disabled
另外在這之前,網站曾處在 httpd 403 forbidden 的錯誤,一併記錄以免忘記 這也是因為 SELinux 在搞鬼,只要下以下指令即可
restorecon -r /var/www/html
以上都是用 root 權限去設定的 感謝有一群社群技術支援的朋友們,才解開這個羅生門~
References:
2015/05/05
[Visual Studio Code] 跨平台編輯器 on Ubuntu Linux
2008/10/20
[Linux] Force to stop a running program
List all running programs
Kill a running program by PID (-9 is force to stop)
Some more info: Process Control
$ ps -lA
Kill a running program by PID (-9 is force to stop)
$ kill -9 [PID]
Signal | Number | Meaning |
HUP | 1 | 控制中的終端/程序中斷 |
INT | 2 | 鍵盤的插入指令 (同 Ctrl + C) |
QUIT | 3 | 鍵盤的中斷指令 (同 Ctrl + \) |
TERM | 15 | 程序的終止指令 |
KILL | 9 | 程序的強制終止指令 |
CONT | 18 | 程序的再啟動指令 (STOP後再重新啟動) |
STOP | 19 | 程序的停止指令 (同 Ctrl+Z) |
Some more info: Process Control
2007/01/30
[Linux] Remote MySQL
Recently, I'm trying to remote access MySQL database from different computer.
Finally I have it work!
Step 1: change setting info in /etc/mysql/my.cnf
Step 3: open the specific port you just set
Step 4: test & done!
Finally I have it work!
Step 1: change setting info in /etc/mysql/my.cnf
- comment out bind-address = 127.0.0.1 (it's for localhost only)
- add safe-show-database (for security reason)
- change server and client mysql default port 3306 to any other port number (e.g. 12345)
Step 3: open the specific port you just set
Step 4: test & done!
[Linux] Kubuntu hostname setup
I saw my kubuntu box displayed as "Unknown" server name in my router.
Here is the solution to fix unknown hostname problem:
change
Learned from: Fix hostname unknown in router
Here is the solution to fix unknown hostname problem:
sudo vi /etc/dhcp3/dhclient.conf
change
send host-name "newHostName";
Learned from: Fix hostname unknown in router
2007/01/09
[Linux] Kubuntu Printer Setup
Kubuntu 6.06 Printer Setup
Maybe your system can setup printer correctly, if not, here is the solution:
Open your browser and type the url:
Step by step to setup your printer:
Maybe your system can setup printer correctly, if not, here is the solution:
sudo foomatic-cleanupdrivers
sudo adduser cupsys shadow
sudo dpkg-reconfigure cupsys
Open your browser and type the url:
http://localhost:631/
Step by step to setup your printer:
- click "Administraion" Tab
- from the listing printers, click to add the correct one
- change to "Printers" Tab, click "Set as Default"
- Done
訂閱:
文章 (Atom)