Source: <<Linux 命令行与Shell脚本编程大全>>
如何使用shell备份linux系统上的数据
归档数据文件
1. 你需要归档的文件或目录 Files_To_Backup
/home/winnie/Downloads
/home/winnie/Documents
/home/winnie/script
/home/winnie/Does_not_exist
2.创建按日归档的脚本 Daily_Archive
#!/bin/bash # #Daily_Archive-Archive designated files & directories ############################ #Gather Current Date # DATE=`date +%y%m%d` # #Set Archieve File Name # FILE=archieve$DATE.tar.gz # #Set Configuration and Destination File # CONFIG_FILE=/home/winnie/archive/File_To_Backup DESTINATION=/home/winnie/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" 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 1st record # while [ $? -eq 0 ] #Create list of files to backup 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 $LINE_NO of the config file." echo "Continuing to build archive list.." echo fi # FILE_NO=$[ $FILE_NO + 1 ] #Increasing Line/File number by one. read FILE_NAME #Read next recor. done # ############################# # #Backup the files and compress archive # tar -czf $DESTINATION $FILE_LIST 2> /dev/null #
运行结果sh ./Daily_Archive
/home/winnie/Does_not_exist does not exist.
Obviously, I will not include it in this archive.
It is listed on line of the config file.
Continuing to build archive list..
winnie@ubuntu:~/archive$ ls -al
total 20
drwxr-xr-x 3 root root 4096 Jul 17 06:02 .
drwxr-xr-x 18 winnie winnie 4096 Jul 17 05:42 ..
-rw-r--r-- 1 root root 3359 Jul 17 05:59 archieve170717.tar.gz
-rw-r--r-- 1 root root 94 Jul 17 05:42 File_To_Backup
drwxr-xr-x 3 root root 4096 Jul 17 06:02 home
winnie@ubuntu:~/archive$ sudo tar -zxvf archieve170717.tar.gz
[sudo] password for winnie:
home/winnie/Downloads/
home/winnie/Documents/
home/winnie/script/
home/winnie/script/mytest1
home/winnie/script/Delete_User
home/winnie/script/test10
home/winnie/script/Big_user
home/winnie/script/Daily_Archive
=======================================================================================
建立Hourly_Archive
#!/bin/bash
#
#Hourly_Archive-Every hour create an archive
##########################################
#
#Set Configuration file
#
CONFIG_FILE=/home/winnie/archive/hourly/Files_To_Backup
#
#Set Base archive destination location
#
BASEDEST=/home/winnie/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
#
#Buid archive destination file name
#
DESTINATION=$BASEDEST/$MONTH/$Day/archive$TIME.tar.gz
##############Main script##################