System-Backup/system-backup

66 lines
2.2 KiB
Bash

#!/bin/bash
# Show help page
if [ "$1" = "-h" ]
then
echo "system-backup <option>"
echo
echo "If no options are specified it will use default"
echo
echo "Options:"
echo
echo "-d [USB Drive]"
echo
echo
exit 0
fi
# Initialize variables
backup_dir="/mnt/USB/Backup/"$(date +'%Y/%m/%d')
backup_dir_remote="/media/backup/"$(date +'%Y/%m/%d')
nextcloud_dir="/var/www/nextcloud"
remote_ip="192.168.178.8"
# Shuting down services for backup
cd $nextcloud_dir
sudo -u www-data php occ maintenance:mode --on
sudo service apache2 stop
sudo mysqldump --single-transaction nextcloud > /tmp/nextcloud.sql
sudo mkdir /tmp/gitea-dump
sudo chown -R git:git /tmp/gitea-dump/
sudo -u git /usr/local/bin/gitea dump --config /etc/gitea/app.ini --file "/tmp/gitea-dump/backup.zip" --tempdir "/tmp/gitea-dump"
# Back the fuck up bro...
if [ "$1" = "-d" ]
then
sudo mkdir /mnt/USB/
sudo mount $2 /mnt/USB/
sudo mkdir -p $backup_dir"/nextcloud"
sudo mkdir $backup_dir"/mysql"
sudo mkdir $backup_dir"/gitea"
sudo screen -dmS backup -t backup bash -c "rsync -Aarz --progress -e ssh $nextcloud_dir/ $backup_dir/nextcloud;
rsync -Aar --progress -e ssh /tmp/nextcloud.sql $backup_dir/mysql/;
rsync -Aarz --progress -e ssh /tmp/gitea-dump/backup.zip $backup_dir/gitea/"
else
# Create remote directory
sudo ssh backup-user@$remote_ip mkdir -p $backup_dir_remote/gitea mkdir $backup_dir_remote/nextcloud mkdir $backup_dir_remote/mysql
# copy the shit over
sudo screen -dmS backup -t backup bash -c "rsync -WAXart -4 --progress -e ssh /tmp/nextcloud.sql backup-user@$remote_ip:$backup_dir_remote/mysql/;
rsync -WAXarzt -4 --progress -e ssh /tmp/gitea-dump/backup.zip backup-user@$remote_ip:$backup_dir_remote/gitea/
rsync -XWAarzt -4 --progress -e ssh $nextcloud_dir/ backup-user@$remote_ip:$backup_dir_remote/;"
fi
# Start back up
sudo service apache2 start
cd $nextcloud_dir
# Prompt user if maintenance mode should stay enabled
while true; do
read -p "Do you wish to leave maintenace mode enabled? [y/n] " yn
case $yn in
[Yy]* ) exit;;
[Nn]* ) sudo -u www-data php occ maintenance:mode --off; break;;
* ) echo "Please answer yes or no.";;
esac
done