Difference between revisions of "SUNScholar/Disaster Recovery/Backups/Local"

From Libopedia
Jump to navigation Jump to search
Line 15: Line 15:
 
#!/bin/bash
 
#!/bin/bash
  
## Setup the backup variables ##
+
SHELL=/bin/bash
LOCAL_SERVER="%hostname%"
+
 
LOCAL_FOLDER="/var/backup"
+
# Make the backup folder
BACKUP_LOGFILE="/var/log/backup.log"
+
if test ! -d /var/backup
# Day Of the Week
+
then
 +
  mkdir -p /var/backup
 +
  else
 +
  echo ""
 +
fi
 +
 
 +
# Setup date stamps
 
DOW=`date +%a`
 
DOW=`date +%a`
 
TIME=`date`
 
TIME=`date`
  
 +
# Setup paths
 +
FOLDER="/var/backup"
 +
FILE="/var/log/backup-$DOW.log"
 +
 +
# Setup name
 +
SERVER="scholar.sun.ac.za"
 +
 +
# Do the backups
 
{
 
{
## Timestamp the beginning of the backup ##
+
echo "Backup started: $TIME"
echo "Backup for $LOCAL_SERVER started: $TIME"
 
  
## Check that we have a backup folder ##
+
# Check we have a backup folder
if [ ! -d $LOCAL_FOLDER ]; then
+
if test ! -d $FOLDER
   mkdir -p $LOCAL_FOLDER
+
then
 +
   mkdir -p $FOLDER
 
   echo "New backup folder created"
 
   echo "New backup folder created"
  else
 
  echo "Backup started: $TIME"
 
 
fi
 
fi
 +
# Make sure we're in / since backups are relative to that
 +
cd /
  
## Make sure we're in / since backups are relative to that ##
+
# Get a list of the installed software
cd /
+
dpkg --get-selections > $FOLDER/installed-software-$DOW.txt
 +
 
 +
## PostgreSQL database (Check for a root .pgpass file)
 +
which -a psql
 +
if [ $? == 0 ] ; then
 +
    echo "SQL dump of PostgreSQL dspace database"
 +
    su - postgres -c "pg_dump --inserts dspace > /tmp/dspace-db.sql"
 +
    cp /tmp/dspace-db.sql /opt/backup/dspace-db-$DOW.sql
 +
    su - postgres -c "vacuumdb --analyze dspace > /dev/null 2>&1"
 +
fi
  
## Get a list of the installed software ##
+
# Backup MySQL database (Check for a root .my.cnf file)
dpkg --get-selections > $LOCAL_FOLDER/installed-software.$DOW
+
which -a mysql
 +
if [ $? == 0 ] ; then
 +
    echo "SQL dump of MySQL database"
 +
    mysqldump -A > $FOLDER/mysql-db-$DOW.sql
 +
fi
  
## Backup the server config files ##
+
# Backup '/etc' folder
 
echo "Archive '/etc' folder"
 
echo "Archive '/etc' folder"
tar czf $LOCAL_FOLDER/etc.tgz.$DOW etc/
+
tar czf $FOLDER/etc-$DOW.tgz etc/
  
## Backup the '/root' folder ##
+
# Backup '/root' folder
 
echo "Archive '/root' folder"
 
echo "Archive '/root' folder"
tar czf $LOCAL_FOLDER/root.tgz root/
+
tar czf $FOLDER/root.tgz root/
  
## Backup the '/usr/local' folder which houses customised software ##
+
# Backup '/usr/local' folder
 
echo "Archive '/usr/local' folder"
 
echo "Archive '/usr/local' folder"
tar czf $LOCAL_FOLDER/usr-local.tgz usr/local/
+
tar czf $FOLDER/usr-local.tgz usr/local/
  
## Backup the DSpace postgres database which houses the catalog of the digital assets ##
+
# View backup folder
su - postgres -c "pg_dump dspace > /tmp/dspace-db.sql"
+
echo ""
cp /tmp/dspace-db.sql $LOCAL_FOLDER/dspace-db.sql-$DOW
+
echo "** Backup folder **"
su - postgres -c "vacuumdb --analyze dspace > /dev/null 2>&1"
+
ls -lhS $FOLDER
  
### Optional as an example for MySQL on other servers ###
+
# Show disk usage
## Backup MySQL database (Check for a root .my.cnf file) ##
+
echo ""
#which -a mysql
+
echo "** Disk usage **"
#if [ $? == 0 ] ; then
+
df -h
#    echo "SQL dump of MySQL database"
 
#    mysqldump -A > $FOLDER/mysql-db-$DOW.sql
 
#fi
 
  
## View the backup folder ##
 
ls -lhS $LOCAL_FOLDER
 
 
## Timestamp the end of the backup ##
 
 
TIME=`date`
 
TIME=`date`
echo "Backup for $LOCAL_SERVER ended: $TIME"
+
echo "Backup ended: $TIME"
} > $BACKUP_LOGFILE
+
} > $FILE
 
 
## Make a daily copy of the backup log file ##
 
cp $BACKUP_LOGFILE $BACKUP_LOGFILE.$DOW
 
 
 
## Email the backup logfile to the root user ##
 
cat $FILE | mail -s "BACKUP : $DOW : $LOCAL_SERVER" root
 
  
### EOF ###
+
# Prepare email
</pre>
+
cat $FILE | mail -s "BACKUP : $DOW : $SERVER" hgibson@sun.ac.za
 +
### EOF ###</pre>
  
 
{{NANO}}
 
{{NANO}}

Revision as of 09:42, 10 July 2014

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. Replace %hostname% with the hostname of your server.

#!/bin/bash

SHELL=/bin/bash

# Make the backup folder
if test ! -d /var/backup
then
  mkdir -p /var/backup
  else
  echo ""
fi

# Setup date stamps
DOW=`date +%a`
TIME=`date`

# Setup paths
FOLDER="/var/backup"
FILE="/var/log/backup-$DOW.log"

# Setup name
SERVER="scholar.sun.ac.za"

# Do the backups
{
echo "Backup started: $TIME"

# Check we have a backup folder
if test ! -d $FOLDER
then
  mkdir -p $FOLDER
  echo "New backup folder created"
fi
# Make sure we're in / since backups are relative to that
cd /

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

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

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

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

# Backup '/root' folder
echo "Archive '/root' folder"
tar czf $FOLDER/root.tgz root/

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

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

# Show disk usage
echo ""
echo "** Disk usage **"
df -h

TIME=`date`
echo "Backup ended: $TIME"
} > $FILE

# Prepare email
cat $FILE | mail -s "BACKUP : $DOW : $SERVER" hgibson@sun.ac.za
### EOF ###

NANO Editor Help
CTL+O = Save the file and then press Enter
CTL+X = Exit "nano"
CTL+K = Delete line
CTL+U = Undelete line
CTL+W = Search for %%string%%
CTL+\ = Search for %%string%% and replace with $$string$$
CTL+C = Show line numbers

More info = http://en.wikipedia.org/wiki/Nano_(text_editor)


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 /var/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
@midnight /usr/local/bin/backup.sh

NANO Editor Help
CTL+O = Save the file and then press Enter
CTL+X = Exit "nano"
CTL+K = Delete line
CTL+U = Undelete line
CTL+W = Search for %%string%%
CTL+\ = Search for %%string%% and replace with $$string$$
CTL+C = Show line numbers

More info = http://en.wikipedia.org/wiki/Nano_(text_editor)