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.
Name prefix of compressed file, _date and .zip is added at the end, ex. prefix_date.zip.
Keep local backup files after sent to SFTP server, if no than nothing is kept locally. (no/yes)
Number of days to keep local files before pruning the backup directory, relies on vKeepBackup.
Should we send the files to a Sftp server, requires the
pysftp
python module. (no/yes)
User for the remote server.
Password for the remote server.
Use key file as authenticator against remote server for SFTP.
Full path and key to use when connecting via key file instead of username / password.
Destination folder on remote server.
Remote server address.
Remote server port.
Run extra OS specific commands before backup. (no/yes)
Run extra OS specific commands after backup. (no/yes)
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.
External OS commands to execute at the end of the script.
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.
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 need to make sure you have pythons venv
package, on Ubuntu it is called python3-venv
, when creating 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, be sure to remove the venv
folder you created first or it will most likely complain.
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.
Or for root it would be
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.
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.
Now we need to change the first line in the script from this.
To this, so we point it at our venv we just created.
Or as root
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.
And this enables you to run it like this.
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.
GitHub
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.