Skip to content

Instantly share code, notes, and snippets.

@zGrav
Last active October 21, 2020 09:46
Show Gist options
  • Save zGrav/725ef5c548fdd320a4c59be01e2fa1b1 to your computer and use it in GitHub Desktop.
Save zGrav/725ef5c548fdd320a4c59be01e2fa1b1 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Backing up the OVH server, checking the archive and sending it through FTP
#
BACKUPHOME=/home/backups
FTPUSER=ftpbackupser
FTPPASS=S0m3Th1nGH4rD
FTPSERVER=ftp.server.com
SERVER=youservername
MYSQLUSER=backup
MYSQLPASS=S0m4Th1nGTr1cKy
PKGLIST=$BACKUPHOME/installed-packages
DBDUMP=$BACKUPHOME/mysqldumpall.sql
ARCHIVE=$BACKUPHOME/$SERVER-backup-`date +%d-%m-%Y`.tar.bz2
ADMINEMAIL=your.email@domain.com
# create BACKUPHOME if not exists
mkdir -p $BACKUPHOME
# get installed Debian packages
dpkg --get-selections| awk -F' ' '{print $1}' > $PKGLIST
RETVAL=$?
if [ $RETVAL != 0 ];then
echo "Issue while using dpkg --get-selections of $SERVER on $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
fi
# get mysql database
mysqldump -u$MYSQLUSER -p$MYSQLPASS --all-databases > $DBDUMP
RETVAL=$?
if [ $RETVAL != 0 ];then
echo "Issue while performing mysqldump of $SERVER on $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
fi
# get directories and files
tar --preserve-permissions --preserve-order -j -c -f $ARCHIVE /etc /var /home /opt /usr/local/bin $DBDUMP \
--exclude=/var/lib/mysql/data \
--exclude=$BACKUPHOME/$SERVER-backup* \
--exclude=/var/cache/apt/archives > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL != 0 ];then
echo "Issue while making the archive from $SERVER to $FTPSERVER" | mail -s "Issue while making $ARCHIVE" $ADMINEMAIL
fi
# remove one-month-old archive on the FTP
lftp -e "rm -f $SERVER-backup-`date -d "-10 day" +%d-%m-%Y`.tar.bz2;exit" -u $FTPUSER,$FTPPASS $FTPSERVER > /dev/null 2>&1
if [ $RETVAL != 0 ];then
echo "Issue while removing the old archive of $SERVER on $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
fi
# send the archive to ftp account
lftp -e "mput $ARCHIVE;exit" -u $FTPUSER,$FTPPASS $FTPSERVER > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL != 0 ];then
echo "Issue while uploading the archive from $SERVER to $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
fi
# remove the local backups
rm -f $PKGLIST $DBDUMP $ARCHIVE
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment