Script for a backup ?
-
At present, I clone my proxmox virtual machine for my backups and from time to time, I manually run a dump of the database and images.
I’d like to automate this backup via a cron. Do you have a script that allows me to :
- database dump
- rsync images to remote media
Perhaps you have something better to suggest? Thanks
-
@Bristow-0 you may already do this. But you mentioned you clone the vm and not back it up. Would it not be best to run a proxmox backup of the fog server VM to another location like a Nas/share located elsewhere.
-
@falko Yes, I already clone my VM on a remote space. But if my clone isn’t correct, I lose everything.
So I’d like a backup script that allows me to automate a database backup and an rsync to a remote share. You can never be too careful. -
@Bristow-0 A while back, I was trying to find an answer to your question, but I couldn’t find anything, so I just wrote it myself. It’s not perfect, but this could be a good starting point.
I have it set up on a Proxmox LXC container running Ubuntu 22.04 and it’s running on a cron job weekly. If you want to go this route, you just have to set up a few things on the container first.
- Make sure you can send emails from the container. I use
msmtp
, but you can use something else that you prefer, just make sure to change the command in the script. - Make sure you can SSH into your FOG server from the container with an SSH key so that you don’t have to deal with username/password.
- Not extremely necessary but recommended. Add a mount point, to the container, of a separate storage location to save all the data to.
- In the script, I have it set to
/mnt/FOGBackups
- In the script, I have it set to
Make sure that you read through the script and change the FOG information.
For some reason the forum doesn’t show the bash script correctly, so I am adding a link to a public gist with the code: https://gist.github.com/rluzuriaga/a2cd00cbff9a5cc70d0fb93afdd15566
Hope this helps!
- Make sure you can send emails from the container. I use
-
Thanks a lot @rodluz !
I took your script and adapted it. I use it directly on my FOG server, which simplifies things a bit. I also changed the mail to a notification sent to my smartphone with ntfy, which I find easy to use.
I also mounted my NAS share in NFS directly on /mnt/FOGBackups.
Here’s my script:
#!/bin/bash fogServerAddress="172.X.Y.Z" # Change to actual FOG server address backupDate=$(date +"%Y%m%d") backupDir="/mnt/FOGBackups/$backupDate" backupDirImages="/mnt/FOGBackups/images" snapinLocation="/opt/fog/snapins" reportLocation="/var/www/fog/lib/reports" imageLocation="/images" failedBackupDB=0 failedBackupSnapins=0 failedBackupReports=0 failedBackupImages=0 [[ ! -d $backupDir ]] && mkdir -p $backupDir/{mysql,snapins,reports,logs} >/dev/null 2>&1 [[ ! -d $backupDir/mysql || $backupDir/snapins || $backupDir/reports || $backupDir/logs ]] && mkdir -p $backupDir/{mysql,snapins,reports,logs} >/dev/null 2>&1 backupDB() { wget --no-check-certificate --post-data="nojson=1" -O $backupDir/mysql/fog.sql "http://$fogServerAddress/fog/management/export.php?type=sql" 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1 stat=$? if [[ ! $stat -eq 0 ]]; then echo "Failed to backup database!" failedBackupDB=1 else echo "Database backed up." fi } backupSnapins() { cp -r $snapinLocation $backupDir/snapins/ 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1 stat=$? if [[ ! $stat -eq 0 ]]; then echo "Failed to backup snapins!" failedBackupSnapins=1 else echo "Snapins backed up." fi } backupReports() { cp -r $reportLocation $backupDir/reports/ 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1 stat=$? if [[ ! $stat -eq 0 ]]; then echo "Failed to backup reports!" failedBackupReports=1 else echo "Reports backed up." fi } backupImages() { rsync -auv $imageLocation $backupDirImages 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1 stat=$? if [[ ! $stat -eq 0 ]]; then echo "Failed to backup images!" failedBackupImages=1 else echo "Images backed up." fi } checkForFailures() { echo "Running checkForFailures()" if [[ $failedBackupDB -eq 1 ]]; then message="$message \nFailed to backup database." fi if [[ $failedBackupSnapins -eq 1 ]]; then message="$message \nFailed to backup snapins." fi if [[ $failedBackupReports -eq 1 ]]; then message="$message \nFailed to backup reports." fi if [[ $failedBackupImages -eq 1 ]]; then message="$message \nFailed to backup images." fi } sendNotif() { echo "Running sendNotif()" if [[ ! -z $message ]]; then { curl -d "FOG Backup Failed\n $message" ntfy.sh/XYZATEHFJICH } else curl -d "FOG Backup Successful!" ntfy.sh/XYZATEHFJICH fi } backupDB backupSnapins backupReports backupImages checkForFailures sendNotif