Shell sh How to back up log files every day

Source: Internet
Author: User
Tags empty log backup
#!/bin/bash # Daily BACKUP log file log_path= "/home/fdipzone/logs" # Log directory Backup_path= "/home/fdipzone/logs/bak" # Backup directory ex PIRE=30 # Backup file only retains 30 days function backup () {if [-d ' $log _path] && [d] $backup _ Path "]; Then for file in $ (find $log _path-maxdepth 1-name "*.log"-type f); Do copy $file did else echo "${log_path} or ${backup_path} NOT exists" fi} functi

    On copy () {ofile=$1 dpath= "${backup_path}/$ (date-d yesterday +%y/%m/%d/)" dfile= "${dpath}${ofile##*/}" If [-F "$ofile"] && [-S "$ofile"]; Then if [!-D "$dpath"]; Then # Create folder Mkdir-p "$dpath" fi cp "$ofile" "$dfile" # Copy file cat/dev/n

    ull > "$ofile" # Create Empty file echo "${ofile} Copy to ${dfile} success" fi} function Clearexpire () { If [-D "$backup _path"]; then echo ${expire:=30} >/dev/null $ (fIND $backup _path-name "*.log"-type f-mtime + $expire-exec rm {} \;) # Delete expire file $ (Find $backup _path-m 
    indepth 1-depth-empty-type d-exec rm-r {} \;) # Delete empty folder else echo ' ${backup_path} NOT exists ' FI} backup Clearexpire exit 0

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.