Shell scripts for full backup data and differential backup data

Source: Internet
Author: User
Tags rsync

#!/bin/bash ## description:  the script function, cycle in weeks; Friday full backup, Saturday No backup, Sunday to Thursday differential backup;#                due to work needs, do not implement compression packaging method, using CP full backup, rsync differential backup;#                to use this script, Please modify the following two parameters directly: Backuppath and DataPath, other places please do not modify; # date: 2017-04-24 22:43:00# modify date:  2017-04-28 10:00:00  #     VERSION: 1.1#       Author: xixi_chan#  Import PATH environment variable export path=/usr/local/sbin:/usr/local/bin:/usr/ sbin:/usr/bin:/root/bin:/bin:/sbin:/root/bin#  redefine IFS variables to avoid ifs= ' echo -en  ' \n\b ' ' When a space exists in the directory or file name   Define global variables backuppath=/mnt/backup_data/#  Specify the backup path  DATAPath=/data/ac-cnc/  #  directory to be backed up   time= ' date +%y-%m-%d ' #  defines today's time variable host= ' echo  $HOSTNAME  | awk -f '.   ' {print $1} ' #  define hostname logfile=/var/Log/${host}/${host}_${time}.log "  #  definition log file name logdir= ' echo " $LogFile " | sed - r  ' [email protected][^/]+/[email protected]@g ' #  define the directory where the log files are located dirfile= ' ls  $DATAPath ' #   Save the files and directories under the backup path to the variable dirfile firstfile= "${backuppath}${host}_${time}/' Basename ${datapath} '/"    #  defines the current backup directory based on Time oldfirstfile= "${backuppath}${host}_$ (date +%y-%m-%d --date= ' 7 days  ago ') "#  definition 7 days ago Backup directory #  log file start part head ()  {[ ! -d  $LogDir  ] & & mkdir  $LogDirecho   "---------------------the backup starts---------------------- " >  $LogFile  echo  >>  $LogFile  2>&1echo " start  backup  $DATAPath  $ (date + "%y-%m-%d %h:%m:%s") "  >>  $LogFile  2 >&1 echo  >>  $LogFile  2>&1echo ---------------------the  backup starts----------------------"  >>  $LogFile  2>&1 echo  >> $ logfile 2>&1echo  >>  $LogFile  2>&1}#  Full backup fullbackup () {echo   "today is week:  ' date +%a ',  start performing a full backups ... "echo  >>  $LogFile  2>&1echo  >>  $LogFile  2> &1if [ -d  $OldFirstFile  ];then     rm -rf $ oldfirstfile  >>  $LogFile  2>&1      echo  " [  $OldFirstFile  ] delete old file success! ' Date ' "  >> $ logfile 2>&1[ ! -d  $FirstFile  ] && mkdir -pv $ firstfile   >>  $LogFile  2>&1cd  $DATAPathfor  File in  $DirFile;  docp -a ${file}   $FirstFile   >>  $LogFile  2>&1if [ $? -eq 0  ];thenecho  "[ ${datapath}${file} ] backup success! ' Date '"   >>   $LogFile  2>&1 fidoneelseif [ ! -d  $FirstFile  ];thenmkdir  -pv  $FirstFile   >>  $LogFile  2>&1 cd  $DATAPathfor  file  in  $DirFile; docp -a ${file}   $FirstFile   >>  $LogFile  2>&1 if [ $? -eq 0 ];thenecho  "[ ${datapath}${file}  ] backup success! ' Date '   >>  $LogFile  2>&1 fidoneelseecho   "[  $DATAPath  ]the backup file is exists,can ' t backup! ' date '    >>  $LogFile  2>&1 fifi   }#  Differential backup diffbackup ()  {echo   "today is week:  ' date +%a ',  start performing differential backups ... "echo  > >  $LogFile  2>&1echo  >>  $LogFile  2>&1if [ -d   $OldFirstFile  ];then        rm -rf  $OldFirstFile   >>  $LogFile  2>&1        echo  " [  $OldFirstFile  ] Delete Old File Success!          ' Date '   >>  $LogFile  2>&1         [ ! -d  $FirstFile  ] && mkdir -pv  $FirstFile     >>  $LogFile  2>&1        rsync  --dry-run -racnc --out-format= "%n"    $DATAPath   $d _dir | grep -v  "/ $ "&NBSP;|&NBSP;XARGS&Nbsp;-i{} rsync -r -a -z ${datapath}/./{}  $FirstFileif  [ $? -eq  0 ];thenecho  "[ ${datapath}${file} ] backup success! ' Date '"    >>  $LogFile  2>&1 fielse        if [  ! -d  $FirstFile  ];then                mkdir -pv  $FirstFile   >>  $LogFile  2>&1                rsync --dry-run  -racnc --out-format= "%n"    $DATAPath   $d _dir | grep -v  "/$"  |  xargs -I{} rsync -R -a -z ${DATAPath}/./{}  $FirstFile          if [ $? -eq 0 ];then        echo  "[&NBsp;${datapath}${file} ] backup success! ' Date '   >>  $LogFile  2> &1         fi         else                echo  "[ ${datapath} ]the backup file is exists,can ' t backup!        ' Date '   >>  $LogFile  2>&1         fifi}#  End of Journal end () {echo  >>  $LogFile  2>&1echo   >>  $LogFile  2>&1echo  "---------------------the backup ends -- --------------------"  >>  $LogFile  2>&1 echo  >> $ logfile 2>&1echo  "  $DATAPath  backup ends             $ (date + "%y-%m-%d %h:%m:%s") "  >>  $LogFile  2>&1echo   >>  $LogFile  2>&1echo  "---------------------the backup ends  ----------------------"  >>  $LogFile  2>&1 }a= ' date +%u ' #   Definition today is the day of the week b=7 #  defines how many days a full backup and differential backup is defined, here is defined as a 7-point loop once, if you want to increase the number of cycles, modify here c=5#  5 represents a full backup of Friday, If you want to change to a full backup in Saturday, modify the 6d=6#  to define 6 of the weekly data ago=$[  $a  +  $b  -$c  ]#  define how many days away from $c d_dir=$ {backuppath}${host}_$ (date +%y-%m-%d -d  "' echo  $ago '  days ago")/' basename $ {DATAPath} '/#date  +%Y-%m-%d -d  ' echo  $ago '  days ago '  #  define a backup strategy if  [  "$a"  -eq  "$c"  ]; thenHeadFullBackupendelif [  "$a"  -eq  "$d"  ]; thenHeadecho  "today is saturday 6, according to the  Backup strategy, today nO data backup. "  >>  $LogFile  2>&1endelseHeadDiffBackupendfi#  Send mail to designated email after backup mail_group= "[email  protected] [email protected] "mail -s  " from  $Host  backup  Information "  $Mail _group <  $LogFileexit  0


This article from "Ccschan" blog, reproduced please contact the author!

Shell scripts for full backup data and differential 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.