SUNScholar/Disaster Recovery/Backups/Local

Back to Client Setup

First click here to create a PostgreSQL credentials file.

Create local backup script
Here is a sample script to make local backups which are then sent to the remote backup server.

Type the following. sudo nano /usr/local/bin/backup.sh Now copy and paste the following into the open editor and modify the backup variables to suit your location and server.


 * 1) Replace %email-address% with the email address of the person responsible for server backups.
 * 1) Replace %email-address% with the email address of the person responsible for server backups.


 * 1) !/bin/bash

SHELL=/bin/bash
 * 1) Setup shell to use for backups

SERVER="%hostname%"
 * 1) Setup name of local server to be backed up

DOW=`date +%a` TIME=`date`
 * 1) Setup event stamps

FOLDER="/home/backup" FILE="/var/log/backup-$DOW.log"
 * 1) Setup paths

{ echo "Backup started: $TIME"
 * 1) Do the backups

if test ! -d /home/backup then mkdir -p /home/backup echo "New backup folder created" else echo "" fi
 * 1) Make the backup folder if it does not exist

cd /
 * 1) Make sure we're in / since backups are relative to that

dpkg --get-selections > $FOLDER/installed-software-$DOW.txt
 * 1) Get a list of the installed software

which -a psql if [ $? == 0 ] ; then echo "SQL dump of PostgreSQL databases" su - postgres -c "pg_dump --inserts dspace > /tmp/dspace-db.sql" cp /tmp/dspace-db.sql $FOLDER/dspace-db-$DOW.sql su - postgres -c "vacuumdb --analyze dspace > /dev/null 2>&1" fi
 * 1) PostgreSQL database (Needs a /root/.pgpass file)

which -a mysql if [ $? == 0 ] ; then echo "SQL dump of MySQL databases" mysqldump -A > $FOLDER/mysql-db-$DOW.sql fi
 * 1) Backup MySQL database (Needs a /root/.my.cnf file)

echo "Archive '/etc' folder" tar czf $FOLDER/etc-$DOW.tgz etc/
 * 1) Backup '/etc' folder

echo "Archive '/root' folder" tar czf $FOLDER/root.tgz root/
 * 1) Backup '/root' folder

echo "Archive '/usr/local' folder" tar czf $FOLDER/usr-local.tgz usr/local/
 * 1) Backup '/usr/local' folder

echo "" echo "** Backup folder **" ls -lhS $FOLDER
 * 1) View backup folder

echo "" echo "** Disk usage **" df -h
 * 1) Show disk usage

TIME=`date` echo "Backup ended: $TIME"

} > $FILE

cat $FILE | mail -s "BACKUP : $DOW : $SERVER" %email-address%
 * 1) Prepare email


 * 1) EOF ###

Now we make the backup script executable. sudo chmod 0755 /usr/local/bin/backup.sh

Invoke Local Manual Backup
After you have completed the above, you can start a backup anytime by typing the following as the root user: sudo /usr/local/bin/backup.sh Then check the files in the backup folder by typing the following: sudo ls -lh /home/backup

Setup Daily Backup Job
A cron job entry is added to the root crontab to run the script at midnight each day. To add the script to the root crontab type the following as the root user: sudo crontab -e Type the following to run backups at midnight: @midnight /usr/local/bin/backup.sh Or alternatively at 05h00 in the morning in order to reduce system load at midnight. 00 5 * * * /usr/local/bin/backup.sh

Crontab

 * http://www.crontab-generator.org
 * http://en.wikipedia.org/wiki/Cron
 * http://adminschoice.com/crontab-quick-reference
 * http://www.yourownlinux.com/2014/04/schedule-your-jobs-in-linux-with-cron-examples-and-tutorial.html

Backup/Restore PostgreSQL DB

 * http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postgres-database-using-pg_dump-and-psql
 * https://www.commandprompt.com/blog/a_better_backup_with_postgresql_using_pg_dump
 * http://postgresguide.com/utilities/backup-restore.html
 * http://www.enterprisedb.com/resources-community/tutorials-quickstarts/linux/how-use-pgdump-and-pgrestore-postgres-plus-tutorial-
 * https://www.pgadmin.org