bash/backup.sh
2022-12-19 20:18:51 +11:00

50 lines
1.7 KiB
Bash

#!/bin/bash
# Make a new folder at /opt/backup: mkdir -p /opt/backup && touch /opt/backup/backup.log && cd /opt/backup
# Save this file at /opt/backup/backup.sh
# Set a cronjob: 0 3 * * * /bin/bash /opt/backup/backup.sh > /opt/backup/backup.log 2>&1
# Get the relative path of the backup script
backup_script=$(dirname "$realpath $0")
# Loading the Config
rclone_config_name=""
rclone_config_pass=""
s3_bucket_name=""
backup_folder_name="$(date +"%Y_%m_%d_%I_%M_%p")"
# Website and Database
website_folder=""
website_folder_exclude=""
database_name=""
# Remove old db dump
cd /opt/backup
rm /opt/backup/dbbackup-*
rm /opt/backup/website-*
# Generating a database dump backup
# if $database_name exists, then dump the database, else skip
if [ -n "$database_name" ]; then
mysqldump -u root $database_name > dbbackup-$backup_folder_name.sql
tar -zcvf dbbackup-$backup_folder_name.tar.gz dbbackup-$backup_folder_name.sql
fi
# if $website_folder_exclude exists, then tar the website folder excluding the $website_folder_exclude
if [ -n "$website_folder_exclude" ]; then
tar --exclude $website_folder_exclude -zcvf website-$backup_folder_name.tar.gz $website_folder
else
tar -zcvf website-$backup_folder_name.tar.gz $website_folder
fi
# if $rclone_config_pass exists, then set the rclone config password, else skip
if [ -n "$rclone_config_pass" ]; then
export RCLONE_CONFIG_PASS=$rclone_config_pass
fi
# Copy the database and website backup
if [ -n "$database_name" ]; then
rclone copy /opt/backup/dbbackup-$backup_folder_name.tar.gz $rclone_config_name:$s3_bucket_name/$backup_folder_name
fi
rclone copy /opt/backup/website-$backup_folder_name.tar.gz $rclone_config_name:$s3_bucket_name/$backup_folder_name