Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.10 KB | None | 0 0
  1. TSTART=`date +%s`
  2. LDATE=`date "+%Y%m%d%H%M"`
  3. DBUSER='bkpuser'
  4. DBPASS='SANITIZED'
  5. BKPDIR='/backup/accounting-bkp'
  6. LOGDIR='/backup/accounting-bkp-logs'
  7. BKPLOG="${LOGDIR}/accounting_backup.log"
  8. LOCK_FILE="/tmp/accounting_backup.lock"
  9. STATUSFILE="${LOGDIR}/accounting_backup.status"
  10.  
  11. RSYNC_BIN="/usr/bin/rsync"
  12. RSYNC_OPTIONS="-azq --numeric-ids --timeout=172800 --delete-before --delete"
  13. REMOTESERVER=backups.gammanetworking.com
  14. DESTINATION="${REMOTESERVER}::`hostname`/"
  15.  
  16. #Function to write in the log file
  17. #what is passed in first param.
  18. write_to_log() {
  19.   TIMESTAMP=`date "+%b %d %T"`
  20.   echo -e "$TIMESTAMP $1" >> $BKPLOG
  21. }
  22.  
  23. #Try to get lock file
  24. exec 8>$LOCK_FILE;
  25. flock --nonblock --exclusive 8
  26. # If we were able to get exclusive lock
  27. if [ $? -eq 0 ]; then {
  28.   # Initialize the backup file
  29.   > $BKPLOG
  30.   write_to_log "[INFO] Dump of workspace_accounting database started"
  31.   # Start the dump of the database, redirect any error messages to the
  32.   # log file
  33.   /bin/mysqldump -S /var/lib/mysql/mysql.sock --user=${DBUSER} --password=${DBPASS} --databases workspace_accounting --single-transaction --add-drop-database > $BKPDIR/workspace_accounting.sql 2>${BKPLOG}
  34.   # Was the backup successful?
  35.   if [ $? -eq 0 ]; then {
  36.     #Yes, backup was successful
  37.     write_to_log "[INFO] Dump of workspace_accounting database completed successfully"
  38.     #Ship this backup over to the backup server
  39.     write_to_log "[INFO] Shipping the backup over to ${REMOTESERVER}..."
  40.     ${RSYNC_BIN} ${RSYNC_OPTIONS} ${BKPDIR} ${DESTINATION} >> $BKPLOG 2>&1
  41.     if [ $? -eq 0 ] ; then
  42.       write_to_log "[INFO] Shipment completed successfully"
  43.     else
  44.       write_to_log "[ERROR] Shipment failed, please check the log files!"
  45.     fi
  46.   } else {
  47.     # Reaching here, the backup failed for some reason
  48.     write_to_log "[ERROR] Dump failed, please check the log files"
  49.     exit 1;
  50.   }
  51.   fi
  52.   #All steps have completed successfully!
  53.   write_to_log "[INFO] All backup steps have completed successfully!"
  54.   exit 0;
  55. } else {
  56.   #Failed to get the exclusive lock
  57.   write_to_log "Can't get exclusive lock"
  58.   exit 1;
  59. }
  60. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement