Shell Script--Script analysis of System file backup

Source: Internet
Author: User


The main command for the following script is:

CD $TARGET _dir/$YEAR/$MONTH/$DAY; tar-g $TARGET _dir/snapshot-czvf $FILES ${source_dir[@]} # #创建好的备份目录中, execute backup command, Here is an example of a complete command:

Tar-g/tmp/snapshot-czvf/tmp/2017_full_sysbak.tar.gz/data/sh # #第一次创建快照完整备份, compress the/data/sh directory into/tmp/and save as 2017_full_sysbak.tar.gz, if the file under/data/sh is increased, we will make an incremental backup as follows:

tar-g/tmp/snapshot-czvf/tmp/2017_add_sysbak.tar.gz/data/sh/ ----Use the original snapshot incremental backup again, just back up the part of the file that was added


The following script is to implement a canonical backup that can be added to the crontab to be executed every night in the morning, for example:

Crontab-e

0 0 * * */bin/bash/data/sh/toolscript/auto_system_backup.sh/var/log/>>/tmp/backup.log # # #每天凌晨执行备份日志目录并追加相 Shut down log information


-----------------------------Script Auto_system_ Backup.sh---------------------------------------------------------------------------------

#!/bin/bash

#automatic Backup Linux system files

#author Robin 2017-4-4


#define Variable # #参数定义

Source_dir= ($*) # #以数组的形式存放需要备份的目录, you need to specify manually when executing the script

target_dir=/data/backup/ # #指定备份路径

Year= ' Date +%y ' # #年参数, these are used to create a hierarchical directory

Month= ' Date +%m ' # #月参数

day= ' Date +%d ' # #日参数

week= ' Date +%u ' # #星期参数

A_name= ' Date +%h%m ' # #时分参数

Files=${a_name}_system_backup.tgz # #定义压缩文件格式, can also be compressed into other formats such as tar.gz

Code=$? # #判断是否有错


#Juge the Inter file exist # # #这里首先要判断在执行脚本时有没有指定参数, which is the directory to be backed up

If [-Z ' $* '];then #如果没有指定参数则提示你

Echo-e "\033[32musage:\nplease Enter Your Backup Files or directories\n-------------------------------\n\nusage: {$0/b OOT/ETC} \033[0m " # #提示信息

Exit

Fi


#Determine Whether the Target Directory Exists # #注释信息

if [!-D $TARGET _dir/$YEAR/$MONTH/$DAY];then # #判断当天的备份目录是否存在

Mkdir-p $TARGET _dir/$YEAR/$MONTH/$DAY # #创建该目录

Echo-e "\033[32mthe $TARGET _dir Created successfully! \033[0m " # #提示信息


#EXEC full_backup Function Command # #提示以下是完整备份函数


Full_backup ()

{

If ["$WEEK"-eq "7"];then # #这里指定的是周末进行完整备份

RM-RF $TARGET _dir/snapshot # #首先删除掉原有本分目录下的快照

CD $TARGET _dir/$YEAR/$MONTH/$DAY; tar-g $TARGET _dir/snapshot-czvf $FILES ${source_dir[@]} # #这里就是完整备份的命令, explain ${ Source_dir[@]}, this is what you need to specify one or more directories that need to be backed up, in the form of arrays, for example:sh/date/auto_backup.sh/etc/home

["$CODE" = = "0"]&&echo-e "----------------------------\n\033[32mthese full_backup System Files Backup SUCCESSF Ully!\033[0m " # #提示信息, indicates that the prompt message indicates success if none of the above commands are present. The following function is also the same

Fi

}

#Perform incremental Bacpup Function Command # #提示下面是增量更新的函数


Add_backup ()

{

If ["$WEEK"-ne "7"];then # #这里指定的是除开周末则进行增量更新

CD $TARGET _dir/$YEAR/$MONTH/$DAY; tar-g $TARGET _dir/snapshot-czvf update$files ${source_dir[@]} # # The difference between this statement and the one above is the backup name.

["$CODE" = = "0"]&&echo-e "----------------------------\n\033[32mthese full_backup System Files $TARGET _dir/$ year/$MONTH/$DAY/${year}_$a_name$files Backup successfully!\033[0m " # #也是提示信息

Fi

}


Sleep 3 # #延时3秒在继续以下动作

Full_backup; Add_backup ##调用函数, the execution order of the code is the last execution of the function, first two judgment statements are executed before the function processing

---------------------------------------------------------------------------------------------------------

The following is my test information:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/82/wKioL1jjTUmhGOW1AAA1C5iEx0Y277.png-wh_500x0-wm_ 3-wmp_4-s_1501796330.png "title=" Qq20170404153643.png "alt=" Wkiol1jjtumhgow1aaa1c5iex0y277.png-wh_50 "/>

The last hint is that the incremental update succeeds, and if you want to make a full update, you can modify the system time to Sunday, or modify the ["$WEEK"-eq "7"] in your code to the time of your current system before executing the script.

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/82/wKioL1jjULngDknZAABcuEY-fwQ284.png-wh_500x0-wm_ 3-wmp_4-s_845401227.png "title=" qq picture 20170404155111.png "alt=" Wkiol1jjulngdknzaabcuey-fwq284.png-wh_50 "/>

You can see the backup file in/09/0002_system_backup.tgz full backup for weekend 9th, incremental backup of 04/UPDATE1549_SYSTEM_BACKUP.TGZ week 24th


Note: The above script can be changed according to your actual needs, to achieve more features.


This article is from "The SED of linux command" blog, declined reprint!

Shell Script--Script analysis of System file backup

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.