rsync-linux备份脚本,rsync-linux脚本其实只有一条命令:r


编写为脚本

这个脚本的基本形式来自于 rsync 的网站。其实只有一条命令:

rsync –force –ignore-errors –delete –delete-excluded –exclude-from=exclude_file –backup –backup-dir=`date +%Y-%m-%d` -av

关键的参数是:

–backup: 在覆盖前备份文件


–backup-dir=`date +%Y-%m-%d`: 为备份的文件建立一个备份目录,看上去就像: 2002-08-15


-av: 存档(archive)模式和详细输出(verbose)模式。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

#!/bin/sh

#########################################################

# Script to do incremental rsync backups

# Adapted from script found on the rsync.samba.org

# Brian Hone 3/24/2002

# This script is freely distributed under the GPL

#########################################################

##################################

# Configure These Options

##################################

###################################

# mail address for status updates

# - This is used to email you a status report

###################################

MAILADDR=your_mail_address_here

###################################

# HOSTNAME

# - This is also used for reporting

###################################

HOSTNAME=your_hostname_here

###################################

# directory to backup

# - This is the path to the directory you want to archive

###################################

BACKUPDIR=directory_you_want_to_backup

###################################

# excludes file - contains one wildcard pattern per line of files to exclude

# - This is a rsync exclude file. See the rsync man page and/or the

# example_exclude_file

###################################

EXCLUDES=example_exclude_file

###################################

# root directory to for backup stuff

###################################

ARCHIVEROOT=directory_to_backup_to

#########################################

# From here on out, you probably don't #

# want to change anything unless you #

# know what you're doing. #

#########################################

# directory which holds our current datastore

CURRENT=main

# directory which we save incremental changes to

INCREMENTDIR=`date +%Y-%m-%d`

# options to pass to rsync

OPTIONS="--force --ignore-errors --delete --delete-excluded

--exclude-from=$EXCLUDES --backup --backup-dir=$ARCHIVEROOT/$INCREMENTDIR -av"

export PATH=$PATH:/bin:/usr/bin:/usr/local/bin

# make sure our backup tree exists

install -d $ARCHIVEROOT/$CURRENT

# our actual rsyncing function

do_rsync()

{

rsync $OPTIONS $BACKUPDIR $ARCHIVEROOT/$CURRENT

}

# our post rsync accounting function

do_accounting()

{

echo "Backup Accounting for Day $INCREMENTDIR on $HOSTNAME:">/tmp/rsync_script_tmpfile

echo >> /tmp/rsync_script_tmpfile

echo "################################################">>/tmp/rsync_script_tmpfile

du -s $ARCHIVEROOT/* >> /tmp/rsync_script_tmpfile

echo "Mail $MAILADDR -s $HOSTNAME Backup Report < /tmp/rsync_script_tmpfile"

Mail $MAILADDR -s $HOSTNAME Backup Report < /tmp/rsync_script_tmpfile

echo "rm /tmp/rsync_script_tmpfile"

rm /tmp/rsync_script_tmpfile

}

# some error handling and/or run our backup and accounting

if [ -f $EXCLUDES ]; then

if [ -d $BACKUPDIR ]; then

# now the actual transfer

do_rsync && do_accounting

else

echo "cant find $BACKUPDIR"; exit

fi

else

echo "cant find $EXCLUDES"; exit

fi

相关内容

    暂无相关文章