-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdatastaxagent_backup_local_disk
More file actions
executable file
·114 lines (103 loc) · 4.25 KB
/
datastaxagent_backup_local_disk
File metadata and controls
executable file
·114 lines (103 loc) · 4.25 KB
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
#!/usr/bin/env bash
#
# datastaxagent_backup_local_disk
#
# Script to recursively backup the cassandra data directory.
# Uses rsync to remove old files and copy only new files.
# License - Apache 2.0
#
#
# TODO - rsync -e ssh for security. Courtesy Matt Atwater
# TODO - don't backup the live table folder then remove the "errcode=0; ;;" 2 uses below.
# TODO - Capture list of keyspaces
# TODO - Capture each keyspace > Keyspace_name_schema.cql
# TODO - Capture hostname, hostname -i, date
#
#
# Planned change
# Compactions and write path continuously change the files to be rsynced. Rsync detects this and after completing the backup, returns errors which I replace with a 0. See
#
# 23) echo Partial transfer due to error; errcode=0; ;;
# 24) echo Partial transfer due to vanished source files; errcode=0; ;;
#
# The script was always intended to be extended to backup only the snapshot directories.
# The change would look like:
#
# rsync -avzhH --delete */snapshot "$backup_path"
# and
#
# 23) echo Partial transfer due to error; ;;
# 24) echo Partial transfer due to vanished source files; ;;
#
# allow invokation of related scripts in this dir
scriptdir=`dirname "$BASH_SOURCE"`
PATH="$scriptdir:$PATH"
# Directory to backup
source_backup_path=/mnt/cassandra
# List of backup destination paths
declare -A backup_path_list
backup_path_list[0]=/backup1/cassandra
backup_path_list[1]=/backup2/cassandra
backup_path_count=${#backup_path_list[@]}
# Choose backup destination path by modulo of date since Jan 1 1970
backup_start=$(date)
date=$(date +%F)
epoch_seconds=$(date '+%s')
epoch_day=$(echo $epoch_seconds '/60/60/24' | bc -q )
backup_path_number=$(echo $epoch_day "%$backup_path_count" | bc -q )
backup_path=${backup_path_list[$backup_path_number]}
backup_log="$backup_path/log.$date"
backup_path_files="$backup_path/files"
# Check and create directories
if [ x"$backup_path" == x ]; then echo Error: Missing backup path "$backup_path" ; exit 254; fi
if [ ! -d "$backup_path" ]; then echo Error: Backup path, "$backup_path", is not a directory; exit 253; fi
if [ ! -d "$backup_path_files" ]; then
mkdir "$backup_path_files"
chown cassandra:cassandra "$backup_path_files"
chmod 777 "$backup_path_files"
fi
exec >> "$backup_log" 2>&1 # send all output to the log
echo ''
echo backup date $backup_start
echo backup_path_count $backup_path_count
echo seconds since epoch: $epoch_seconds
echo days since epoch: $epoch_day
echo backup path number: $backup_path_number
echo backup path: "$backup_path"
if [ ! -d "$source_backup_path" ]; then echo Error: backup path "$source_backup_path" is not accessible; exit -252; fi
echo ''
# Copy files to backup
cat > /dev/null 2>&1 #read and discard the directory names
echo backing "$source_backup_path" to "$backup_path_files"
errcode=99
rsync -avzhH --delete "$source_backup_path" "$backup_path_files"
errcode=$?
printf '%s %s - ' errcode $errcode
case $errcode in
0) echo Success; ;;
1) echo Syntax or usage error; ;;
2) echo Protocol incompatibility; ;;
3) echo Errors selecting input/output files\, dirs ;;
4) echo Requested action not supported: an attempt was made to manipulate 64-bit files on a platform that cannot support them or an option was specified that is supported by the client and not by the server.; ;;
5) echo Error starting client-server protocol; ;;
6) echo Daemon unable to append to log-file; ;;
10) echo Error in socket I/O; ;;
11) echo Error in file I/O; ;;
12) echo Error in rsync protocol data stream; ;;
13) echo Errors with program diagnostics; ;;
14) echo Error in IPC code; ;;
20) echo Received SIGUSR1 or SIGINT; ;;
21) echo Some error returned by waitpid\(\); ;;
22) echo Error allocating core memory buffers; ;;
23) echo Partial transfer due to error; errcode=0; ;;
24) echo Partial transfer due to vanished source files; errcode=0; ;;
25) echo The --max-delete limit stopped deletions; ;;
30) echo Timeout in data send/receive; ;;
35) echo Timeout waiting for daemon connection; ;;
*) echo unknown status; ;;
esac
if [ $errcode == 0 ]; then
echo Backup successful. Started $backup_start. Completed $(date). ; exit $errcode
else
echo Backup failed: Started $backup_start. Aborted $(date). ; exit $errcode;
fi