Difference between revisions of "SUNScholar/Disaster Recovery/Backups/Local"
| Line 34: | Line 34: | ||
# Setup name | # Setup name | ||
| − | SERVER="$ | + | SERVER="$hostname%" |
# Do the backups | # Do the backups | ||
Revision as of 09:44, 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="$hostname%"
# 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" root
### 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)