It took me a while to get this working and I finally got it tested and working.
It is a simple script to backup the Supermon and Asterisk folders so if you need to reinstall ASL all you have to do is make a new image and overwrite the folders and all your hard work is there again with a little bit of tinkering it should be back up and running in no time.
I have a more simple script that backups the same folders but it leaves them on the Pi and if you don’t remember to go into the Pi and download to your computer it was in my opinion a bit useless so that is how the FTP backup came into play.
I have a CRON job that runs the script every Friday.
What does it do?
As mentioned before it backups the Supermon and Asterisk folders and makes a .ZIP file of each with the date it was made.
It then creates a folder on the root of the Pi called backups where it saves those zip files just created.
Then it uploads the zip files to the FTP server you program it to upload to.
After a successful upload, it deletes the zip files from the Pi if it’s not able to upload the files it will leave the files in the folder and create a LOG file so you can see what is wrong.
The LOG file should only contain 5 backups worth of information so that way the log file doesn’t grow and take up space on your Pi.
#!/bin/bash
#Created By N4ASS Alex D. 01/09/2024
# Set FTP credentials
ftp_user="USER-NAME"
ftp_password="PASSWORD"
ftp_server="FTP-SERVER-NAME"
ftp_port="21"
# Set variables. Change src_dir and src_dir2 to the path of the folder you want to backup
src_dir="/srv/http/supermon"
backup_dir="/backup"
log_dir="$backup_dir/logs"
date=$(date +"%m-%d-%Y")
src_dir2="/etc/asterisk"
max_backups=2
# Check if backup directory exists; if not, create it
if [ ! -d "$backup_dir" ]; then
mkdir -p "$backup_dir"
fi
# Check if log directory exists; if not, create it
if [ ! -d "$log_dir" ]; then
mkdir -p "$log_dir"
fi
# Delete older backups, keeping only the latest $max_backups.CHANGE Supermon.AllstarPersonal and Asterisk.AllstarPersonal to whatever you like. this will be the name of the final file with the date at the end.
find "$backup_dir" -name "Supermon.AllstarPersonal-*.zip" -type f -mtime +$max_backups -exec rm {} \;
find "$backup_dir" -name "Asterisk.AllstarPersonal-*.zip" -type f -mtime +$max_backups -exec rm {} \;
# Compress everything in the supermon directory to the folder backup - name it today's date. AGAIN change the name of the created file here to match the one above.SAME with the next line with the Asterisk file.
zip -r "$backup_dir/Supermon.AllstarPersonal-$date.zip" "$src_dir"
# Compress everything in the asterisk directory to the folder backup - name it today's date
zip -r "$backup_dir/Asterisk.AllstarPersonal-$date.zip" "$src_dir2"
# Upload files to FTP server. AGAIN make sure file names match here from what you renamed them.
curl -T "$backup_dir/Supermon.AllstarPersonal-$date.zip" -u "$ftp_user:$ftp_password" "ftp://$ftp_server:$ftp_port/"
curl -T "$backup_dir/Asterisk.AllstarPersonal-$date.zip" -u "$ftp_user:$ftp_password" "ftp://$ftp_server:$ftp_port/"
# Check if the upload was successful before deleting files
if [ $? -eq 0 ]; then
# Log results
echo "$(date +"%Y-%m-%d %H:%M:%S") - Backup and upload successful" >> "$log_dir/backup_log_$date.txt"
# Delete zip files after successful upload. ONE more time change to match the name you changed above to match
rm "$backup_dir/Supermon.AllstarPersonal-$date.zip"
rm "$backup_dir/Asterisk.AllstarPersonal-$date.zip"
else
# Log results
echo "$(date +"%Y-%m-%d %H:%M:%S") - Backup and upload failed" >> "$log_dir/backup_log_$date.txt"
fi
It is a TXT file when you upload to the Pi make sure you change the TXT to .sh and change permission so it can run.
Hope is useful to someone.