-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwpbackup.sh.old
102 lines (87 loc) · 3.14 KB
/
bwpbackup.sh.old
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
#!/usr/bin/env bash
#
# Copyright (C) 2020 Josh Madrone
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# bwpbackup.sh Backup Script
#
# Bash script to backup Bitnami for Wordpress stacks
#
# Configure user options in the included `.config` file.
#
# To setup CRON
# add the next line to crontab of root user to run every day at 3:30am
# 30 3 * * * /path/to/bwpbackup.sh >> /path/to/logs/bwpbackup_cron.log 2>&1
#
#
############################# options #########################################
#
# Please edit all USER OPTIONS in the `.config` file first
#
######################## script happens below #################################
#
# Load configurations
CONFIG_FILE=".config"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Configuration file does not exist."
exit 1
fi
source $CONFIG_FILE
# Check if root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Change to working directory
cd "$work_dir" || exit
# Create the backup folder
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
# check if tmp dir was created
#if [[ ! "$backup_dir" || ! -d "$backup_dir" ]]; then
# echo "Could not create temp dir"
# exit 1
#fi
# Create the logs folder
logs="${backup_dir}/logs"
if [ ! -d "$logs" ]; then
mkdir -p "$logs"
fi
# Create the log file
touch "${logs}/${log_file}"
# Stop all services
echo "Stopping Services" >>"${logs}/$log_file"
sudo /opt/bitnami/ctlscript.sh stop >>"${logs}/$log_file" 2>&1
# Compress entire directory
echo "Creating backup..." >>"${logs}/$log_file"
sudo tar -pczf "${backup_dir}/full-application-backup-${today}.tgz" /opt/bitnami >>"${logs}/$log_file" 2>&1
# Restart all services
echo "Starting Services" >>"${logs}/$log_file"
sudo /opt/bitnami/ctlscript.sh start >>"${logs}/$log_file" 2>&1
echo "Backup complete at ${backup_dir}/full-application-backup-${today}.tgz" >>"${logs}/$log_file"
# Move to S3
echo "Moving backup file to S3 bucket" >>"${logs}/$log_file"
/usr/bin/aws s3 cp "${backup_dir}/full-application-backup-${today}.tgz" "$s3_bucket/wpbackup/full-application-backup-$now.tgz" >>"${logs}/$log_file" 2>&1
#echo "Removing local backup file at $backup_dir/www-backup-$now.tgz" >> $LOG_FILE
#sudo rm $backup_dir/www-backup-$now.tgz >> $LOG_FILE 2>&1
# Delete old backups
if [ "$local_days_to_keep" -gt 0 ]; then
echo "Deleting local backups older than $local_days_to_keep days" >>"${logs}/$log_file"
find $backup_dir/*.tgz -mtime +$local_days_to_keep -exec rm {} \; >>"${logs}/$log_file" 2>&1
fi
echo "Finished backing up to S3 at" >>"${logs}/$log_file"
echo "$s3_bucket/full-application-backup-${today}.tgz" >>"${logs}/$log_file"