Skip to main content

[Backup] Ký tự % trong crontab

Một ngày mưa, viết crontab backup dữ liệu về máy, gặp ngay lỗi
unexpected EOF while looking for matching ``'
Tức tự dưng một ký tự kết thúc file (EOF) xuất hiện trong câu lệnh chứa đoạn
scp SOMEWHERE xxx-`date +%Y%m%d_%H%M%S`.tar.gz
Với Linux based OSes, ký tự % trong file crontab không có giá trị gì đặc biệt.

Theo doc Ubuntu manpage 5 crontab
The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.
Khi gõ man 5 crontab trên Ubuntu 16.04, ta có:

The ``sixth'' field (the rest of the line) specifies the command to be run.  The entire command  portion of  the  line,  up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file.  Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the  shell's trailing "\".
Xem online tại: http://manpages.ubuntu.com/manpages/xenial/en/man1/crontab.1posix.html
Tài liệu online của Ubuntu manpage không giống xem từ lệnh man, nó dùng nội dung của file man cung cấp bởi systemd-cron

Trên OSX, ký tự % cũng được xem như dấu xuống dòng (newline).
Theo man của OpenBSD:

The command field (the rest of the line) is the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab. Percent signs (‘%’) in the command, unless escaped with a backslash (‘\’), will be changed into newline characters, and all data after the first ‘%’ will be sent to the command as standard input. If the command field begins with ‘-q’, execution will not be logged. Use whitespace to separate ‘-q’ from the command.
Vì thế nên trên OSX,  nếu muốn thêm timestamp vào tên file backup, phải escape dấu %.
scp hvnaws:/var/lib/xxx/xxx.tar.gz /Users/hvn/backup/xxx_`date +\%Y\%m\%d_\%H\%M\%S`.tar.gz
PS:
để chạy lệnh scp ngon lành, cần chỉ rõ path của private SSH key mình sẽ sử dụng, bởi khi chạy trong crontab, các biến môi trường sẽ không được set như lệnh gõ trên shell bình thường.

Câu lệnh trên sẽ phải sửa thành
scp -i /Users/hvn/.ssh/mykey.pem ...

Hết.
Happy backup
HVN at FAMILUG dot ORG
Sài Gòn chiều mưa

Comments

Popular posts from this blog

Tài liệu và hướng dẫn học Python

Để tiết kiệm thời gian, tốt nhất là đi học PyMI Updated: 130617 Sau đây là các tài liệu khuyên dùng: Vì nhiều lý do, nên học python2.7 tại thời điểm hiện tại (giờ là tháng 6/2013 - muốn biết tại sao thì tự tìm hiểu) python 3.5+ (giờ là tháng 2/2017) Chuẩn bị: 1. biết bật tắt máy 2. biết cài python 3. tập gõ 10 ngón - gõ 2 ngón hay 1 ngón cũng không sao, nhưng 10 ngón là cách dễ nhất để gõ nhanh nhất. Tài liệu - Nên dùng tài liệu tại trang chủ của Python làm chính, tham khảo thêm các tài liệu khác tại http://www.familug.org/2016/12/free-ebook.html Căn bản, mới học 1.1 Python PyMI.vn https://pymi.vn/tutorial/ 1.2. Python offical tutorial kết hợp làm bài tập trên HackerRank  (đề bài bằng tiếng Anh, nhưng Google translate 1 lúc cũng ra vì có nhiều ví dụ mẫu đi kèm). Học viên của Pymi.vn có rất nhiều học viên đã tự học với Learn Python the hard way nhưng chưa thấy ai thành công cả. Hai link dưới nên đọc sau khi đã nắm được những phần cơ bản của ngôn ngữ pytho...

The PyMiers

New FAMILUG