数据备份脚本的编写
再复杂的脚本也都是按着顺序一步一步来的,要学会脚本的书写步骤。
创建归档的命令tar
-z 压缩
-c 创建
-f 跟归档文件名
[root@tzPC 24Unit]# tar -cf root.tar /root/script/* tar: Removing leading `/' from member names #tar会将绝对路径改为相对路径,这样使归档文件能解压到任何地方 tar: /root/script/24Unit/root.tar: file is the archive; not dumped #root.tar是存档文件,不能删除
注意加z跟不加z选项的文件后缀名的区别
[root@tzPC 24Unit]# tar -zcf root.tar.gz /root/script/* tar: Removing leading `/' from member names tar: /root/script/24Unit/root.tar.gz: file changed as we read it
不解压直接查看tar包内容
[root@tzPC 24Unit]# tar tvf tz.tar
注意:如果使用的是带图形化界面的发行版,$HOME目录中会含有很多跟图形化有关的配置文件跟临时文件,这些我们并不需要,归档时最好选择需要归档的文件放入单独的目录
打包隐藏文件
[root@tzPC 24Unit]# tar rf tz_bash.tar /home/tz/.bash_profile tar: Removing leading `/' from member names [root@tzPC 24Unit]# tar tf tz_bash.tar home/tz/.bash_profile
打包隐藏文件目录
[root@tzPC 24Unit]# tar zcf tz_home.tar.gz /home/tz tar: Removing leading `/' from member names [root@tzPC 24Unit]# tar tf tz_home.tar.gz home/tz/ home/tz/.bash_logout home/tz/.bash_profile home/tz/.bashrc home/tz/.bash_history
排除打包文件目录script
[root@tzPC 24Unit]# tar cvf tz_home.tar --exclude script
编写需要打包的文件或目录
[root@tzPC 24Unit]# cat Files_To_Backup /root/script/20Unit /root/script/21Unit /root/script/19Unit /root/temp_script
使用exec命令重定向标准输入STDIN
exec < $CONFIG_FILE
read FILE_NAME
只要FILE_NAME文件中有文件可读,其返回状态码就为0,可使用$?获取
while [ $? -eq 0 ] do [...] read FILE_NAME done
判断文件或目录是否存在,输出不存在目录或文件那一行行号
if [ -f $FILE_NAME -o -d $FILE_NAME ] then #If file exists, add its name to the list. FILE_LIST="$FILE_LIST $FILE_NAME" else #If file doesn't exist, issue warning 发出警告 echo echo "$FILE_NAME, does not exist." echo "Obviously, I will not include it in this archive." #显然,我将不会包含它在这个归档中 echo "It is listed on line $FILE_NO of the config file." echo "Continuing to build archive list..." echo fi FILE_NO=$[$FILE_NO + 1] #Increase Line/File number by one. #行/文件数目增加一
授予用户访问权限
[root@tzPC 24Unit]# mkdir /archive [root@tzPC 24Unit]# ls -ld /archive/ drwxr-xr-x. 2 root root 6 Sep 4 22:22 /archive/ #创建一个用于存放备份文件的目录 [root@tzPC 24Unit]# mv Files_To_Backup /archive/ [root@tzPC 24Unit]# groupadd Archivers [root@tzPC 24Unit]# chgrp Archivers /archive [root@tzPC 24Unit]# ls -ld /archive/ drwxr-xr-x. 2 root Archivers 29 Sep 4 22:22 /archive/ #备份目录属于Archivers组,方便该组成员查看使用目录下的文件 [root@tzPC 24Unit]# useradd Christine [root@tzPC 24Unit]# usermod -aG Archivers Christine [root@tzPC 24Unit]# chmod 775 /archive [root@tzPC 24Unit]# ls -ld /archive/ drwxrwxr-x. 2 root Archivers 29 Sep 4 22:22 /archive/ #创建用户Christine,且还用户属于Christine组,赋予该目录权限
可以将目录的粘滞位加上
对文件按日期归档命名
DATE=$(date +%y%m%d) #Set Archive File Name 设置归档文件名称 FILE=archive$DATE.tar.gz #Set Configuration and Destination File 设置配置跟目标文件 CONFIG_FILE=/archive/Files_To_Backup DESTINATION=/archive/$FILE
整个配置脚本整体如下
[root@tzPC 24Unit]# cat Daily_archive.sh #!/bin/bash #Daily_Archive - Archive designated files & directories #日常归档-归档指定的文件和目录 #Gather Current Date #收集当前日期 DATE=$(date +%y%m%d) #Set Archive File Name 设置归档文件名称 FILE=archive$DATE.tar.gz #Set Configuration and Destination File 设置配置跟目标文件 CONFIG_FILE=/archive/Files_To_Backup DESTINATION=/archive/$FILE #Main Script #Check Backup Config file exists if [ -f $CONFIG_FILE ] #Make sure the config file still仍然 exists then #if it exists, do nothing but continue on. echo else #if it doesn't exist, issue error & exit script. echo echo "$CONFIG_FILE does not exist." echo "Backup not completed due to missing Configuration File" #due to 由于 echo exit fi #Build the names of all the files to backup #建立所有要备份的文件名称 FILE_NO=1 #Start on Line 1 of Config File. exec < $CONFIG_FILE #Redirect Std Input to name of Config File重定向标准输入到配置文件名称 read FILE_NAME #Read lst record 阅读第一条记录 while [ $? -eq 0 ] do #Make sure the file or directory exists. if [ -f $FILE_NAME -o -d $FILE_NAME ] then #If file exists, add its name to the list. FILE_LIST="$FILE_LIST $FILE_NAME" else #If file doesn't exist, issue warning 发出警告 echo echo "$FILE_NAME, does not exist." echo "Obviously, I will not include it in this archive." #显然,我将不会包含它在这个归档中 echo "It is listed on line $FILE_NO of the config file." echo "Continuing to build archive list..." echo fi FILE_NO=$[$FILE_NO + 1] #Increase Line/File number by one. #行/文件数目增加一 read FILE_NAME #Read next record. done #Backup the files and Compress压缩 Archive echo "Starting archive..." echo tar -czf $DESTINATION $FILE_LIST 2>/dev/null echo "Archive completed" echo "Resulting archive file is :$DESTINATION" echo exit
执行效果如下
[root@tzPC 24Unit]# bash Daily_archive.sh Starting archive... Archive completed Resulting archive file is :/archive/archive200904.tar.gz
创建按小时归档的脚本
要求:以/archive/hourly主目录为准,其子目录为每个月份,每个月份目录包含每天,每天目录里包含每小时的备份文件。
给用户添加权限
[root@tzPC 24Unit]# mkdir /archive/hourly [root@tzPC 24Unit]# chgrp Archivers /archive/hourly [root@tzPC 24Unit]# ls -ld /archive/hourly/ drwxr-xr-x. 2 root Archivers 6 Sep 4 23:11 /archive/hourly/ [root@tzPC 24Unit]# chmod 775 /archive/hourly [root@tzPC 24Unit]# ls -ld /archive/hourly drwxrwxr-x. 2 root Archivers 6 Sep 4 23:11 /archive/hourly
编写要归档的目录
[root@tzPC 24Unit]# cat Files_To_Backup /root/script/20Unit /root/script/21Unit /root/script/19Unit /root/temp_script
移动目录到/archive/hourly/
[root@tzPC 24Unit]# mv Files_To_Backup /archive/hourly/
脚本整体如下
[root@tzPC 24Unit]# cat Hourly_Archive.sh #!/bin/bash #Hourly_Archive - Every hour create an archive #set Configuration File CONFIG_FILE=/archive/hourly/Files_To_Backup #Set Base Archive Destination Location位置 BASEDEST=/archive/hourly #Gather收集 Current Day,Month &Time DAY=$(date +%d) MONTH=$(date +%m) TIME=$(date +%k%M) #Create Archive Destination Directory mkdir -p $BASEDEST/$MONTH/$DAY #Build Archive Destination File Name DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz #Main Script #[主体本分参见Daily_archive.sh]
学习来自:《Linux命令行与Shell脚本大全 第3版》第24章