linux利用shell腳本和cron實現定時備份、上傳及垃圾日誌清理(原創)

一、編輯detailAppBackup.shphp

輸入:apache

#!/bin/sh
#Copyright(c) 2010-2011 luoch (luocheng.cn@luocheng.cn) 
#Backup the detail files and update, clear logs
#Version 0.1.1
echo "=== Start deleting logs ==="
rm -vf `find /opt/apache/htdocs/detail/app/log -name '*.log' -mtime +7 -print` > detailAppBackup.log
echo "=== Success removed logs,delete log write in detailAppBackup.log ==="
echo "=== Start deleting CVS ==="
find /opt/apache/htdocs/detail/ -name "CVS" -exec rm -rf {} ;
echo "=== Success deleting CVS ==="
echo "=== Start backup ==="
rm -vf `find /opt/data_bak/ -name 'detail*.tar.gz' -mtime +30 -print` > detailAppBackup.log
ctime=$(date +%Y%m%d"_"%H%M%S)
tar czvf /opt/data_bak/detail_$ctime.tar.gz -C /opt/apache/htdocs/ detail --exclude config.php
echo "=== Success backup ==="
echo "=== Start upload ==="
cd /opt/data_bak
ftp -n 192.168.0.1 << autoftp
user ftpuser ftppwd
binary
put detail_$ctime.tar.gz
quit
autoftp
echo "=== Success upload ==="

將文件上傳到 /root/sh windows


二、執行 detailAppBackup.shbash


若是你也和我同樣懶,那麼這時會出現亂碼,由於你是在windows新建的文件app


vi ./detailAppBackup.shide

:set ff?測試

你會看到fileformat=unix 或者 fileformat=dosui

用:set ff=unix把它強制爲unix格式url

再次執行 ./detailAppBackup.sh 應該正常了unix


三、設置定時執行

crontab -e

在文件的最後同樣 加上

* * * * * /root/sh/detailAppBackup.sh

爲了測試,這裏時間段設置爲一分鐘執行一次

也許你會問設置定時器不是有兩種方式嗎?

crontab -e ; vi /etc/crontab

對,crontab -e是某個用戶的週期計劃任務;/etc/crontab是系統的週期任務

/etc/crontab支持兩種輸入指令的方式,一種是直接以指令形式輸入,一種則是以目錄形式輸入。


記得最後必定要加上換行符, 並從新加載配置文件

service crond reload


如:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
*  * * * * root /root/sh/detailAppBackup.sh
相關文章
相關標籤/搜索