-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsw-nightly-db-backup.sh
72 lines (62 loc) · 2.66 KB
/
sw-nightly-db-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
#!/bin/bash
###################################################################################
##
## Title: Spacewalk Nightly Database Backup Script
## Author: metalcated <metalcated at gm@il com> & justin edmands
## Source: http://www.justinedmands.com/?q=spacewalkschemabackup
## Date: 11/05/2015
## Version: 0.1
##
## Changelog: 0.1 - Initial Release
###################################################################################
# how many days to keep backups for
dayskeep="7"
# define spacewalk schema
schema="rhnschema"
# define backup dir and file
backupdir="/var/lib/pgsql/backups"
dbfile="${schema}.postgres.`date -I`.backup"
echo -e "\n[\e[32mSpacewalk Backup\e[0m]: stopping spacewalk services"
# stop spacewalk services
/usr/sbin/spacewalk-service stop
echo -e "[\e[32mSpacewalk Backup\e[0m]: starting postgresql service"
# start postgresql service
/etc/init.d/postgresql start
# check if backup dir exists
if [[ ! -d $backupdir ]]; then
echo -e "[\e[31mSpacewalk Backup\e[0m]: faild to find target backup directory: $backupdir"
mkdir -p $backupdir
chown -R postgres: $backupdir
if [[ -d $backupdir ]]; then
echo -e "[\e[32mSpacewalk Backup\e[0m]: $backupdir created"
else
echo -e "[\e[32mSpacewalk Backup\e[0m]: $backupdir failed to create, ending script"
exit 0
fi
fi
# make db backups
echo -e "[\e[32mSpacewalk Backup\e[0m]: running postgresql db backups"
su - postgres -c "touch ${backupdir}/${dbfile}"
su - postgres -c "pg_dump ${schema} > ${backupdir}/${dbfile}"
rpm -qa|grep pigz > /dev/null 2>&1
if [[ "$?" != 0 ]]; then
echo -e "[\e[32mSpacewalk Backup\e[0m]: installing pigz compression tool"
$(which yum) install pigz -y
fi
# compress backups
echo -e "[\e[32mSpacewalk Backup\e[0m]: compressing postgresql db backup"
$(which pigz) --fast ${backupdir}/${dbfile}
echo -e "[\e[32mSpacewalk Backup\e[0m]: starting spacewalk services"
# restart all of the services
/usr/sbin/spacewalk-service start
echo -e "[\e[32mSpacewalk Backup\e[0m]: cleaning up backups older than $dayskeep days"
# cleanup backups older than x days
find ${backupdir} -type f -mtime +${dayskeep} -exec rm -f {} \;
#find ${backupdir}/${dbfile} -name "*.backup" -mtime +7 -exec rm -f {} \;
wait
# if file exists and is not blank else
if [[ -f ${backupdir}/${dbfile}.gz && -s ${backupdir}/${dbfile}.gz ]]; then
echo -e "\n[\e[32mSpacewalk Backup\e[0m]: \e[32msucessful backup made: ${backupdir}/${dbfile}.gz\e[0m, exiting PostgreSQL backup script.\n"
else
echo -e "\n[\e[31mSpacewalk Backup\e[0m]: \e[31mbackup failed\e[0m, exiting PostgreSQL backup script.\n"
fi