How to Setup Automated Folder Backups

Published

Before I type the rest, I will admit that I'm far from an expert on Linux or bash. I did a lot of googling to arrive at the script and process that I have/use, but it's been tested and it works on my server running Debian 7.

To make any use of this tutorial, you will first need to install 7-Zip on your machine or server. This can be done with the following command although you may need administrative privileges to use it.

    
        apt-get install p7zip-full -y
    

Now that you have 7-Zip installed, you can create a backup script to be run either manually or automatically using a cronjob. This first example will backup the specified folder and save it in the format fileName_year_month_day.7z and then, as an optional step if you wish to use it, the script will find all files/folders within the specified folder that were created more than fourteen days ago and it will delete them.

    
        #!/bin/bash

        #Get the date in the format of YYY_MM_DD:
        DATE=$(date + '%Y_%m_%d')

        #Backup the /var/www/ folder and save it to /home/folder/ as www_YYY_MM_DD.7z
        7z a /home/folder/www_$DATE.7z /var/www/

        #Delete all backups older than 14 days in the /home/folder/ folder:
        find /home/folder/ -mtime +14 -delete
    

The script above works perfectly fine in most cases, but there may be a few special cases where you want to backup everything in a folder except for a specific file or folder.

Let's assume that you want to backup the entire www folder, but you don't want to backup the uploaded_files folder that's within the www folder. In this case you can use the -xr! argument to exclude this folder. This also works for files as well.

    
        #!/bin/bash

        #Get the date in the format of YYY_MM_DD:
        DATE=$(date + '%Y_%m_%d')

        #Backup the /var/www/ folder and save it to /home/folder/ as www_YYY_MM_DD.7z
        #, but don't include the uploaded_files folder in the backup.
        7z a /home/folder/www_$DATE.7z /var/www/ =mx0 -xr!uploaded_files

        #Delete all backups older than 14 days in the /home/folder/ folder:
        find /home/folder/ -mtime +14 -delete
    

After creating the script file, you'll need to add a cronjob entry for it to run once per-day and set the permissions of the script file. If you don't already have cron installed, then run the following command.

    
        apt-get install cron
    

Now that cron is installed, go to the location of the script file and enter the command chmod x+u scriptFile.sh. Next, enter the command crontab -e, then scroll to the bottom of the file that opens and type @daily /path/to/the/bash/script/backup_databases.sh. Now save the file and whichever folders you specify should automatically keep backups for up to fourteen days.

If anything is unclear or could use some more explanation, just comment below. If you require support, I'll do my best, but I'm no expert at this.

To unpack a backup using 7-Zip, you can use the following command.

    
        #Unpacks the archive www_YYY_MM_DD.7z and maintains folder structure.
        7za x www_YYYY_MM_DD.7z