Shell script deletes the backup file instance one day ago

Source: Internet
Author: User
Tags ftp zip

Today, the company asked me to copy the backup files on the server to the internal server, and as long as they are up-to-date, well, what is the ideal goal of O & M personnel, that is, what can be done by computers, never do it by yourself.
System: centos 5.9
Environment: The backup file is under/root/bak. Of course, I tested it on a virtual machine.

1. Backup file environment diagram

2. shell script content:

The code is as follows: Copy code


#! /Bin/bash

Location = "/root/bak /"

Find $ location-type f-mtime + 1 | xargs rm-f


Figure after running the script


Now, we can see that the script has deleted all the backups other than today. If you don't want to delete them, change them to the following script:

The code is as follows: Copy code


#! /Bin/bash
Location = "/root/bak /"
Newfile = "/root/newfile"
File = $ (find $ location-type f-mtime-1)
Cp $ file $ newfile

Okay, that's it. We need to run it every day. Later we saw a friend writeAutomatically delete the backup file of the previous dayInstance

Script content backuplislog. sh:

  

The code is as follows: Copy code
#! /Bin/bash
FTPHOST = "1.1.1.7"
USERNAME = "lislog"
PASSWORD = "lislog"
    
# The Day of the previous day
Y = 'date -- date = '1 days ago '"+ % Y "'
M = 'date -- date = '1 days ago '"+ % m "'
D = 'date -- date = '1 days ago '"+ % d "'
    
# Obtain the date, year, month, and day
Yt = 'date "+ % Y "'
Mt = 'date "+ % m "'
Dt = 'date "+ % d "'
    
# Use the current day as the compressed file name
Y1 = 'date "+ % Y "'
M1 = 'date "+ % m "'
D1 = 'date "+ % d "'
    
# File name information
SRCFILE = "lis42.log. $ y-$ m-$ d * lis42.log. $ yt-$ mt-$ dt * lis42.log"
DESFILE = "lis42_policy1-policm1-policd1.zip"
    
# Directory of the file to be backed up
FILEPATH = "/home/lislog"
    
# FTP Server backup directory
# BACKUPDIR = "lislog"
    
# Compression
Cd $ FILEPATH
Zip $ DESFILE $ SRCFILE
    
# Log on to the FTP Server and transfer files
Ftp-v-n $ FTPHOST <END
User $ USERNAME $ PASSWORD
Binary
Put $ DESFILE
Bye
END
    
# Deleting temporary compressed files
Cd $ FILEPATH
Rm-f $ DESFILE
    
# End

Finally, it's okay to throw it to cron, and then you can get it when using it.

The code is as follows: Copy code

# Crontab-l
30 6 ***/home/itadmin/backuplislog. sh

View results:

The code is as follows: Copy code

# Ll/home/lislog/

-Rw-r -- 1 itadmin dba 240100295 06-08 lis42_2012-06-08.zip

The test results showed that the execution was stable.

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.