Skip to content

File backup script

Version: 1.5.1 Date: 2024-04-10

Having a backup of your files is essential for every day operation, whether it is on your home servers or in a very small business environment with linux servers.

This script is built by me and used every day in my environment to make it easier for me to restore files when needed.

The script can send the compressed file to offsite storage with SFTP and / or keep them locally for a configured amount of time as well.

I have it running on openSUSE and Ubuntu servers as we speak with no issues, but if you would find something not working, please let me know, you can PM me att my Twitter(X) account and i will take a look at it.

Settings

There are some settings in the script that must be set to make it tick, some settings already has a default value set but can be changed as needed.

Backup and temp folder to use during creation of compressed file or to store files locally.

vBckDir

Name prefix of compressed file, _date and .zip is added at the end, ex. prefix_date.zip.

vFilePrefix

Keep local backup files after sent to SFTP server, if no than nothing is kept locally. (no/yes)

vKeepBackup

Number of days to keep local files before pruning the backup directory, relies on vKeepBackup.

vKeepDays

Should we send the files to a Sftp server, requires the pysftp python module. (no/yes)

vSendToSftp

User for the remote server.

vSftpUser

Password for the remote server.

vSftpPass

Use key file as authenticator against remote server for SFTP.

vSftpUseKey

Full path and key to use when connecting via key file instead of username / password.

vSftpKeyFile

Destination folder on remote server.

vSftpDir

Remote server address.

vSftpHost

Remote server port.

vSftpPort

Run extra OS specific commands before backup. (no/yes)

vPreBckCmd

Run extra OS specific commands after backup. (no/yes)

vPostBckCmd

OS Commands

Depending on some settings above, these 2 must be populated as well, these are the OS commands you want to execute either before or after doing your backup and depends on the vPreBckCmd and / or vPostBckCmd being set to yes.

External OS commands to execute before continuing with the rest of the script.

vPreOsCmd

External OS commands to execute at the end of the script.

vPostOsCmd

These can contain multiple commands when needed, to run multiple command for example vPreOsCmd could look like this, there are no theoretical no limit to amount of commands you can add, but try to keep it to what is needed for the script.

vPreOsCmd=["command1","command2"]

Python virtual environment

A python virtual environment or venv as it is called is runtime version of python separated from the rest of the OS, this has several benefits, no extra modules are installed with extra permissions and you can lock it on a certain python version in case you need different versions on your system.

As of Ubuntu 23.04 this is a requirement, they have locked down the system python version and you cannot install any extra modules, might be other distributions going this way or is already there, it is better to start using a venv anyway.

Before creating the virtual environment you might need to install pythons venv package, the best thing is to try to create the venv and if it fails it will most of the times tell you what package it wants installed, install that package and try again.

A venv is often created in the users home directory and in the user running the script, be it root or other user, replace youruser with your user

Start with making sure you are placed in the home directory.

$ cd
$ pwd
/home/youruser

Or for root it would be

$ cd
$ pwd
/root

Run the following command to create a venv for this script, i set the name to oFileBackup since it is an abbreviation on my site name and script function, but you can call it whatever you like.

$ python3 -m venv .venv/oFileBackup

If above commands fails after you installed required package remove the oFileBackup folder from .venv/ directory and try again.

And if your are going to send to SFTP do the following to get the pysftp module installed.

$ source .venv/oFileBackup/bin/activate
$ pip3 install wheel
$ pip3 install pysftp
$ deactivate

Now we need to change the first line in the script from this.

#!/usr/bin/python3

To this, so we point it at our venv we just created.

#!/home/youruser/.venv/oFileBackup/bin/python

Or as root

#!/root/.venv/oFileBackup/bin/python

Remote ssh key...

You need to ssh to the remote server before running the script first time to accept the host key when using the SFTP option or the script will fail, and it must be with the user running the script later.

To run the script simply set the execution mode like below so that the owner (user & group) of the script has full permissions on the script and everybody else has none, recommended since there are passwords in the file.

$ chmod 770 ./oFileBackup.py

And this enables you to run it like this.

$ ./oFileBackup.py

Now you should have everything set up for running a backup, or almost, you still need the script, and here it comes...

The Script

The script has been tested against the following OS & Python versions.

Ubuntu Server: 22.04.x LTS / 23.04
openSUSE Leap: 15.4
Python: 3.4.4 / 3.6.15 / 3.11.4
GitHub

GitHub Invertocat logo This script now resides on GitHub and newer versions will be uploaded there from now on.

This page will still exist with information about the script and how to configure it for now...

You can reach it here: GitHub

Changelog

ChangeLog - Expand to read
Version     Date            Information
-------     ----            -----------
1.5.1       2024-04-10      Refactored most of the code to conform to standard coding practice.
1.5.0       2024-03-25      Enabled the use of key files instead of password
                            when connecting to sftp server.
1.4.0       2023-05-30      Revised, added script PreCMD and PostCMD options
1.0.0       2023-01-25      Initial release.