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.