In the previous article, I wrote about how to automate CyberPanel git push without it’s default Git Manager feature, today I like to share a way to automatic backup CyberPanel websites to S3-compatible Storage without CyberPanel Cloud.
Pre-requisites
Before starting, there are several prerequisites that must be met to be able to use this method: We need an S3 client. There are many options you can use, such as official AWS S3 Client or Minio CLI. On this article, I’ll use the Minio CLI as my S3 client.
Install and configure S3 Client (Minio CLI)
On Linux distributions such as Arch Linux, the Minio client can be installed using it’s package manager by running pacman -S minio-client
and the minio-client binary will be saved as mcli
.
In other distributions such as Ubuntu, minio-client can be installed by downloading its binary program. Follow the official documentation on Minio CLI page.
Install and configutration example on Ubuntu
1curl https://dl.min.io/client/mc/release/linux-amd64/mc \
2 --create-dirs \
3 -o $HOME/minio-binaries/mc
4chmod +x $HOME/minio-binaries/mc
5export PATH=$PATH:$HOME/minio-binaries/
Then add export PATH=$PATH:$HOME/minio-binaries/
to your system $PATH
variable on your shell you use (Ie: ~/bashrc
if you use bash or ~/.zshrc
if you use zsh).
Create alias for S3-Compatible service on Minio CLI
Execute this command to create an alias on Minio CLI:
1mc alias set ALIAS HOSTNAME ACCESS_KEY SECRET_KEY
- Replace
ALIAS
with the name related to your S3 service. - Replace
HOSTNAME
with your S3 endpoint URL . - Replace
ACCESS_KEY
and SECRET_KEY with your S3 access and secret key.
Example:
1mc alias set backup https://s3.amazonaws.com SomERanDomAcceSsKey SomERanDomSeCreTKey
bash script for CyberPanel backup
After S3 alias is configured, create bash
script to do a backup job for CyberPanel website to S3.
1#!/bin/bash
2#title : backup_cyberpanel_to_s3.sh
3#description : Simple script to backup CyberPanel websites to S3 Storage.
4#author : Christian Ditaputratama <[email protected]>
5#date : 2023-02-05
6#last update : 2023-02-05
7#version : 0.0.1
8#usage : bash backup_cyberpanel_to_s3.sh
9#notes : This script need S3 client (minio-cli) installed and
10# configured.
11# Please read https://rtd.ditatompel.com/automatic-backup-cyberpanel-websites-to-s3-storage
12# for more information.
13#==============================================================================
14
15set -e
16
17MINIO_REMOTE_ALIAS="backup" # your mc `alias` name
18MINIO_BUCKET="your-bucket"
19MINIO_FOLDER="path/to/remote/folder/" # Mandatory, don't forget the trailing slash at the end
20BACKUP_RETENTION_DAY=7
21
22##### End basic config #####
23# stop editing here
24div============================================================================
25
26PID_FILE=/tmp/cyberpanel_backup_running.pid
27
28# prevent multiple backup running at the same time
29if [ -f "$PID_FILE" ]; then
30 echo "Process is running! Exiting..."
31 exit 0
32fi
33touch $PID_FILE
34
35LIST_WEBSITES=$(cyberpanel listWebsitesJson | jq -r '. | fromjson')
36
37for WEBSITE in $(echo "${LIST_WEBSITES}" | jq -r '.[].domain'); do
38 echo "Backing up ${WEBSITE}"
39 cyberpanel createBackup --domainName ${WEBSITE}
40
41 echo "Uploading to S3..."
42 mc mirror /home/${WEBSITE}/backup/ $MINIO_REMOTE_ALIAS/$MINIO_BUCKET/$MINIO_FOLDER${WEBSITE}/ --overwrite
43
44 echo "Remove old backup..."
45 find /home/${WEBSITE}/backup -type f -name "backup-${WEBSITE}-*.tar.gz" -delete
46
47 mc rm $MINIO_REMOTE_ALIAS/$MINIO_BUCKET/$MINIO_FOLDER${WEBSITE}/ --recursive --dangerous --force --older-than ${BACKUP_RETENTION_DAY}d
48done
49rm $PID_FILE
Change script file permission so it can be executed with chmod +x path/to/backup_cyberpanel_to_s3.sh
command.
Adjust variable values to suit with your environment :
MINIO_REMOTE_ALIAS
: alias name that we have previously configured.MINIO_BUCKET
: bucket name you useMINIO_FOLDER
: The folder location on S3 storage where we save the folder. Don’t forget to put / at the end of the folder.BACKUP_RETENTION_DAY
: How long (in days) the backup on remote storage (S3) is kept.
Then, create a cron job, adjust as needed:
10 * * * * /bin/bash /path/to/backup_cyberpanel_to_s3.sh >/dev/null 2>&1