shell脚本增量备份系统文件
写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份。
参考的网址:http://blog.51cto.com/wgkgood/1330200
直接上脚本如下:
复制代码
1 #########################################################################
2 # File Name: back_template.sh
3 # Author: happy
4 # mail: zaqwsx526@163.com
5 # Created Time: Wed 06 Jun 2018 12:17:16 PM CST
6 #########################################################################
7 #!/bin/bash
8 SOURCE_DIR=(
9
∗
10
)
11
T
A
R
G
E
T
D
I
R
=
/
b
a
c
k
u
p
/
d
a
t
a
/
12
Y
E
A
R
=
‘
d
a
t
e
+
13
M
O
N
T
H
=
‘
d
a
t
e
+
14
D
A
Y
=
‘
d
a
t
e
+
15
W
E
E
K
=
‘
d
a
t
e
+
16
F
I
L
E
S
=
s
y
s
t
e
m
b
a
c
k
u
p
.
t
g
z
17
C
O
D
E
=
* 10 ) 11 TARGET_DIR=/backup/data/ 12 YEAR=`date +%Y` 13 MONTH=`date +%m` 14 DAY=`date +%d` 15 WEEK=`date +%u` 16 FILES=system_backup.tgz 17 CODE=
∗10)11TARGETDIR=/backup/data/12YEAR=‘date+13MONTH=‘date+14DAY=‘date+15WEEK=‘date+16FILES=systembackup.tgz17CODE=?
18 if [ -z “$*” ];then
19 echo -e “Please Enter Your Backup Files or Directories\n--------------------------------------------\nExample $0 /boot /etc …”
20 exit
21 fi
22 #Determine Whether the Target Directory Exists
23 if [ ! -d
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ];then
24 mkdir -p
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY
25 echo “This KaTeX parse error: Expected 'EOF', got '#' at position 48: …ly !" 26 fi 27 #̲EXEC Full_Backu…WEEK” -eq “7” ];then
31 rm -rf $TARGET_DIR/snapshot
32 cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY
33 tar -g $TARGET_DIR/snapshot -czvf $FILES echo ${SOURCE_DIR[@]}
34 [ “$CODE” == “0” ]&&echo -e “--------------------------------------------\nThese Full_Backup System Files Backup Successfully !”
35 fi
36 }
37 #Perform incremental BACKUP Function Command
38 Add_Backup()
39 {
40 cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;
41 if [ -f
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY/$FILES ];then
42 read -p "These $FILES Already Exists, overwrite confirmation yes or no ? : " SURE
43 if [ $SURE == “no” -o $SURE == “n” ];then
44 sleep 1 ;exit 0
45 fi
46 #Add_Backup Files System
47 if [ $WEEK -ne “7” ];then
48 cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf $KaTeX parse error: Expected group after '_' at position 1: _̲FILES echo ${SOURCE_DIR[@]}
49 [ “$CODE” == “0” ]&&echo -e “-----------------------------------------\nThese Add_Backup System Files Backup Successfully !”
50 fi
51 else
52 if [ $WEEK -ne “7” ];then
53 cd
T
A
R
G
E
T
D
I
R
/
TARGET_DIR/
TARGETDIR/YEAR/
M
O
N
T
H
/
MONTH/
MONTH/DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES echo ${SOURCE_DIR[@]}
54 [ “$CODE” == “0” ]&&echo -e “-------------------------------------------\nThese Add_Backup System Files Backup Successfully !”
55 fi
56 fi
57 }
58 Full_Backup;Add_Backup
复制代码
================================================================================== = 黑发不知勤学早,白首方悔读书迟。 --颜真卿《劝学》