SUNScholar/Disaster Recovery/Backups/Local

From Libopedia
Jump to navigation Jump to search
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 %hostname% with the hostname of your server.
  2. Replace %email-address% with the email address of the person responsible for server backups.
#!/bin/bash

# Setup shell to use for backups
SHELL=/bin/bash

# Setup name of local server to be backed up
SERVER="%hostname%"

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

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

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

# Make the backup folder if it does not exist
if test ! -d /home/backup
then
  mkdir -p /home/backup
  echo "New backup folder created"
  else
  echo ""
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 (Needs a /root/.pgpass file)
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

# Backup MySQL database (Needs a /root/.my.cnf file)
which -a mysql
if [ $? == 0 ] ; then 
    echo "SQL dump of MySQL databases"
    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" %email-address%

### 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 /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

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)


References

Crontab

Backup/Restore PostgreSQL DB