ASL Backup script to personal or public FTP

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.

1 Like

Thanks for posting.
I was going to post one using rsync but never seem to have gotten around to it.

I might suggest a revision…
To fist capture .* (dot star) along with * files in your zip
as to be sure and backup .htaccess and .htpasswd files as they are hidden.
Otherwise
a restore might need to include the steps of creating them again depending on the nature of the restore.
Great work !

DUDE thank you. I completly forgot about htacces and htpasswd.
let me update it. BRB LOL

I just checked my recent backup and access and passwd are there so it is saving it.

I tried rsync and even rsftp,src and a couple others and I could not get it to work. Then I finally got a FTP going and it ran the first time.
I also had it doing a .tar file and 7zip but decided to make it a zip because even win is capable of opening those files without any other software. This is probably the 7th revision of this script if not the 10th lol

@Mike
You know more about this than I do. Any other folders or files I should add to be backed up?

I would not say I know more, perhaps done a few things a few times more.

These are the directories I backup but I do some things a bit different than some and even not recommended to be in the holy graces of linux.

/etc/asterisk
(where I place all my custom scripts that might be called from asterisk and the same with custom sound files in /subdirectories of asterisk)

/var/www/html
that captures web pages in the root dir and supermon/allmon & ?? Things that are installed as a web service.

Originally, we would place app_rpt scripts in /user/local/sbin and was in the search path.
So perhaps that some may need to contend with. Not sure if that is being used that way anymore.

If you run dvswitch, you are going to want /opt
but if you also use /opt for other stuff, you may want to specify the subdir’s

I also found the convenience of keeping a list of installed apps/programs.ie mpg123,sox etc
And yea, I still use midnight commander ‘mc’ for convenience at times.
Then made a script to reinstall all of them in the case of a full server rebuild.
That may be a bit excessive for some. That depends on how quickly you want to be back running again from the worst disaster and forgetting some of what you installed being used by custom scripts you may have wrote.
I may be overlooking something?

Not everyone has the same needs, so to each his/her own.

But I’m glad you posted that, cause it starts a needed conversation.
BTW,
if you want to dabble with rsync, here is a command I use to backup to one of my other servers manually.
But I have many depending on which server I want it to go to. Just an line example for doing this on some other server for safe keeping. I do run quite a few servers so I really find it convenient to script this stuff.

rsync -avPz -e "ssh -p 8120" /bu2/public_html/ root@144.202.xxx.xxx:/home/mike/public_html/

This preserves ownership and rights. As a bash command, It will prompt you for PW but there are options to set it in your script.
As I look at it now, this was actually a restore job on a centos system, but you get the idea. The backup was held on a debian system and that makes no difference as long as it is linux and has ssh ready for biz. OH… I find it better ‘for me’ to not zip or compress the files. It makes it easier to extract just one file. But if you need to save the space or bandwidth in tx them, I guess you have to.

I think your script is the way to go in helping others devise there own exact script based on needs.
But I might add to make it also a way to restore.
You do this much neater than myself.
LOL giving you a challenge !?
TNX