Veeam-Backup with SFTP
In a previous post I already wrote about how to create a backup with veeam to an FTP-server. However, I have encountered problems with the mounpoint in the past.
Unfortunately the server loses the connection to the remote SFTP-server and the password has to be re-entered. To avoid this I came across the script function in Veeam. There I have the possibility to execute a script before running the backup job.
So in this post I make use of the function in the hope that the backups will run by itselves and reliably in the future.
To do this, we open nano and copy the following script into it:
sudo nano /home/root/scripts/pre-job.sh
Copy this script with your parameters into a pre-job.sh file and save it to the location of your choice. In my case it was "/root/home/scripts". In the script itself there is a check if the mountpoint already exists and is active. If not, then the mount will be created again. The script will also write logs to the LOG_FILE path.
For security reasons don't save your SFTP password inside the script itself. That's why I use this:
Inside "sftp_secret" save your sftp password for your sftp connection:
Keep in mind, that veeam is using the service-user "veeam" in the users group "veeam" which needs permissions to execute the scripts otherwise the scripts will fail and give you a warning. This might depend on where you've created the script files.
chmod -R g+rwx /home/root/scripts/
chgrp -R veeam /home/root/scripts/
Now we need a post-job-script to unmount the sftp mount from our system. I kept getting problems with only the pre-job-script because veeam or linux seem to randomly close the mount by itself.
sudo nano /home/root/scripts/post-job.sh
Then you create a new backup job via cli because the veeam setup might won't let you create a new job when the destination path is on the same drive, even though you excluded the specific mountpoint.
First we need a new backup-repository for this:
veeamconfig repository create --name "SFTP" --location /mnt/backup/your-mount
After that we create a new backup-job:
veeamconfig job create filelevel --name "sftp-backup" --reponame "SFTP" --includedirs /etc,/home,/media,/opt,/root,/usr,/var --prejob "/home/root/scripts/pre-job.sh" --postjob "/home/root/scripts/post-job.sh" --daily --at 04:00
This command creates a file-level backup with the name "sftp-backup" in our recently created repository "SFTP" and includes the folders "/etc,/home,/media,/opt,/root,/usr,/var". Additionally, a pre-job and post-job is run with the scripts we created earlier. The backup is started every day at 4:00 am. You maybe get a warning about the destination path but it will still work, but not if configured in the veeam setup.
All future modifications to the job must be done via cli, otherwise you will get an error. For example: If you want to exclude some directories afterwards you can modify your backup-job like this:
veeamconfig job edit filelevel --excludedirs /home/root/mnt-0,/home/root/mnt-1/ for --name sftp-backup
Modify the command as it suits you. More info from veeam itself here.