Consolidate the local daily backup to a function

This commit is contained in:
Bob Mottram 2016-02-20 10:23:00 +00:00
parent efd1433b15
commit fb6d4c77d1
1 changed files with 38 additions and 80 deletions

View File

@ -1843,6 +1843,37 @@ function check_certificates {
fi
}
function backup_database_local_daily {
database_name=$1
backup_databases_script=/usr/bin/backupdatabases
echo '' >> $backup_databases_script
echo $"# Backup the ${database_name} database" >> $backup_databases_script
echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script
echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script
echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script
echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script
echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script
echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script
echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script
echo '' >> $backup_databases_script
echo ' # try to restore yesterdays database' >> $backup_databases_script
echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script
echo '' >> $backup_databases_script
echo ' # Send a warning email' >> $backup_databases_script
echo " echo \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
echo ' else' >> $backup_databases_script
echo ' # Send a warning email' >> $backup_databases_script
echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
echo ' fi' >> $backup_databases_script
echo 'else' >> $backup_databases_script
echo ' chmod 600 $TEMPFILE' >> $backup_databases_script
echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script
echo '' >> $backup_databases_script
echo ' # Make the backup readable only by root' >> $backup_databases_script
echo ' chmod 600 $DAILYFILE' >> $backup_databases_script
echo 'fi' >> $backup_databases_script
}
function install_not_on_BBB {
if grep -Fxq "install_not_on_BBB" $COMPLETION_FILE; then
return
@ -6576,32 +6607,8 @@ function install_owncloud {
# Ensure that the database gets backed up locally, if remote
# backups are not being used
backup_databases_script_header
echo '' >> /usr/bin/backupdatabases
echo $'# Backup Owncloud database' >> /usr/bin/backupdatabases
echo 'TEMPFILE=/root/owncloud.sql' >> /usr/bin/backupdatabases
echo 'DAILYFILE=/var/backups/owncloud_daily.sql' >> /usr/bin/backupdatabases
echo 'mysqldump --password="$MYSQL_PASSWORD" owncloud > $TEMPFILE' >> /usr/bin/backupdatabases
echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> /usr/bin/backupdatabases
echo 'if [ "$FILESIZE" -eq "0" ]; then' >> /usr/bin/backupdatabases
echo ' if [ -f $DAILYFILE ]; then' >> /usr/bin/backupdatabases
echo ' cp $DAILYFILE $TEMPFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # try to restore yesterdays database' >> /usr/bin/backupdatabases
echo ' mysql -u root --password="$MYSQL_PASSWORD" owncloud -o < $DAILYFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # Send a warning email' >> /usr/bin/backupdatabases
echo ' echo "Unable to create a backup of the Owncloud database. Attempted to restore from yesterdays backup" | mail -s "Owncloud backup" $EMAIL' >> /usr/bin/backupdatabases
echo ' else' >> /usr/bin/backupdatabases
echo ' # Send a warning email' >> /usr/bin/backupdatabases
echo ' echo "Unable to create a backup of the Owncloud database." | mail -s "Owncloud backup" $EMAIL' >> /usr/bin/backupdatabases
echo ' fi' >> /usr/bin/backupdatabases
echo 'else' >> /usr/bin/backupdatabases
echo ' chmod 600 $TEMPFILE' >> /usr/bin/backupdatabases
echo ' mv $TEMPFILE $DAILYFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # Make the backup readable only by root' >> /usr/bin/backupdatabases
echo ' chmod 600 $DAILYFILE' >> /usr/bin/backupdatabases
echo 'fi' >> /usr/bin/backupdatabases
backup_database_local_daily owncloud
nginx_ensite $OWNCLOUD_DOMAIN_NAME
@ -8620,7 +8627,7 @@ function install_gnu_social {
CURRENT_DDNS_DOMAIN=$MICROBLOG_DOMAIN_NAME
add_ddns_domain
microblog_nginx_site=/etc/nginx/sites-available/$MICROBLOG_DOMAIN_NAME
microblog_nginx_site=/etc/nginx/sites-available/$MICROBLOG_DOMAIN_NAME
if [[ $ONION_ONLY == "no" ]]; then
echo 'server {' > $microblog_nginx_site
echo ' listen 80;' >> $microblog_nginx_site
@ -8737,33 +8744,8 @@ function install_gnu_social {
# backups are not being used
backup_databases_script_header
if ! grep -q "GNU Social" /usr/bin/backupdatabases; then
echo '' >> /usr/bin/backupdatabases
echo $'# Backup the GNU Social database' >> /usr/bin/backupdatabases
echo 'TEMPFILE=/root/gnusocial.sql' >> /usr/bin/backupdatabases
echo 'DAILYFILE=/var/backups/gnusocial_daily.sql' >> /usr/bin/backupdatabases
echo 'mysqldump --password="$MYSQL_PASSWORD" gnusocial > $TEMPFILE' >> /usr/bin/backupdatabases
echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> /usr/bin/backupdatabases
echo 'if [ "$FILESIZE" -eq "0" ]; then' >> /usr/bin/backupdatabases
echo ' if [ -f $DAILYFILE ]; then' >> /usr/bin/backupdatabases
echo ' cp $DAILYFILE $TEMPFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # try to restore yesterdays database' >> /usr/bin/backupdatabases
echo ' mysql -u root --password="$MYSQL_PASSWORD" gnusocial -o < $DAILYFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # Send a warning email' >> /usr/bin/backupdatabases
echo ' echo "Unable to create a backup of the GNU Social database. Attempted to restore from yesterdays backup" | mail -s "GNU Social backup" $EMAIL' >> /usr/bin/backupdatabases
echo ' else' >> /usr/bin/backupdatabases
echo ' # Send a warning email' >> /usr/bin/backupdatabases
echo ' echo "Unable to create a backup of the GNU Social database." | mail -s "GNU Social backup" $EMAIL' >> /usr/bin/backupdatabases
echo ' fi' >> /usr/bin/backupdatabases
echo 'else' >> /usr/bin/backupdatabases
echo ' chmod 600 $TEMPFILE' >> /usr/bin/backupdatabases
echo ' mv $TEMPFILE $DAILYFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # Make the backup readable only by root' >> /usr/bin/backupdatabases
echo ' chmod 600 $DAILYFILE' >> /usr/bin/backupdatabases
echo 'fi' >> /usr/bin/backupdatabases
if ! grep -q "gnusocial" /usr/bin/backupdatabases; then
backup_database_local_daily gnusocial
fi
if ! grep -q "GNU Social" /etc/cron.weekly/backupdatabasesweekly; then
@ -9350,32 +9332,8 @@ function install_hubzilla {
# Ensure that the database gets backed up locally, if remote
# backups are not being used
backup_databases_script_header
echo '' >> /usr/bin/backupdatabases
echo $'# Backup the Hubzilla database' >> /usr/bin/backupdatabases
echo 'TEMPFILE=/root/hubzilla.sql' >> /usr/bin/backupdatabases
echo 'DAILYFILE=/var/backups/hubzilla_daily.sql' >> /usr/bin/backupdatabases
echo 'mysqldump --password="$MYSQL_PASSWORD" hubzilla > $TEMPFILE' >> /usr/bin/backupdatabases
echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> /usr/bin/backupdatabases
echo 'if [ "$FILESIZE" -lt "1024" ]; then' >> /usr/bin/backupdatabases
echo ' if [ -f $DAILYFILE ]; then' >> /usr/bin/backupdatabases
echo ' cp $DAILYFILE $TEMPFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # try to restore yesterdays database' >> /usr/bin/backupdatabases
echo ' mysql -u root --password="$MYSQL_PASSWORD" hubzilla -o < $DAILYFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # Send a warning email' >> /usr/bin/backupdatabases
echo ' echo "Unable to create a backup of the Hubzilla database. Attempted to restore from yesterdays backup" | mail -s "Hubzilla backup" $EMAIL' >> /usr/bin/backupdatabases
echo ' else' >> /usr/bin/backupdatabases
echo ' # Send a warning email' >> /usr/bin/backupdatabases
echo ' echo "Unable to create a backup of the Hubzilla database." | mail -s "Hubzilla backup" $EMAIL' >> /usr/bin/backupdatabases
echo ' fi' >> /usr/bin/backupdatabases
echo 'else' >> /usr/bin/backupdatabases
echo ' chmod 600 $TEMPFILE' >> /usr/bin/backupdatabases
echo ' mv $TEMPFILE $DAILYFILE' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo ' # Make the backup readable only by root' >> /usr/bin/backupdatabases
echo ' chmod 600 $DAILYFILE' >> /usr/bin/backupdatabases
echo 'fi' >> /usr/bin/backupdatabases
backup_database_local_daily hubzilla
echo '' >> /etc/cron.weekly/backupdatabasesweekly
echo '# Hubzilla' >> /etc/cron.weekly/backupdatabasesweekly