-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsyncbackup.sh
executable file
·167 lines (146 loc) · 4.29 KB
/
rsyncbackup.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# Author: Eric Olsen - [email protected]
# Command Line Options
# command: rsyncbackup.sh
# opt 1 : '--destination-dir | -d "destination directory"'
# opt 2 : '--file-list | -f "files from list"'
# opt 3 : '--source-name | -s "backup source name | remote hostname"'
# opt 4 : '--rshell | -r "use ssh"'
# opt 5 : '--ssh-user | -U "[ssh username (default rsync-backup)]"'
# opt 6 : '--ssh-port | -P "[ssh port (default 22)]"'
_MINOPTS=3
_RSYNC=`/usr/bin/which rsync`
_RSYNCOPTS='-avR --delete'
_RSHELL="no"
_RSYNCSSHUSER="rsync-backup"
_RSYNCSSHPORT=22
_SSHIDENTITY=""
_RSYNCSSH="ssh -i $_SSHIDENTITY -p $_RSYNCSSHPORT -l $_RSYNCSSHUSER"
_RSYNCSSHOPTS="--rsync-path='sudo rsync'"
_LOGFILE=/var/log/backups/backups.log
_LOGDATE='date -u'
remote_host=
destination_dir=
backup_source=
file_list=
log_message () {
echo `eval $_LOGDATE` "$1" >> $_LOGFILE
}
parse_options () {
if [[ "$#" -lt "$_MINOPTS" ]]; then
echo "usage"
echo '--destination-dir | -d "destination directory"'
echo '--file-list | -f "files from list"'
echo '--source-name | -s "backup source name | remote hostname"'
echo '--rshell | -r "use ssh"'
echo '--ssh-user | -U "[ssh username (default rsync-backup)]"'
echo '--ssh-port | -P "[ssh port (default 22)]"'
log_message "Improper usage detected."
exit 1
else
until [ -z "$1" ]
do
case "$1" in
--destination-dir|-d)
# Does backup destination dir exist?
if [[ -d $2 ]]; then
log_message "Destination directory exists."
destination_dir=$2
else
log_message "Destination directory does not exist. Exiting."
exit 1
fi
;;
--file-list|-f)
# Is there a backup file list provided? (Needs Improvement)
if [[ ! -z $2 ]]; then
IFS=","
x=0
for item in $2; do
if [[ "$x" -gt 0 ]]; then
file_list="$file_list $item"
else
file_list="$file_list$item"
fi
let x++
done
unset IFS
log_message "Backup source file list exists."
else
log_message "Backup source file list does not exist. Exiting"
exit 1
fi
;;
--source-name|-s)
# Is there a directory for the backup source in destination dir?
if [[ -d $destination_dir/$2 ]]; then
backup_source=$2
log_message "Backup source directory exists in desitination directory."
else
backup_source=$2
mkdir -p $destination_dir/$backup_source
log_message "Backup source directory did not exist. Created new directory."
fi
;;
--rshell|-r)
# Use SSH for remote shell rsync backup
_RSHELL="yes"
remote_host=$backup_source
log_message "Rsync over SSH enabled."
;;
--ssh-user|-U)
_RSYNCSSHUSER=$2
_RSYNCSSH="ssh -p $_RSYNCSSHPORT -l $_RSYNCSSHUSER"
_RSYNCSSH="ssh -i $_SSHIDENTITY -p $_RSYNCSSHPORT -l $_RSYNCSSHUSER"
log_message "Rsync SSH user passed."
;;
--ssh-port|-P)
_RSYNCSSHPORT=$2
_RSYNCSSH="ssh -i $_SSHIDENTITY -p $_RSYNCSSHPORT -l $_RSYNCSSHUSER"
log_message "Rsync SSH port passed."
;;
esac
shift
done
fi
}
debugging () {
echo $_MINOPTS
echo $_RSYNC
echo $_RSYNCOPTS
echo $_RSHELL
echo $_RSYNCSSHUSER
echo $_RSYNCSSHPORT
echo $_RSYNCSSH
echo $_LOGFILE
echo $destination_dir
echo $backup_source
echo $file_list
}
rotate_backup () {
rotate_date=$(date +'%Y-%m-%d_%H-%M-%S')
local current_directory="$destination_dir/$backup_source/current"
local rotated_directory="$destination_dir/$backup_source/$rotate_date"
echo $rotate_date
cp -al $current_directory $rotated_directory
}
run_backup () {
local current_directory="$destination_dir/$backup_source/current"
local rsync_command="$_RSYNC $_RSYNCOPTS"
log_message "Starting backup."
if [[ ! -d $current_directory ]]; then
log_message "Creating current directory: $current_directory"
mkdir $current_directory
fi
if [[ "$_RSHELL" == "yes" ]]; then
rsync_command="$rsync_command -e \"$_RSYNCSSH\" $_RSYNCSSHOPTS $remote_host:\"$file_list\" $current_directory/."
else
rsync_command="$rsync_command $file_list $current_directory/."
fi
echo $rsync_command
eval $rsync_command
rotate_backup
}
parse_options $*
#debugging
run_backup