Guide

How to create your personal backup of your linux server

server backup

The backstory

There is a time in every website hoster’s life when some malicious person breaks in and installs some unwanted software on their server. When it happens there are a few options for you. You can try to delete any unwanted content on your server and resume normal operation. Or you can use your backup to restore the last healthy state of your server.

This could be a problem if the backup is on the same corrupted server. Most of the providers require you to remove the malicious content before you can use the backup. The safest way is to store your backups on a separate storage. You can use another provider for this if you do not have an FTP server available.

Our solution

To help you with this we give you a short script that can automatically save your server and manage your stored personal backups. This scrip makes backup of any selected folders and databases on your server and stores it in a compressed file.

After compressing it will upload the file through FTP or SFTP to a safe storage. Of course you will need to have a working FTP or SFTP server to save your data. Our script was tested on Debian 8 and 9. In other Linux based operating systems some commands may differ.

At the first part of the scrip you can find the configuration values, where you can set up how it will work. At STORAGE_PERIOD you can set how many days worth of backup will be stored. Any older files will be deleted. At WHAT_TO_BACKUP you can set what folders or files will be saved in your backup.

Folders and filenames should be separated with one space. In the backup file the date is included, but not the hours, if you make more than one backup on the same day only the newest will be saved. At ONE_FILE you can set if the sql file from your databases will be included in the same archive or it will be saved separately. If you set the ALL_DB_BACKUP to ‘1’ every database will be saved and DB_NAME variable can be left empty.

Let’s see it in practice

You need to make sure that every package is installed. The sshpass and sftp packages only required if you want to use an SFTP connection. It is likely that some of the packages are already installed.

apt-get install sshpass
apt-get install sftp
apt-get install ftp
apt-get install mysql-client

If you are using an SFTP connection you will need to connect at the first time from the command line, because you will need to accept the certification of the server. Otherwise the script will not be able to upload your backup files.

sftp -oPort=115 sftpuser@mybackupserver.com

After this you need to accept the certification and add your password. If it is done, you can use ‘quit’ to exit.

Now you need to create folders where the temporary files will be stored. If the backup is done these files will be deleted. You can create the folders anywhere you want, but you might need to modify the BACKUP_DIR value! If you want a separate folder on the server where the backups will be stored you need to make those as well. Don’t forget about the permissions.

mkdir /backup
chmod 700 /backup

You can create the script with the first command and copy the code found at the bottom of the post. Save and exit, and after you can add permissions.

nano /bin/backup.sh
chmod 700 /bin/backup.sh

If you are done you can run the script making your first own backup.

cd /bin
./backup.sh

For automatic backups you can use the following command.

crontab -e

At the first run it will ask for the editor to use. If you don’t have a preference use nano. After the file is opened you need to insert the following code to the bottom of the file.

0 3 * * * /bin/backup.sh

This line will make a backup of your server every day at 3 am. If you want a different time or frequency you can find more info on the crontab site.

You can download the script from here, or copy the following text:

#!/bin/sh
STORAGE_PERIOD=14 #how many days store the backups
WHAT_TO_BACKUP="/home /var/www/html /srv/mytext.txt" #local directories to be included in backup If you want more directory, list them sepatare with one empty space
BACKUP_FILENAME="mybackup" #backup file filename
BACKUP_DB_FILENAME="mybackup" #db backup file filename
REMOTE_DIR="/" #directory on the remote server to backup
ONE_FILE=1 #0/1 disable/enable to put the sql file and the directories in one backup file

DB_USER="root" #db username for backup
DB_PASSWORD="123456" #db user password for backup
DB_HOST="localhost" #db server ip/hostname
DB_NAME="db1 db2 db3 db4" #The db name for backup. If you want more db, list them sepatare with one empty space
ALL_DB_BACKUP=0 #0/1 disable/enable db backup for all databases

FTP_TYPE=1 #Transfer type, 0=FTP, 1=SFTP

FTP_USER="sftpusername" #ftp/sftp server username
FTP_PASSWORD="123456" #ftp/sftp server user password
FTP_HOST="mybackupserver.com" #ftp/sftp server hostname/ip
FTP_PORT="115" #sftp server port, only when you use sftp server

BACKUP_DIR=/backup #local server directory to keep backups-> backups temp directory

#------DO NOT EDIT --------#

TODAY=`date +%d-%m-%Y`
DELETEDATE=`date --date="-$STORAGE_PERIOD day" +%d-%m-%Y`

if [ $ONE_FILE -eq 0 ];then
    if [ $ALL_DB_BACKUP -eq 0 ];then
    mysqldump --user=$DB_USER --password=$DB_PASSWORD --host=$DB_HOST --databases $DB_NAME | gzip --best > $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql.gz
    elif [ $ALL_DB_BACKUP -eq 1 ]; then
    mysqldump --user=$DB_USER --password=$DB_PASSWORD --host=$DB_HOST --all-databases | gzip --best > $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql.gz
    fi
elif [ $ONE_FILE -eq 1 ];then
    if [ $ALL_DB_BACKUP -eq 0 ];then
    mysqldump --user=$DB_USER --password=$DB_PASSWORD --host=$DB_HOST --databases $DB_NAME > $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql
    elif [ $ALL_DB_BACKUP -eq 1 ]; then
    mysqldump --user=$DB_USER --password=$DB_PASSWORD --host=$DB_HOST --all-databases > $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql
    fi
fi

if [ $ONE_FILE -eq 0 ];then
    tar -cvzf $BACKUP_DIR/${BACKUP_FILENAME}_$TODAY.tar.gz $WHAT_TO_BACKUP
elif [ $ONE_FILE -eq 1 ];then
    tar -cvf $BACKUP_DIR/${BACKUP_FILENAME}_$TODAY.tar $WHAT_TO_BACKUP
    cd $BACKUP_DIR
    tar --append --file=${BACKUP_FILENAME}_$TODAY.tar ${BACKUP_DB_FILENAME}_$TODAY.sql
    gzip ${BACKUP_FILENAME}_$TODAY.tar
fi

if [ $FTP_TYPE -eq 0 ];then
    cd $BACKUP_DIR
    ftp -n -v $FTP_HOST <<EOF
    user $FTP_USER $FTP_PASSWORD
    binary
    cd $REMOTE_DIR
    put ${BACKUP_FILENAME}_$TODAY.tar.gz
    put ${BACKUP_DB_FILENAME}_$TODAY.sql.gz
    delete ${BACKUP_FILENAME}_$DELETEDATE.tar.gz
    delete ${BACKUP_DB_FILENAME}_$DELETEDATE.sql.gz
    quit
EOF
elif [ $FTP_TYPE -eq 1 ];then
    sshpass -p $FTP_PASSWORD sftp -oPort=$FTP_PORT $FTP_USER@$FTP_HOST:$REMOTE_DIR <<EOF
    put $BACKUP_DIR/${BACKUP_FILENAME}_$TODAY.tar.gz
    put $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql.gz
    rm $REMOTE_DIR/${BACKUP_FILENAME}_$DELETEDATE.tar.gz
    rm $REMOTE_DIR/${BACKUP_DB_FILENAME}_$DELETEDATE.sql.gz
    quit
EOF
fi

if [ $ONE_FILE -eq 0 ];then
rm $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql.gz
elif [ $ONE_FILE -eq 1 ];then
rm $BACKUP_DIR/${BACKUP_DB_FILENAME}_$TODAY.sql
fi
rm $BACKUP_DIR/${BACKUP_FILENAME}_$TODAY.tar.gz

You can find us on github with more content coming very soon.

Leave a Comment

Your email address will not be published.

You may also like