1
|
#!/bin/bash
|
2
|
# Script to backup Redmine files to a mounted storage device
|
3
|
# with daily, weekly, monthly backup rotation
|
4
|
|
5
|
# Sysadmin email address
|
6
|
sysadmin_email="admin@domain.com"
|
7
|
|
8
|
# What to backup
|
9
|
db_dump_file="/usr/share/redmine/db/redmine-database-dump.sql"
|
10
|
|
11
|
backup_files="$db_dump_file /usr/share/redmine/files /var/hg /var/svn /etc/apache2/conf.d/hg.conf"
|
12
|
backup_files="$backup_files /etc/apache2/sites-available/redmine /etc/init/s3.conf /etc/cron.d/redmine"
|
13
|
backup_files="$backup_files /usr/local/bin/backup-redmine.sh"
|
14
|
|
15
|
# Where to backup to
|
16
|
backup_dir="/mnt/s3"
|
17
|
|
18
|
# Set database access
|
19
|
redmine_db_name="redmine"
|
20
|
redmine_db_user="redmine"
|
21
|
redmine_db_password="password"
|
22
|
|
23
|
# Encryption
|
24
|
encrypt="true"
|
25
|
secret_passphrase="secret"
|
26
|
|
27
|
# Set rotation in units of days
|
28
|
daily_remove_older_than=6
|
29
|
weekly_remove_older_than=31
|
30
|
monthly_remove_older_than=62
|
31
|
|
32
|
# Redirect stderr to a log file
|
33
|
error_log="/tmp/backup-redmine.log"
|
34
|
exec 6>&2
|
35
|
exec 2>$error_log
|
36
|
|
37
|
on_exit() {
|
38
|
# Restore IO output
|
39
|
exec 2>&6 6>$-
|
40
|
|
41
|
# Check for errors
|
42
|
if [ -s "$error_log" ]; then
|
43
|
logger -t "$0" -s "#### Backup Failed ####"
|
44
|
logger -t "$0" -s -f "$error_log"
|
45
|
cat "$error_log" | mail -s "Backup failed!" $sysadmin_email
|
46
|
else
|
47
|
logger -t "$0" -s "Backup Complete"
|
48
|
fi
|
49
|
|
50
|
# Clean up
|
51
|
rm -f $error_log
|
52
|
}
|
53
|
|
54
|
trap on_exit EXIT SIGHUP SIGINT SIGQUIT SIGTERM
|
55
|
|
56
|
# Setup variables for the archive filename.
|
57
|
hostname=$(hostname -s)
|
58
|
date_time_stamp=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2011-12-31_23h59m
|
59
|
date_stamp=`date +%Y-%m-%d` # Date p e.g 2011-12-31
|
60
|
date_day_of_week=`date +%A` # Day of the week e.g. Monday
|
61
|
date_day_of_month=`date +%e` # Date of the Month e.g. 27
|
62
|
|
63
|
# Is the backup directory mounted?
|
64
|
mount | grep -sq "$backup_dir"
|
65
|
if [ $? != 0 ]; then
|
66
|
echo "backup destination ${backup_dir} is not mounted" >&2
|
67
|
exit 1
|
68
|
fi
|
69
|
|
70
|
# Make required directories
|
71
|
[ -d "$backup_dir/monthly" ] || mkdir "$backup_dir/monthly"
|
72
|
[ -d "$backup_dir/weekly" ] || mkdir "$backup_dir/weekly"
|
73
|
[ -d "$backup_dir/daily" ] || mkdir "$backup_dir/daily"
|
74
|
|
75
|
# Delete old archives
|
76
|
find "${backup_dir}/monthly" -mtime +"$monthly_remove_older_than" -type f -name "${hostname}_*" -exec rm {} \;
|
77
|
find "${backup_dir}/weekly" -mtime +"$weekly_remove_older_than" -type f -name "${hostname}_*" -exec rm {} \;
|
78
|
find "${backup_dir}/daily" -mtime +"$daily_remove_older_than" -type f -name "${hostname}_*" -exec rm {} \;
|
79
|
|
80
|
archive_file="${backup_dir}/daily/${hostname}_${date_time_stamp}.tgz"
|
81
|
|
82
|
[ $encrypt == "true" ] && archive_file="${archive_file}.gpg"
|
83
|
|
84
|
# Dump the redmine database
|
85
|
rm -f "${db_dump_file}"
|
86
|
mysqldump --user="${redmine_db_name}" --password="${redmine_db_password}" "${redmine_db_name}" > $db_dump_file
|
87
|
|
88
|
# Write the archive file to the backup directory
|
89
|
if [ $encrypt == "true" ]; then
|
90
|
tar czP $backup_files | gpg -c -z 0 --yes --no-use-agent --passphrase="${secret_passphrase}" -o "${archive_file}"
|
91
|
else
|
92
|
tar czfP "${archive_file}" $backup_files
|
93
|
fi
|
94
|
|
95
|
# Make a weekly backup on Saturday
|
96
|
if [ $date_day_of_week == "Saturday" ]; then
|
97
|
weekly_count=$(find "${backup_dir}/weekly" -type f -name "${hostname}_${date_stamp}*" | wc -l)
|
98
|
[ $weekly_count == "0" ] && cp "${archive_file}" "${backup_dir}/weekly/"
|
99
|
fi
|
100
|
|
101
|
# Make a monthly backup on the first day of every month
|
102
|
if [ $date_day_of_month == "1" ]; then
|
103
|
monthly_count=$(find "${backup_dir}/monthly" -type f -name "${hostname}_${date_stamp}*" | wc -l)
|
104
|
[ $monthly_count == "0" ] && cp "${archive_file}" "${backup_dir}/monthly/"
|
105
|
fi
|
106
|
|
107
|
if [ -s "$error_log" ]; then
|
108
|
exit 1
|
109
|
else
|
110
|
exit 0
|
111
|
fi
|
112
|
|