Difference between revisions of "SUNScholar/Disaster Recovery/Backups/Local"
m |
|||
| Line 15: | Line 15: | ||
#!/bin/bash | #!/bin/bash | ||
| + | # Setup shell to use for backups | ||
SHELL=/bin/bash | SHELL=/bin/bash | ||
| − | # | + | # Setup name of local server to be backed up |
| − | + | SERVER="scholar.sun.ac.za" | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | # Setup | + | # Setup event stamps |
DOW=`date +%a` | DOW=`date +%a` | ||
TIME=`date` | TIME=`date` | ||
| Line 32: | Line 28: | ||
FOLDER="/var/backup" | FOLDER="/var/backup" | ||
FILE="/var/log/backup-$DOW.log" | FILE="/var/log/backup-$DOW.log" | ||
| − | |||
| − | |||
| − | |||
# Do the backups | # Do the backups | ||
| Line 40: | Line 33: | ||
echo "Backup started: $TIME" | echo "Backup started: $TIME" | ||
| − | # | + | # Make the backup folder if it does not exist |
| − | if test ! -d | + | if test ! -d /var/backup |
then | then | ||
| − | mkdir -p | + | mkdir -p /var/backup |
echo "New backup folder created" | echo "New backup folder created" | ||
| + | else | ||
| + | echo "" | ||
fi | fi | ||
| + | |||
# Make sure we're in / since backups are relative to that | # Make sure we're in / since backups are relative to that | ||
cd / | cd / | ||
| Line 52: | Line 48: | ||
dpkg --get-selections > $FOLDER/installed-software-$DOW.txt | dpkg --get-selections > $FOLDER/installed-software-$DOW.txt | ||
| − | ## PostgreSQL database ( | + | ## PostgreSQL database (Needs a /root/.pgpass file) |
which -a psql | which -a psql | ||
if [ $? == 0 ] ; then | if [ $? == 0 ] ; then | ||
| − | echo "SQL dump of PostgreSQL | + | echo "SQL dump of PostgreSQL databases" |
su - postgres -c "pg_dump --inserts dspace > /tmp/dspace-db.sql" | su - postgres -c "pg_dump --inserts dspace > /tmp/dspace-db.sql" | ||
cp /tmp/dspace-db.sql $FOLDER/dspace-db-$DOW.sql | cp /tmp/dspace-db.sql $FOLDER/dspace-db-$DOW.sql | ||
| Line 61: | Line 57: | ||
fi | fi | ||
| − | # Backup MySQL database ( | + | # Backup MySQL database (Needs a /root/.my.cnf file) |
which -a mysql | which -a mysql | ||
if [ $? == 0 ] ; then | if [ $? == 0 ] ; then | ||
| − | echo "SQL dump of MySQL | + | echo "SQL dump of MySQL databases" |
mysqldump -A > $FOLDER/mysql-db-$DOW.sql | mysqldump -A > $FOLDER/mysql-db-$DOW.sql | ||
fi | fi | ||
| Line 90: | Line 86: | ||
df -h | df -h | ||
| − | |||
echo "Backup ended: $TIME" | echo "Backup ended: $TIME" | ||
| + | |||
} > $FILE | } > $FILE | ||
Revision as of 11:56, 31 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
# Setup shell to use for backups
SHELL=/bin/bash
# Setup name of local server to be backed up
SERVER="scholar.sun.ac.za"
# Setup event stamps
DOW=`date +%a`
TIME=`date`
# Setup paths
FOLDER="/var/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 /var/backup
then
mkdir -p /var/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
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)