linux-bash 归档数据文件

来源:互联网 发布:mac 未能存储文稿 编辑:程序博客网 时间:2024/06/09 23:59

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_BackupDESTINATION=/home/winnie/archive/$FILE########Main Script################Check Backup Config file exists#if [ -f $CONFIG_FILE ]#Make sure the config file still existsthen#If it exists, do nothing but continue on.echoelse#If it doesn't exist, issue error&exit script.echoecho "$CONFIG_FILE does not exist."echo "Backup not completed due to missing Configuration File"echoexitfi##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 backupdo#Make sure the file or directory existsif [ -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 warningecho 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.."echofi#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##################


 
原创粉丝点击