今天将会演示两种使用shell脚本备份Linux系统数据的方法。归档主要采用的tar命令。
1. 创建日归档
思路:
(1)from:首先确定哪些文件或者目录需要被归档,把它们放进一个配置文件configure file中,这样,配置文件每一行的内容就代表一个需要被归档的文件或者目录;
(2)To:最终集中归档的地方。这个地方是已经归档文件存放的地方,destination命名就用每一天的日期来命令;
(3)如何读取数据:采用配置文件重定向到标准输入中,然后通过read一行一行的读取数据并判断是否是文件或者目录,如果是,就加入tar命令的归档文件列表 FILE_LIST,否则,错误提示。
note1:采用了 2>/dev/null,是避免一些不是很重要的Error消息输出在STDOUT中。比如在路径名的最前面写了/,会报错:tar: Removing leading '/' from member names.这是tar命令显示的一条警告信息,表明它删除了路径名开头的斜线,将路径从绝对路径名变成了相对路径名。这样就可以将tar归档文件解压到文件系统中的任何地方了。
note2::因为tar归档文件会小号大量的磁盘空间,最好能压缩一下啊该文件。因此加一个-z选项。用tar.gz或者.tgz表示都行。
note3:一旦read命令到了配置文件的末尾,它就会返回一个非0状态码。这是脚本退出while循环。
note3:or选项用-o考虑
note4:FILE_NO是为了告诉哪一行出错了。
note5:创建号归档目录后,你需要授予某些用户访问权限。(一般这个归档是超级用户创建用的,但是本例子是普通用户自己弄得)
note6:tar -czf顺序很重要,选项顺序不同,会造成不同的输出。
整体代码如下:
1)最开始手动添加如下:
1 [Hermioner@localhost Documents]$ ls 2 a.txt b.txt c.txt Daily_Archive Files_To_Archive 3 [Hermioner@localhost Documents]$ cat Files_To_Archive 4 /home/Hermioner/Documents/a.txt 5 /home/Hermioner/Documents/b.txt 6 /home/Hermioner/Documents/c.txt 7 [Hermioner@localhost Documents]$
2)然后执行脚本
1 [Hermioner@localhost Documents]$ cat Daily_Archive 2 #!/bin/bash 3 #*********************1.************************* 4 # Gather Current Date 5 DATE=$(date +%y%m%d) 6 7 # Set Archvie File name 8 FILE=$DATE.tar.gz 9 10 # Set Configuration and Destination File 11 CONFIG_FILE=/home/Hermioner/Documents/Files_To_Archive 12 DESTINATION=/home/Hermioner/Documents/$FILE 13 14 #*********************2.************************* 15 #check whether configure file is exits 16 if [ -f $CONFIG_FILE ] 17 then 18 echo 19 else 20 echo 21 echo "$CONFIG_FILE does not exist." 22 echo "Backup not completed due to missing Configuration File" 23 echo 24 exit 25 fi 26 27 #*******************3.****************************** 28 #Build the names of all the files to backup 29 30 FILE_NO=1 31 exec < $CONFIG_FILE #Redirect Std Input to name of Config File 32 read FILE_NAME 33 while [ $? -eq 0 ] 34 do 35 if [ -f $FILE_NAME -o -d $FILE_NAME ] 36 then 37 FILE_LIST="$FILE_LIST $FILE_NAME" 38 else 39 echo 40 echo "$FILE_NAME, does not exist." 41 echo "I can't included into this archive." 42 echo "$FILE_NO of configure file is wrong." 43 echo "Continuing to build the next one..." 44 echo 45 fi 46 47 FILE_NO=$[ $FILE_NO+1 ] 48 read FILE_NAME 49 done 50 51 #***************************4.********************************** 52 #Backup the files and Compress Archive 53 54 echo "Starting archive..." 55 echo 56 57 tar -czf $DESTINATION $FILE_LIST 2> /dev/null 58 59 60 echo "Archive completed" 61 echo "Resulting archive file is : $DESTINATION" 62 echo 63 64 exit 65 [Hermioner@localhost Documents]$ bash Daily_Archive 66 67 Starting archive... 68 69 Archive completed 70 Resulting archive file is : /home/Hermioner/Documents/180731.tar.gz 71 72 [Hermioner@localhost Documents]$ ls 73 180731.tar.gz a.txt b.txt c.txt Daily_Archive Files_To_Archive 74 75 #查看归档后的文件的内容如下,成功了 76 [Hermioner@localhost Documents]$ tar -tf 180731.tar.gz 77 home/Hermioner/Documents/a.txt 78 home/Hermioner/Documents/b.txt 79 home/Hermioner/Documents/c.txt 80 [Hermioner@localhost Documents]$
3)如果想要继续执行脚本,并更新配置文件,归档后会出现同样的文件名,但是内容会随着配置文件中的内容更新而更新。
2. 创建按小时归档的脚本
有时候更新很频繁的时候,就需要缩小时间保存。可以重新定义归档名字。如果所有的还是在一个目录就很长的名字,不好读。因此可以分开存放,建立年/月/日/date这样的格式,就可以很容易找到了。可以手动添加目录,也可以批量生成月/日/具体时间这样的格式(用命令提取月日年时间)。
参考文献:
Linux命令行与shell脚本编程大全(第3版)[美] 布鲁姆(Richard Blum),布雷斯纳汉(Christine Bresnahan) 著,门佳,武海峰 译