-
Notifications
You must be signed in to change notification settings - Fork 3
/
backupper.sh
50 lines (42 loc) · 2.03 KB
/
backupper.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
#!/bin/bash
set -eu
main () {
echo "$(date '+%d/%m/%Y %H:%M:%S') Start backuping incremental changes" >> log_incremental_backups.txt
DATE=`date +%Y-%m-%d-%H-%M`
BACKUP_SOURCE_FOLDER="/var/lib/cassandra/data"
BACKUP_ROOT_FOLDER="/var/lib/cassandra/custom_backups"
BACKUP_FOLDER=${BACKUP_ROOT_FOLDER}/$DATE
mkdir -p $BACKUP_FOLDER
KEYSPACE_QUERY=$(cqlsh -e "SELECT KEYSPACE_NAME FROM SYSTEM_SCHEMA.KEYSPACES" -u cassandra -p cassandra)
read -ra KEYSPACES <<< $KEYSPACE_QUERY
for KEYSPACE in "${KEYSPACES[@]}"
do
if [[ $KEYSPACE != *"keyspace_name"* ]] && [[ $KEYSPACE != *"--"* ]] && [[ $KEYSPACE != *"("* ]] && [[ $KEYSPACE != *")"* ]]
then
# Flush all data in memtable to sstable
echo "$(date '+%d/%m/%Y %H:%M:%S') Start backupping incremental changes for keysapce: $keyspace" >> log_incremental_backups.txt
echo "$(date '+%d/%m/%Y %H:%M:%S') Flushing incremental changes from memtable to sstable" >> log_incremental_backups.txt
nodetool -h localhost -p 7199 flush $keyspace
# Copy the incremental backup keyspace by keyspace
for dir in $BACKUP_SOURCE_FOLDER/$keyspace/*
do
TARGET_FOLDER=$BACKUP_FOLDER/$keyspace/${dir##*/}/
SOURCE_FOLDER=$dir/backups
if [ -d "$SOURCE_FOLDER" ]; then
echo "$(date '+%d/%m/%Y %H:%M:%S') Start copying incremental changes from: $TARGET_FOLDER to: $TARGET_FOLDER" >> log_incremental_backups.txt
mkdir -p $TARGET_FOLDER
cp -r -p $SOURCE_FOLDER $TARGET_FOLDER
rm -r $SOURCE_FOLDER
fi
done
fi
done
tar -czf $BACKUP_FOLDER.tgz $BACKUP_FOLDER >> /dev/null 2>&1
# Upload to S3
echo `date` -- "Uploading backups to S3 -- start" >> log_incremental_backups.txt
/usr/local/bin/aws s3 --region="xxxxxxxx" cp $BACKUP_FOLDER.tgz s3://xxx/incremental_backups/
echo `date` -- "Finished uploading" >> log_incremental_backups.txt
echo "$(date '+%d/%m/%Y %H:%M:%S') Finished backuping incremental changes" >> log_incremental_backups.txt
}
# Run main after all functions are defined
main "$@"