如何优化我的备份脚本

如何优化我的备份脚本

到目前为止,我并不擅长编写脚本 :-),但设法创建了单独的脚本来备份每个目录中的 Tellico 数据库。
由于我想要备份的数据库和目录相当多,我最终将所有单独的备份脚本放入一个名为 Backup-all-Tellico.sh 的脚本中。我想知道是否可以进行一些改进/建议,以使 Backup-all 脚本更高效、更精简。

#!/bin/bash  
# Modified on 04/13/2020
#Script name Backup-all-Tellico 
#This script is used to run all the backup script in my varous Tellico Database Dirs
#
# ver 1.0
#
#Database/Tellico/Tellico-MyModelLog.tc
#
cd ~/Database/Tellico/My-Models  
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
      # FileName=$MyModelLog.tc
    cp MyModelLog.tc ${DateTimeStamp}-MyModelLog.tc
    mv ${DateTimeStamp}-MyModelLog.tc ~/Database/Tellico/My-Models/Backups-Archives
#
#Database/Tellico/Tellico-JeffsSoftware
#
cd ~/Database/Tellico/Tellico-JeffsSoftware/
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
       # FileName=$JeffsSoftware-Ver2
    cp JeffsSoftware-Ver2 ${DateTimeStamp}-JeffsSoftware-Ver2
    mv ${DateTimeStamp}-JeffsSoftware-Ver2 ~/Database/Tellico/Tellico-JeffsSoftware/Backups
#
#/Database/Tellico/Tellico-MyBooks/My-Comics
#
cd ~/Database/Tellico/Tellico-MyBooks/My-Comics
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
       # FileName=$MyComics-Ver1.0
    cp MyComics-Ver1.0 ${DateTimeStamp}-MyComics-Ver1.0
    mv ${DateTimeStamp}-MyComics-Ver1.0 ~/Database/Tellico/Tellico-MyBooks/My-Comics/Backups
#
#/Database/Tellico/Tellico-MyDVDS
#
cd ~/Database/Tellico/Tellico-MyDVDS
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
       # FileName=$MyDVDS.tc
    cp MyDVDS.tc ${DateTimeStamp}-MyDVDS.tc
    mv ${DateTimeStamp}-MyDVDS.tc ~/Database/Tellico/Tellico-MyDVDS/Backups
#
#/Database/Tellico/Tellico-MyHardware
#
cd ~/Database/Tellico/Tellico-MyHardware
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
      # FileName=$MyHardware
    cp MyHardware ${DateTimeStamp}-MyHardware
    mv ${DateTimeStamp}-MyHardware ~/Database/Tellico/Tellico-MyHardware/Backups 
#
#/Database/Tellico/Tellico-MyHardware
#
cd ~/Database/Tellico/Tellico-MySubscriptions
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
      # FileName=$MySubscriptions
    cp MySubscriptions ${DateTimeStamp}-MySubscriptions
    mv ${DateTimeStamp}-MySubscriptions ~/Database/Tellico/Tellico-MySubscriptions/Backups
#
#/Database/Tellico/Tellico-MySubscriptions
#      
cd ~/Database/Tellico/Tellico-MySubscriptions
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
      # FileName=$MySubscriptions
    cp MySubscriptions ${DateTimeStamp}-MySubscriptions
    mv ${DateTimeStamp}-MySubscriptions ~/Database/Tellico/Tellico-MySubscriptions/Backups
#
#/Database/Tellico/Tellico-MyURLS
#
cd ~/Database/Tellico/Tellico-MyURLS      
 echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
      # FileName=$MyInfoUrls-Ver2.0
    cp MyInfoUrls-Ver2.0 ${DateTimeStamp}-MyInfoUrls-Ver2.0
    mv ${DateTimeStamp}-MyInfoUrls-Ver2.0 ~/Database/Tellico/Tellico-MyURLS/Backups   
#  
#/Database/Tellico/Tellico-MyWantList
#    
cd ~/Database/Tellico/Tellico-MyWantList
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
       # FileName=$MyWantList.tc
    cp MyWantList.tc ${DateTimeStamp}-MyWantList.tc
    mv ${DateTimeStamp}-MyWantList.tc ~/Database/Tellico/Tellico-MyWantList/Backups
#

答案1

我认为一个明显的改进是创建一个函数来备份每个数据库/目录,并接受一个或多个参数。假设目录名称和数据库名称不同,因此使用 2 个参数。

这是一个简单的例子,以你的例子为基础:(显然没有测试过)

#!/bin/bash  
# Modified on 04/13/2020
#Script name Backup-all-Tellico 
#This script is used to run all the backup script in my varous Tellico Database Dirs
#
# ver 1.1

# Define function to back up each dir/db
# $1 is dir name, $2 is database name
run_backup() {
   cd ~/Database/Tellico/$1  
   echo   
        DateTimeStamp=$(date '+%d-%m-%y-%H:%M')  
    cp $2 ${DateTimeStamp}-$2
    mv ${DateTimeStamp}-$2 ~/Database/Tellico/$1/Backups
}

# Run backup function for each dir/db

# Database/Tellico/Tellico-MyModelLog.tc
run_backup My-Models MyModelLog.tc

# Database/Tellico/Tellico-JeffsSoftware
run_backup Tellico-JeffsSoftware JeffsSoftware-Ver2

# Database/Tellico/Tellico-MyBooks/My-Comics
run_backup Tellico-MyBooks/My-Comics MyComics-Ver1.0

# Database/Tellico/Tellico-MyDVDS
run_backup Tellico-MyDVDS MyDVDS.tc

etc...

我希望上面的例子中我没有犯任何错误。除了代码更简洁、更短这一明显好处之外,如果你想在备份函数中实现更多功能,例如文件检查、错误代码处理等,它还可以使维护变得更加容易。

相关内容