System-Backup/system-backup

68 lines
2.2 KiB
Plaintext
Raw Normal View History

2021-01-06 21:08:40 +01:00
#!/bin/bash
2021-01-07 00:09:29 +01:00
# Show help page
2021-01-06 21:08:40 +01:00
if [ "$1" = "-h" ]
then
2021-01-07 00:09:29 +01:00
echo "system-backup <option>"
2021-01-06 21:08:40 +01:00
echo
echo "If no options are specified it will use default"
echo
echo "Options:"
echo
2021-01-07 23:37:25 +01:00
echo "-d [USB Drive]"
2021-01-07 00:09:29 +01:00
echo
2021-01-06 21:08:40 +01:00
echo
exit 0
fi
2021-01-07 00:09:29 +01:00
# Initialize variables
2021-01-07 23:37:25 +01:00
backup_dir="/mnt/USB/Backup/"$(date +'%Y/%m/%d')
2021-01-07 00:09:29 +01:00
backup_dir_remote="/media/backup/"$(date +'%Y/%m/%d')
nextcloud_dir="/var/www/nextcloud"
2021-01-22 12:53:30 +01:00
remote_ip="192.168.178.8"
2021-01-07 00:09:29 +01:00
# Shuting down services for backup
cd $nextcloud_dir
sudo -u www-data php occ maintenance:mode --on
2021-01-07 19:28:08 +01:00
sudo service apache2 stop
sudo mysqldump --single-transaction nextcloud > /tmp/nextcloud.sql
2021-01-07 00:52:31 +01:00
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"
2021-01-07 23:37:25 +01:00
# Back the fuck up bro...
2021-01-22 13:23:31 +01:00
sudo screen -dmS backup -t backup bash -c "
2021-01-07 23:37:25 +01:00
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"
2021-01-22 13:23:31 +01:00
rsync -Aarz --progress -e ssh $nextcloud_dir/ $backup_dir/nextcloud;
2021-01-07 23:37:25 +01:00
rsync -Aar --progress -e ssh /tmp/nextcloud.sql $backup_dir/mysql/;
2021-01-22 13:23:31 +01:00
rsync -Aarz --progress -e ssh /tmp/gitea-dump/backup.zip $backup_dir/gitea/
2021-01-07 23:37:25 +01:00
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
2021-01-22 13:23:31 +01:00
rsync -WAXart -4 --progress -e ssh /tmp/nextcloud.sql backup-user@$remote_ip:$backup_dir_remote/mysql/;
2021-01-22 12:53:30 +01:00
rsync -WAXarzt -4 --progress -e ssh /tmp/gitea-dump/backup.zip backup-user@$remote_ip:$backup_dir_remote/gitea/
2021-01-22 13:23:31 +01:00
rsync -XWAarzt -4 --progress -e ssh $nextcloud_dir/ backup-user@$remote_ip:$backup_dir_remote/nextcloud;
2021-01-07 23:37:25 +01:00
fi
2021-01-07 00:09:29 +01:00
2021-01-07 14:03:34 +01:00
# 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
2021-01-22 13:23:31 +01:00
done"
sudo screen -R backup