shell實(shí)現(xiàn)目錄增量備份的示例代碼
更新時(shí)間:2024年08月28日 10:37:28 作者:小黑要上天
本文主要介紹了shell實(shí)現(xiàn)目錄增量備份的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
auto_backup_system.sh
#針對(duì)目錄進(jìn)行備份;每周1-6執(zhí)行增量備份,每周日(值為:0 or 7)執(zhí)行全備份
#!/bin/bash #Automatic Backup Linux System Files #By Author xiaoheiyaoshangtian #2024/x/x source_dir=($*) target_dir=/tmp year=$(date +%Y) month=$(date +%m) day=$(date +%d) week=$(date +%u) files=system_backup.tar.gz code=$? if [ -z $source_dir ];then echo -e "Please Enter File or Directory You Need to Backup:\n--------------------------------------------------\nExample $0 /boot /etc ......" exit fi #determin whether the target directory exists if [ ! -d $target_dir/$year/$month/$day ];then mkdir -p $target_dir/$year/$month/$day echo "This $target_dir/$year/$month/$day created successfully!" fi #exec full backup function command full_backup() { if [ "$week" -eq "0" ];then rm -rf $target_dir/snapshot cd $target_dir/$year/$month/$day tar -g $target_dir/snapshot -czvPf $files $(echo ${source_dir[@]}) [ "$code" == "0" ]&&echo -e "---------------------------------------\nfull_backup system files backup successfully!" fi } #perform incremental backup function command add_backup() { cd $target_dir/$year/$month/$day if [ -f $target_dir/$year/$month/$day/$files ];then read -p "$files already exists,overwrite confirmation yes or no(default yes)?:" sure if [ "$sure" == "no" -o "$sure" == "n" -o "$sure" == "N" -o "$sure" == "NO" -o "$sure" == "No" ];then sleep 1 exit 0 fi if [ $week -ne "0" ];then cd $target_dir/$year/$month/$day tar -g $target_dir/snapshot -czvPf $$_$files $(echo ${source_dir[@]}) [ "$code" == "0" ]&&echo -e "---------------------------------------\nadd_backup system files backup successfully!" fi else if [ $week -ne "0" ];then cd $target_dir/$year/$month/$day tar -g $target_dir/snapshot -czvPf $files $(echo ${source_dir[@]}) [ "$code" == "0" ]&&echo -e "---------------------------------------\nadd_backup system files backup successfully!" fi fi } full_backup add_backup
驗(yàn)證:
[root@logstash ~]# sh auto_backup_system.sh Please Enter File or Directory You Need to Backup: -------------------------------------------------- Example auto_backup_system.sh /boot /etc ...... [root@logstash ~]# [root@logstash ~]# sh auto_backup_system.sh /tmp This /tmp/2024/06/21 created successfully! tar: /tmp: Directory is new tar: /tmp/.ICE-unix: Directory is new tar: /tmp/.Test-unix: Directory is new tar: /tmp/.X11-unix: Directory is new tar: /tmp/.XIM-unix: Directory is new tar: /tmp/.font-unix: Directory is new tar: /tmp/2024: Directory is new tar: /tmp/2024/06: Directory is new tar: /tmp/2024/06/21: Directory is new /tmp/ /tmp/.ICE-unix/ /tmp/.Test-unix/ /tmp/.X11-unix/ /tmp/.XIM-unix/ /tmp/.font-unix/ /tmp/2024/ /tmp/2024/06/ /tmp/2024/06/21/ /tmp/snapshot /tmp/2024/06/21/system_backup.tar.gz --------------------------------------- add_backup system files backup successfully! [root@logstash ~]# [root@logstash ~]# sh auto_backup_system.sh /tmp system_backup.tar.gz already exists,overwrite confirmation yes or no(default yes)?:no [root@logstash ~]# [root@logstash ~]# sh auto_backup_system.sh /tmp system_backup.tar.gz already exists,overwrite confirmation yes or no(default yes)?: /tmp/ /tmp/.ICE-unix/ /tmp/.Test-unix/ /tmp/.X11-unix/ /tmp/.XIM-unix/ /tmp/.font-unix/ /tmp/2024/ /tmp/2024/06/ /tmp/2024/06/21/ /tmp/2024/06/21/2577_system_backup.tar.gz --------------------------------------- add_backup system files backup successfully! [root@logstash ~]#
[root@logstash tmp]# ls 2024 snapshot [root@logstash tmp]# [root@logstash tmp]# cd 2024/ [root@logstash 2024]# cd 06/21/ [root@logstash 21]# ls 2577_system_backup.tar.gz system_backup.tar.gz [root@logstash 21]#
到此這篇關(guān)于shell實(shí)現(xiàn)目錄增量備份的示例代碼的文章就介紹到這了,更多相關(guān)shell 目錄增量備份內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
shell命令while循環(huán)中使用sleep命令代碼示例
這篇文章主要介紹了shell命令while循環(huán)中使用sleep命令代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02修改Apache配置指定php配置文件php.ini的位置方法
下面小編就為大家?guī)硪黄薷腁pache配置指定php配置文件php.ini的位置方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02