#!/bin/bash # # D-Star Gateway Backup script # John Simpson KG4ZOW 2012-07-25 # ############################################################################### # # Copyright (C) 2012 John M. Simpson. # # 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 2 or version 3 of the # license, at your option. # # 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 . # ############################################################################### PATH="/opt/products/dstar/pgsql/bin:/usr/bin:/bin" ############################################################################### # # Configuration ######################################## # Where to store backup files. When the script runs, it will delete any # *.sql files it may find in this directory, and write a new one. It will # then create a .tar.gz file in this directory. BACKUP_DIR="/dstar/backups" BACKUP_CLEAN_DAYS=7 ######################################## # Location of the dsipvsd.conf file # The script uses this to figure out the machine's call sign. DSIPVSD_CONF="/opt/products/dstar/dstar_gw/dsipsvd/dsipsvd.conf" ######################################## # If the backup file should be emailed, specify the email address that the # message(s) should be sent FROM and TO. If you need to send the file to # multiple addresses, separate the addresses with spaces, like this: # BACKUPS_TO="user1@domain user2@domain user3@domain" # # Both variables must be present in order for any emails to be sent. #BACKUPS_FROM="dstargw@domain.xyz" #BACKUPS_TO="user@domain.xyz" ######################################## # SSH private key file and target for scp copy. # # The SCP_TARGET value should look like one of these: # SCP_TARGET="userid@hostname:/directory/" # SCP_TARGET="-P port userid@hostname:/directory/" # # Both variables must be present, and the SCP_KEY file must exist, # in order for the copy to be done. #SCP_KEY="/root/.ssh/id_rsa_backup" #SCP_TARGET="userid@hostname:/directory/" ############################################################################### # # Capture the time that the script started. Use this time to build a timestamp # for the backup filenames, and to build an RFC822 timestamp for the emails. NOW=$( date +%s ) DATE=$( date -s "@$NOW" +"%Y%m%dT%H%M%S%z" ) RFC822DATE=$( date -s "@$NOW" -R ) ############################################################################### # # Read dsipsvd.conf to get the machine's call sign. if [ -f $DSIPVSD_CONF ] then MYCALL=$( grep '^ZR_CALLSIGN=' $DSIPVSD_CONF | \ sed '{s/^.*=//;s/[[:space:]].*//;}' ) else cat < $WORKFILE COPY unsync_multicast_mng TO '$BACKUP_DIR/unsync_multicast_mng.$DATE.csv' DELIMITER ',' ; COPY unsync_user_mng TO '$BACKUP_DIR/unsync_user_mng.$DATE.csv' DELIMITER ',' ; EOF su - postgres -c "psql -U dstar dstar_global -f $WORKFILE" if [ \! -f "$BACKUP_DIR/unsync_multicast_mng.$DATE.csv" \ -o \! -f "$BACKUP_DIR/unsync_user_mng.$DATE.csv" ] then echo ERROR: one of the .csv files was not created exit 1 fi rm $WORKFILE ############################################################################### # # Build the list of files to be backed up echo "===== Building the list of files to be backed up" SCRIPT=$( which "$0" ) FILES="$BACKUP_DIR/*.csv $SCRIPT $SCP_KEY $SCP_KEY.pub" FILES="$FILES $( find /opt -name dstarmonitor.properties )" FILES="$FILES $( find /opt -name agree\* )" FILES="$FILES $( find /opt -name \*.cfg )" FILES="$FILES $( find /opt -name \*.conf )" FILES="$FILES $( find /opt -name \*.crt )" FILES="$FILES $( find /opt -name \*.key )" FILES="$FILES $( find /dstar -name \*.conf )" FILES="$FILES $( find /dstar -name \*.dv\* )" FILES="$FILES /dstar/scripts" FILES="$FILES /etc/cron.d" FILES="$FILES /etc/hosts" FILES="$FILES /etc/mail/sendmail.*" FILES="$FILES /etc/sysconfig/iptables" FILES="$FILES /etc/sysconfig/network" FILES="$FILES /etc/sysconfig/network-scripts/ifcfg-*" FILES="$FILES /var/log/ds*.log" FILES="$FILES /var/www/html" ############################################################################### # # Make the tarball BACKUP_FILE="$MYCALL-$DATE.tar.gz" echo "===== Backing up files to $BACKUP_DIR/$BACKUP_FILE" tar czf $BACKUP_DIR/$BACKUP_FILE $FILES ############################################################################### # # Possibly email the backup file if [ -n "${BACKUPS_FROM:-}" -a -n "${BACKUPS_TO:-}" ] then MID="backup.$MYCALL.$NOW.$$" SEP="sep.$MID" for RECIPIENT in $BACKUPS_TO do echo "===== Emailing backup file to $RECIPIENT" ( cat < To: <$RECIPIENT> Subject: $BACKUP_FILE Mime-Version: 1.0 Message-ID: <$MID> Content-Type: multipart/mixed; boundary="$SEP" --$SEP Content-Type: text-plain Content-Transfer-Encoding: 7bit The attached file is a backup of the $MYCALL D-Star gateway. --$SEP Content-Type: application/x-gzip; name="$BACKUP_FILE" Content-Disposition: attachment; filename="$BACKUP_FILE" Content-Transfer-Encoding: base64 EOF base64 "$BACKUP_DIR/$BACKUP_FILE" cat <