Database backup

For a database backup you must perform the following steps:

Make sure that:

  • There is enough free space on the hard disk.
df -h
  • There is an installed and operational version of postgresql, it is currently running.
sudo service postgresql status

Performing backup

Run the command

sudo staffcop info

If the staffcop version is higher than 3.0.233, run the command

sudo staffcop backup_db

Your backup will be placed in the /var/lib/staffcop/staffcop_backup folder

To transfer to another computer, copy this folder over the network or onto a removable drive

In case your version of the staffcop is 2.x or older than 3.0.233 run the command

wget -P /tmp/ -N http://dist.staffcop.ru/utils/script/backup/bp_install.sh && sudo bash /tmp/bp_install.sh

It will download and install the necessary software components. After its implementation you need to repeat

sudo staffcop backup_db

Restore backup

To restore the backup run the following command:

sudo staffcop restore_db

If the backup file is not in /var/lib/staffcop/staffcop_backup, the path to it should be explicitly indicated, for example:

sudo staffcop restore_db /home/support/backup

Note

The path to the folder must be specified in full!

Source codes of scripts:

  • Installer script - bp_install.sh
#!/bin/bash -e
cd ~
wget -O ~/backup_db http://dist.staffcop.ru/utils/script/backup/backup_db
cp ~/backup_db /usr/share/staffcop/bin/
chmod +x /usr/share/staffcop/bin/backup_db

wget -O ~/restore_db http://dist.staffcop.ru/utils/script/backup/restore_db \
cp ~/restore_db /usr/share/staffcop/bin/restore_db \
chmod +x /usr/share/staffcop/bin/restore_db
  • Backup creation script - backup_db.sh
#!/bin/bash -e
echo "Backup StaffCop"

if ! [ -f /bin/tar ]; then
echo "No app tar. Please install 'sudo apt-get install tar'"
exit 3
fi

if ! [ -f /usr/bin/pg_dump ]; then
echo "No app pg_dump. Exit"
exit 3
fi
#---------------------------
bname=${DBNAME}-db.dump
sname=${DBNAME}-setting.tar
cname=${DBNAME}-sert.tar
def_folder="staffcop_backup"
def_patch="/var/lib/staffcop"
#--------------------------

if [ $1 ] ;  then
patch=$1
else
patch=${def_patch}
fi

if ! [ -f /usr/bin/realpath ]; then
echo "Warning. No app realpatch. Please install 'sudo apt-get install realpath'
else
patch=$(realpath $patch)
fi

patch=${patch}/${def_folder} \
echo "Backup patch $patch"

#Create backup patch \
echo "Create backup patch" \
mkdir -p $patch \
chmod 777 -R $patch

if ! [ -d  "$patch" ] ; then \
echo "directory does not exist. exit" \
exit 1 \
fi \
echo "Remove old backup files" \
rm $patch/* || true

#File name \
full_patch=${patch}/${bname} \
sfull_patch=${patch}/${sname} \
cfull_patch=${patch}/${cname}

echo "Backup patch $full_patch" \
echo "Setting patch $sfull_patch" \
echo "CA patch $cfull_patch" \
echo "------------------------------------------" \
echo "Backup config" \
tar --absolute-names --create --verbose --file $sfull_patch /etc/staffcop

echo "Backup CA" \
tar --absolute-names --create --verbose --file $cfull_patch /var/lib/staffcop/CA

echo "Backup DB" \
sudo -u postgres pg_dump --host=$DBHOST_OPTION --port=$DBPORT_OPTION  --verbose                                                                                         --compress=7 --clean --create --blobs --format=c --file=$full_patch --dbname=$DB                                                                                        NAME

echo "------------------------------------------" \
echo "Backup patch"

if ! [ -f $full_patch ]; then \
echo 'No file db. Error' \
exit 1 \
else \
echo "File DB: $(du -h $full_patch)" \
fi

if ! [ -f $sfull_patch ]; then \
echo 'No file setting. Error' \
exit 1 \
else \
echo "File seting: $(du -h $sfull_patch)" \
fi

if ! [ -f $sfull_patch ]; then \
echo 'No file CA. Error' \
exit 1 \
else \
echo "File CA: $(du -h $sfull_patch)" \
fi
  • Database recovery script - restore_db.sh:
#!/bin/bash
#---------------------------
bname=${DBNAME}-db.dump
sname=${DBNAME}-setting.tar
cname=${DBNAME}-sert.tar
def_folder="staffcop_backup"
def_patch="/var/lib/staffcop/${def_folder}"
#--------------------------

if [ $1 ] ;  then
        patch=$1
else
        patch=${def_patch}
fi

if ! [ -f /usr/bin/realpath ] ; then
 echo "Warning. No app realpatch. Please install 'sudo apt-get install realpath'"
else
 patch=$(realpath $patch)
fi

if ! [ -d  "$patch" ] ; then
        echo "directory does not exist. exit"
        exit 1
fi

echo "Restore patch $patch"

full_patch=${patch}/${bname}
cfull_patch=${patch}/${cname}

if ! [ -f $full_patch ]; then
        echo "No backup db"
else
        echo "Restore backup staffcop DB ${DBNAME}"
        echo "Stop service staffcop"
        service staffcop stop
        echo "Restart PG"
        service postgresql restart
        echo "Start restore ${DBNAME}"

        sudo -u postgres pg_restore  --host=$DBHOST_OPTION --port=$DBPORT_OPTION  --verbose --clean --create  --format=c $full_patch -d postgres
        echo "Analyse ${DBNAME}"
        echo "analyze;" | sudo -u postgres psql ${DBNAME}
        echo "Vacuum"
        echo "vacuum full analyze;" | sudo -u postgres psql ${DBNAME}
        echo "Reindex"
        echo "reindex database ${DBNAME};" | sudo -u postgres psql ${DBNAME}
        staffcop init
        echo "Sessions reset"
        staffcop sessions_reset full
        echo "Finish restore  ${DBNAME}"
        staffcop info
fi

echo "Certificate"
if ! [ -f $cfull_patch ]; then
        echo "No backup sert"
else
        service staffcop stop
        service nginx stop
        rm /var/lib/staffcop/CA/*
        tar --absolute-names --extract --verbose --file $cfull_patch
        chown -R staffcop:staffcop /var/lib/staffcop/CA/
        chmod 775 -R /var/lib/staffcop/CA/
        service staffcop start
        service nginx start
fi
service staffcop start