How to create your personal backup of your linux server

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

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/
chmod 700 /bin/

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

cd /bin

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/

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.

Here is the code to use:

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="" #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
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

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

if [ $FTP_TYPE -eq 0 ];then
    cd $BACKUP_DIR
    ftp -n -v $FTP_HOST <<EOF
    cd $REMOTE_DIR
    put ${BACKUP_FILENAME}_$TODAY.tar.gz
    put ${BACKUP_DB_FILENAME}_$TODAY.sql.gz
    delete ${BACKUP_FILENAME}_$DELETEDATE.tar.gz
elif [ $FTP_TYPE -eq 1 ];then

if [ $ONE_FILE -eq 0 ];then
elif [ $ONE_FILE -eq 1 ];then

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