Linux+shell obtaining and copying the latest backup data

Source: Internet
Author: User

Linux+shell obtaining and copying the latest backup data

Before we introduced some script configuration, today we introduce, using shell script How to copy the most recent files in the specified directory to the specified directory, we copy the files are compressed files, and the date of the backup is named, we present two ways, The first is to judge the movement by the last modification time of the file, and the second is to make a copy of the judgment by acquiring the filename, see below:

Let's start with the backup script, because our source file is a compressed file, so we introduce the backup script file;

We are compressing the ABC directory under the/oafs directory and naming it with the name + date

#!/bin/bashsource=/oafs/weaver    target=/oafs/weaver_backup    cpexec =$ (WHICH CP)     mkdirexec=$ (which mkdir)     curdate=$ (date  +%y-%m-%d)     bakdirtmp=${source%*/}    bakdirname=${bakdirtmp## */} $CURDATE     basedir=${bakdirtmp%/*}    hostname= ' HOSTNAME ' if [  ! -d  "$SOURCE"  ]; then       echo  "$ (date  +%y-%m-%d_%h:%m:%s)  - The source  $SOURCE  is not existed you  specified " >>/var/log/${BAKDIRTMP##*/}.log           exit 2   fiif [ ! -d  "$TARGET"  ]; then        echo  "$ (date +%y-%m-%d_%h:%m:%s)  - the target   $TARGET  is not&Nbsp;existed you spec     ified " >>/var/log/${bakdirtmp##*/}.log           exit 22     ficd   $TARGET  && tar zcf  $HOSTNAME .${bakdirname}.tar.gz -c  $BASEDIR  ${BAKDIRTMP##*/}if [  "$?"  -ne 0 ]; then   echo  "$ (date +%y-%m-%d_%h:%m:%s)  -  backup directory:  $SOURCE  to  $TARGET/${bakdirname}.tar.gz is failed " > >/var/log/${BAKDIRTMP##*/}.log     else        echo  "$ (date +%y-%m-%d_%h:%m:%s)  - Backup directory:  $SOURCE  to $ Target/${bakdirname}.tar.gz is successful " >>/var/log/${BAKDIRTMP##*/}.log    fi      exit 0


After compression

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/88/4C/wKiom1ft_wXyvoqbAACqmPG-Mc4539.png "height="/>

The next step is to copy, and we'll start with the last file modification date to determine the latest copy of the file.

Ls-lt $LOCALBAKDIR | awk {' Print $9 '} |grep-v ^$ |head-n 1

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/88/4C/wKiom1ft_waw5255AADG85XMIW0994.png "height="/>

We write the execution of the command into script execution

#!/bin/bashlocalbakdir= "/oafs/weaver_backup" target= "/oafs" function Get_last_targz () {echo $ (ls-lt $LOCALBAKDIR | awk {' Print $9 '} |grep-v ^$ |head-n 1)}function copy () {CP $LOCALBAKDIR/$ (GET_LAST_TARGZ) $TARGET}copyecho "OK"

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/88/49/wKioL1ft_wfj8y5KAAB_V0zJxYg530.png "height=" 261 "/>

Start execution

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/88/49/wKioL1ft_wfh-2nhAABojVIDxsQ797.png "height="/>

Next we use the Get file name to determine the latest copy of the file

ls | awk ' {match ($0,/.{ 4}-. {2}-. {2}/,a); B[a[0]]=$0;y=a[0]>y?a[0]:y}end{print B[y]} '

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/88/4C/wKiom1ft_wjAlr_VAACy_myvC94999.png "height="/>

We also edit script execution

#!/bin/bashlocalbakdir= "/oafs/weaver_backup" target= "/oafs" function Get_last_targz () {echo $ (ls $LOCALBAKDIR | awk ' {Match ($0,/.{ 4}-. {2}-.   {2}/,a) (B[a[0]]=$0;y=a[0]>y?a[0]:y}end{print B[y]} ')} function copy () {CP $LOCALBAKDIR/$ (GET_LAST_TARGZ) $TARGET} Copy echo "OK"

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/88/4C/wKiom1ft_wmAa5fwAABnCgneOcs058.png "height=" 207 "/>

Start execution

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/88/4C/wKiom1ft_wrT53UwAABh1bYLxzU970.png "height="/>

This article from "Gao Wenrong" blog, declined reprint!

Linux+shell obtaining and copying the latest backup data

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.