Automatically back up files to a remote FTP server under Linux and delete backups before the specified date

  
        

Here, all files under the /backup/oracledata/ts directory should be backed up to /backup/oracledata/tsbak and saved as a compressed file of TianShan20140409.tar.gz (20140409 refers to the date of the day when the backup was executed). Then only keep the backup for the last 7 days, then upload the backup file to the specified space/oraclebackup through the ftp server, and only retain the data for the last 7 days.

Scene Description

Operating System: Centos

FTP: 192.168.148.121 User: dell Password: 123

Solution: Using Linux crontab-Automated Task implementation automatic backup

Implementation steps

1. Create a directory to save backup files

[root@localhost ts]# mkdir -p /backup/oracledata/tsbak

2, create a backup script file tianshanftp.sh vim /backup/oracledata/tsbak/tianshanftp.sh; enter the following content

(detailed code explanation) #!/bin/sh

FTP_IP=192.168.148.121 #ftp地址

FTP_USER=dell #ftp username

FTP_PASS=123 #ftp密码

FTP_backup=/oraclebackup #ftp on backup The directory of the file needs to be built on FTP first

BK_DR=/backup/oracledata/tsbak #backup file storage path

DB_DR=/backup/oracledata/ts #有存在文件

DAYS=7 #DAYS=7 means delete the 7 days ago , that is, only keep the last 7 days of backup

LINUX_USER=root #system username

date=` date +%Y%m%d `

tar zcvf $BK_DR /TianShan$date.tar.gz $DB_DR

chown -R $LINUX_USER:$LINUX_USER $BK_DR #Change the owner of the backup database file

find $BK_DR -name "TianShan*" ; -type f -mtime +$DAYS -exec rm {} \\; # Delete backup files 7 days ago (note: {} \\; there are spaces in between)

deldate=` date -d -7day + %Y%m%d ` #Delete ftp server space backup 7 days ago

ftp -i -v -n $FTP_IP " END #Open ftp server. 21 is the ftp port "END and the last END: the term is called: the current document here documents. Here document is a special purpose code block

[# It uses the form of I /O redirection to put a command sequence Passed to an interactive program or command, such as ftp, cat, or ex text editor. This sentence can understand all the commands from END to ftp connection in non-conversation mode until END is entered. Of course, the name END can be taken casually, not necessarily END, you can take other names. 】

user $FTP_USER $FTP_PASS #username, password

binary #设置二元传输

cd $FTP_backup #Enter ftp directory

lcd $BK_DR #列表Local directory

mput TianShan$date.tar.gz TianShan$date.tar.gz #Upload the files in the directory

mdelete TianShan$deldate.tar.gz TianShan$deldate. Tar.gz # Delete the ftp space 7 days before the backup

bye

END

If there is no problem with the manual execution of sh, and put it in the crontab to report the error, please The above comment is deleted, try it in the execution. You can use the code in [] below.

[[ #!/bin/sh

FTP_IP=192.168.148.121

FTP_USER=dell

FTP_PASS=123

FTP_backup =/oraclebackup

BK_DR=/backup/oracledata/tsbak

DB_DR=/backup/oracledata/ts

DAYS=7

LINUX_USER=root< Br>

date=` date +%Y%m%d `

tar zcvf $BK_DR/TianShan$date.tar.gz $DB_DR

chown -R $LINUX_USER:$ LINUX_USER $BK_DR

find $BK_DR -name "TianShan*" -type f -mtime +$DAYS -exec rm {} \\;

deldate=` date -d -7day + %Y%m%d `

ftp -i -v -n $FTP_IP " END

user $FTP_USER $FTP_PASS

binary

cd $ FTP_backup

lcd $BK_DR

mput TianShan$date.tar.gz

mdelete TianShan$deldate.tar.gz

bye

END 】]

3, modify the file properties to make it executable

chmod +x /backup/oracledata/tsbak/tianshanftp.sh

4, modify /etc /crontab Vi /etc/crontab Add

5 2 * * * root /backup/oracledata/tsbak/tianshanftp.sh ” /backup/oracledata/tsbak/mylog.log 2>&1 # 2:5 to perform backup, and keep the corresponding log

5, restart crond to make the settings effective

service crond restart #Start

Everyday you are in /backup/oracledata/Below the tsbak directory, you can see a compressed file like TianShan20140409.tar.gz. At the same time, there is a compressed file like TianShan20140409.tar.gz in the oraclebackup directory under the FTP server.

If you need to restore the file. , just need to extract this file

Unzip: tar -zxvf TianShan20140409.tar.gz?

Copyright © Windows knowledge All Rights Reserved