Database backup¶
For a database backup you must perform the following steps: Preliminary 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
Creating 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 /var/lib/staffcop/staffcop_backup directory
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
Full path to the directory is required
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
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
default_folder="staffcop_backup"
default_path="/var/lib/staffcop"
#--------------------------
function backup_path_dialog() {
echo -e "\e[32mPath to backup database copy: '${default_path}'(Y/N)?:\e[0m"
read var1
case $var1 in
Y|y)
path=${default_path}
;;
N|n)
request_backup_path
;;
*)
error
backup_path_dialog
;;
esac
}
function request_backup_path() {
echo -e "\e[32mSpecify path to backup database copy:\e[0m"
read path
}
function error() {
echo 'Can't recognize answer'
echo 'Press Y or N'
}
if [ $1 ] ; then
path=$1
else
backup_path_dialog
fi
#free space from hdd?
TREE_ROOT=$(df --output=avail $path | sed 1d)
echo "Free space in directory '${path}': ${TREE_ROOT}"
DBPATH=$(echo $(su - postgres -c "psql -t -U postgres $DBHOST_OPTION $DBPORT_OPTION -d $DBNAME -c \"show data_directory;\""))
SIZE_DB=`du -sk $DBPATH | cut -f1`
if [[ "$TREE_ROOT" -gt "$SIZE_DB" ]]; then
echo -e "\e[32mCreate database backup copy...\e[0m"
else
echo -e "\e[31mNot enough free space for database backup copy\e[0m"
exit 1
fi
if ! [ -f /usr/bin/realpath ]; then
echo "Warning. No app realpath. Please install 'sudo apt-get install realpath'"
else
path=$(realpath $path)
fi
path=${path}/${default_folder}
echo "Backup path $path"
#Create backup path
echo "Create backup path"
mkdir -p $path
chmod 777 -R $path
if ! [ -d "$path" ] ; then
echo "directory does not exist. exit"
exit 1
fi
echo "Remove old backup files"
rm $path/* || true
#File name
full_path=${path}/${bname}
sfull_path=${path}/${sname}
cfull_path=${path}/${cname}
echo "Backup path $full_path"
echo "Setting path $sfull_path"
echo "CA path $cfull_path"
echo "------------------------------------------"
echo "Backup config"
tar --absolute-names --create --verbose --file $sfull_path /etc/staffcop
echo "Backup CA"
tar --absolute-names --create --verbose --file $cfull_path /var/lib/staffcop/CA
echo "Backup DB"
sudo -u postgres pg_dump --host=$DBHOST_OPTION $DBPORT_OPTION --verbose --compress=7 --clean --create --blobs --format=c --file=$full_path --dbname=$DBNAME
echo "------------------------------------------"
echo "Backup path"
if ! [ -f $full_path ]; then
echo 'No file db. Error'
exit 1
else
echo "File DB: $(du -h $full_path)"
fi
if ! [ -f $sfull_path ]; then
echo 'No file setting. Error'
exit 1
else
echo "File seting: $(du -h $sfull_path)"
fi
if ! [ -f $sfull_path ]; then
echo 'No file CA. Error'
exit 1
else
echo "File CA: $(du -h $sfull_path)"
fi
echo "-------------------------------------------"
echo "Finish Backup StaffCop"
Database restoring script - restore_db.sh:
bname=${DBNAME}-db.dump
sname=${DBNAME}-setting.tar
cname=${DBNAME}-sert.tar
def_folder="staffcop_backup"
def_path="/var/lib/staffcop/${def_folder}"
if [ $1 ] ; then
path=$1
else
path=${def_path}
fi
path=$(realpath $path)
full_path=${path}/${bname}
cfull_path=${path}/${cname}
if ! [ -f $full_path ]; then
echo "No db backup at $full_path"
else
echo "Restore DB ${DBNAME} from $full_path"
staffcop stop
service postgresql restart
echo "sudo -u postgres pg_restore --host=$DBHOST_OPTION --port=$DBPORT_OPTION --verbose --clean --create --format=c $full_path -d postgres"
sudo -u postgres pg_restore --host=$DBHOST_OPTION --port=$DBPORT_OPTION --verbose --clean --create --format=c $full_path -d postgres
staffcop init
echo "Done!"
fi
if ! [ -f $cfull_path ]; then
echo "No license backup at $cfull_path"
else
echo "Restore license"
rm /var/lib/staffcop/CA/*
tar --absolute-names --extract --verbose --file $cfull_path
chown -R staffcop:staffcop /var/lib/staffcop/CA/
chmod 775 -R /var/lib/staffcop/CA/
staffcop restart
fi