-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira_SFTP_backup.sh
153 lines (129 loc) · 6.01 KB
/
jira_SFTP_backup.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
#!/bin/bash
set -e
set -x
export key_of_ccryptkey="/root/backup_script/key.txt"
export date_regex=""$(date +"%Y-%m-%d-%H-%M-%S")
export global_jira_backup_dir="/var/backups/jira-backups"
export daily_backup_dir="$global_jira_backup_dir/daily"
export weekly_backup_dir="$global_jira_backup_dir/weekly"
export monthly_backup_dir="$global_jira_backup_dir/monthly"
export log_dir="$global_jira_backup_dir/log"
export specific_backup_dir="$global_jira_backup_dir/$date_regex"
export jira_db_docker_volume_backup_dir="$specific_backup_dir/jira_db_docker_volume"
main() {
create_key_for_encrypt
create_specific_backup_dir_and_log_dir
create_timely_backup_dir
get_backup_from_jira_files
create_sub_dir_and_get_backup_from_jiradb
tar_specific_backup_dir_then_encrypt
remove_specific_backup_dir
move_tar_backup_file_to_timely_dirs
remove_first_tar_backup_file
send_daily_file_to_sftp_server
send_weekly_file_to_sftp_server
send_monthly_file_to_sftp_server
removing_local_files_older_than_seven_days_from_daily_dir
removing_local_files_older_than_five_weeks_from_weekly_dir
removing_local_files_older_than_five_months_from_monthly_dir
remove_first_character_from_log_files
removing_remote_files_older_than_seven_days_from_sftp_daily_dir
removing_remote_files_older_than_five_weeks_from_sftp_weekly_dir
removing_remote_files_older_than_five_months_from_sftp_monthly_dir
check_backup_file_size_and_send_notification_mail
}
create_key_for_encrypt () {
echo "your_password_for_encryption" > $key_of_ccryptkey
}
create_specific_backup_dir_and_log_dir() {
mkdir -p $specific_backup_dir
mkdir -p $log_dir
}
create_timely_backup_dir() {
mkdir -p $daily_backup_dir
mkdir -p $weekly_backup_dir
mkdir -p $monthly_backup_dir
}
get_backup_from_jira_files() {
cp -r /root/jira/ "$specific_backup_dir/"
}
create_sub_dir_and_get_backup_from_jiradb() {
mkdir -p "$jira_db_docker_volume_backup_dir"
cp -r /var/lib/docker/volumes/jira_postgresqldata "$jira_db_docker_volume_backup_dir/"
}
tar_specific_backup_dir_then_encrypt() {
tar -czvf "$global_jira_backup_dir/$date_regex-jira.tar.gz" $specific_backup_dir
pushd $global_jira_backup_dir/
ccrypt -e -k $key_of_ccryptkey "$date_regex-jira.tar.gz"
popd
}
remove_specific_backup_dir() {
rm -rf $specific_backup_dir
}
move_tar_backup_file_to_timely_dirs() {
cp "$global_jira_backup_dir/$date_regex-jira.tar.gz.cpt" $daily_backup_dir/
if [[ $(date +%u) -eq 6 ]]; then
cp "$global_jira_backup_dir/$date_regex-jira.tar.gz.cpt" $weekly_backup_dir/
fi
if [[ $(date +%d) -eq 29 ]]; then
cp "$global_jira_backup_dir/$date_regex-jira.tar.gz.cpt" $monthly_backup_dir/
fi
}
remove_first_tar_backup_file() {
rm -rf "$global_jira_backup_dir/$date_regex-jira.tar.gz.cpt"
}
send_daily_file_to_sftp_server() {
sudo sshpass -p 'your_sftp_password' sftp your_sftp_server:your_sftp_location <<< "put $daily_backup_dir/$date_regex-jira.tar.gz.cpt"
}
send_weekly_file_to_sftp_server() {
if [[ $(date +%u) -eq 6 ]]; then
sudo sshpass -p 'your_sftp_password' sftp your_sftp_server:your_sftp_location <<< "put $weekly_backup_dir/$date_regex-jira.tar.gz.cpt"
fi
}
send_monthly_file_to_sftp_server() {
if [[ $(date +%d) -eq 29 ]]; then
sudo sshpass -p 'your_sftp_password' sftp your_sftp_server:your_sftp_location <<< "put $monthly_backup_dir/$date_regex-jira.tar.gz.cpt"
fi
}
removing_local_files_older_than_seven_days_from_daily_dir() {
find $daily_backup_dir/ -type f -mtime +7 -name '*.gz.cpt' > $log_dir/daily_must_be_remove.log
find $daily_backup_dir/ -type f -mtime +7 -name '*.gz.cpt' -print0 | xargs -r0 rm --
}
removing_local_files_older_than_five_weeks_from_weekly_dir() {
find $weekly_backup_dir/ -type f -mtime +35 -name '*.gz.cpt' > $log_dir/weekly_must_be_remove.log
find $weekly_backup_dir/ -type f -mtime +35 -name '*.gz.cpt' -print0 | xargs -r0 rm --
}
removing_local_files_older_than_five_months_from_monthly_dir() {
find $monthly_backup_dir/ -type f -mtime +150 -name '*.gz.cpt' > $log_dir/monthly_must_be_remove.log
find $monthly_backup_dir/ -type f -mtime +150 -name '*.gz.cpt' -print0 | xargs -r0 rm --
}
remove_first_character_from_log_files() {
sed -i 's/^..//' $log_dir/daily_must_be_remove.log
sed -i 's/^..//' $log_dir/weekly_must_be_remove.log
sed -i 's/^..//' $log_dir/monthly_must_be_remove.log
}
removing_remote_files_older_than_seven_days_from_sftp_daily_dir(){
while read line; do
sudo sshpass -p 'your_sftp_password' sftp your_sftp_server:your_sftp_location <<< "rm $line"
done < $log_dir/daily_must_be_remove.log
}
removing_remote_files_older_than_five_weeks_from_sftp_weekly_dir(){
while read line; do
sudo sshpass -p 'your_sftp_password' sftp your_sftp_server:your_sftp_location <<< "rm $line"
done < $log_dir/weekly_must_be_remove.log
}
removing_remote_files_older_than_five_months_from_sftp_monthly_dir(){
while read line; do
sudo sshpass -p 'your_sftp_password' sftp your_sftp_server:your_sftp_location <<< "rm $line"
done < $log_dir/monthly_must_be_remove.log
}
check_backup_file_size_and_send_notification_mail() {
minimumsize=50000
actualsize=$(du -k "$daily_backup_dir/$date_regex-jira.tar.gz.cpt" | cut -f 1)
if [ $actualsize -ge $minimumsize ]; then
echo "about jira ; backup was successful ; size of backup file is : $(echo $actualsize ) kilobytes ; name of backup file is : $(echo $date_regex-jira.tar.gz.cpt)" | mail -s "jira backup" your_email_address
else
echo "about jira ; backup was not successful ; size of backup file is : $(echo $actualsize ) kilobytes " | mail -s "jira backup" your_email_address
fi
}
main