How do I (automatically) backup the yunohost server?

My configuration is more or less similar to the one used by @Nido , except that my “external” backup is just a folder I configured in Nextcloud, and it gets backed up to my other devices (a desktop computer, and my laptop). I just placed this script on /etc/cron.weekly to run weekly:

#!/bin/bash
defaultfolder="/home/yunohost.backup/archives"
#I use the multimedia folder to store my archives. Substitute the target folder for your own user.
targetfolder="/home/yunohost.multimedia/csolisr/archives/ynh_backup"
#Compression format. I generally use .gz as that's the format that Yunohost uses natively to compress.
cmp="gz"
#You can set this parameter to have multiple copies of each backup.
maxbackupnumber=1
#This iterates through all your installed YNH apps
for app in $(/usr/bin/yunohost app list | /usr/bin/grep "id:" | /usr/bin/sed "s/.*id\: //g")
do
        appbackup="$app"_backup
        #If old backup exists, find a free spot and rename it
        if [[ ! -z $(/usr/bin/yunohost backup list | /usr/bin/grep $appbackup) ]]
        then
                spotfound=0
                backupnumber=1
                while [[ spotfound -eq 0 ]]
                do
                        if [[ -f "$defaultfolder/$appbackup_$backupnumber.tar" || -f "$defaultfolder/$appbackup_$backupnumber.tar.$cmp" ]]
                        then
                                backupnumber=$((backupnumber+1))
                        else
                                spotfound=1
                        fi
                done
                if [[ $backupnumber -lt $maxbackupnumber ]]
                then
                        appbackupnumber="$appbackup"_"$backupnumber"
                        mv "$defaultfolder/$appbackup.info.json" "$defaultfolder/$appbackupnumber.info.json" -v
                        mv "$defaultfolder/$appbackup.tar" "$defaultfolder/$appbackupnumber.tar" -v #|| \
                        mv "$defaultfolder/$appbackup.tar.$cmp" "$defaultfolder/$appbackupnumber.tar.$cmp" -v
                        #Do the same in the backup target
                        mv "$targetfolder/$appbackup.info.json" "$targetfolder/$appbackupnumber.info.json" -v
                        mv "$targetfolder/$appbackup.tar.$cmp" "$targetfolder/$appbackupnumber.tar.$cmp" -v
                else
                        if [[ $backupnumber -eq 1 ]]
                        then
                                appbackupnumber="$appbackup"
                        else
                                appbackupnumber="$appbackup"_"$backupnumber"
                        fi
                        rm "$defaultfolder/$appbackupnumber.info.json"
                        rm "$defaultfolder/$appbackupnumber.tar" #|| \
                        rm "$defaultfolder/$appbackupnumber.tar.$cmp"
                        #Do the same in the backup target
                        rm "$targetfolder/$appbackupnumber.info.json"
                        rm "$targetfolder/$appbackupnumber.tar.$cmp"
                        /usr/bin/yunohost backup delete "$appbackupnumber"
                fi
        fi
        #Create the backup
        /usr/bin/yunohost backup create --apps $app --name "$appbackup"
        #Compress and move the backup
        cp "$defaultfolder/$appbackup.info.json" "$targetfolder/$appbackup.info.json" -v
        cp "$defaultfolder/$appbackup.tar.$cmp" "$targetfolder/" -v || \
        cp "$targetfolder/$appbackup.tar.$cmp" "$defaultfolder/" -v
done
#System = ynh_core_backup
if [[ ! -z $(/usr/bin/yunohost backup list | /usr/bin/grep "ynh_core_backup") ]]
then
        spotfound=0
        backupnumber=1
        while [[ spotfound -eq 0 ]]
        do
                if [[ -f "$defaultfolder/ynh_core_backup_$backupnumber.tar" || -f  "$defaultfolder/ynh_core_backup_$backupnumber.tar.$cmp" ]]
                then
                        backupnumber=$((backupnumber+1))
                else
                        spotfound=1
                fi
                done
        if [[ $backupnumber -lt $maxbackupnumber ]]
        then
                appbackupnumber=ynh_core_backup_"$backupnumber"
                mv "$defaultfolder/ynh_core_backup.info.json" "$defaultfolder/$appbackupnumber.info.json" -v
                mv "$defaultfolder/ynh_core_backup.tar" "$defaultfolder/$appbackupnumber.tar" -v #|| \
                mv "$defaultfolder/ynh_core_backup.tar.$cmp" "$defaultfolder/$appbackupnumber.tar.$cmp" -v
                #Do the same in the backup target
                mv "$targetfolder/ynh_core_backup.info.json" "$targetfolder/$appbackupnumber.info.json" -v
                mv "$targetfolder/ynh_core_backup.tar.$cmp" "$targetfolder/$appbackupnumber.tar.$cmp" -v
        else
                if [[ $backupnumber -eq 1 ]]
                then
                        appbackupnumber="ynh_core_backup"
                else
                        appbackupnumber=ynh_core_backup_"$backupnumber"
                fi
                rm "$defaultfolder/$appbackupnumber.info.json"
                rm "$defaultfolder/$appbackupnumber.tar" #|| \
                rm "$defaultfolder/$appbackupnumber.tar.$cmp"
                #Do the same in the backup target
                rm "$targetfolder/$appbackupnumber.info.json"
                rm "$targetfolder/$appbackupnumber.tar.$cmp"
                /usr/bin/yunohost backup delete "$appbackupnumber"
        fi
fi
/usr/bin/yunohost backup create --system conf_ldap conf_ynh_settings conf_ynh_certs data_mail data_xmpp conf_manually_modified_files --name "ynh_core_backup"
#Compress and move the backup
cp "$defaultfolder/$appbackup.info.json" "$targetfolder/$appbackup.info.json" -v
cp "$defaultfolder/ynh_core_backup.tar.$cmp" "$targetfolder/" -v || \
cp "$targetfolder/ynh_core_backup.tar.$cmp" "$defaultfolder/" -v
#Fix permissions
rsync -rtvz $defaultfolder/ $targetfolder
find $targetfolder/* -type f -name '*' -not -user nextcloud -exec chown -R nextcloud:nextcloud {} \;
find $targetfolder/* -type f -name '*' -not -perm 777 -exec chmod 777 {} \;
#Substitute your folder here as well
sudo -u nextcloud php8.2 --define apc.enable_cli=1 /var/www/nextcloud/occ files\:scan --all --path="/csolisr/files/Multimedia/archives"
2 Likes