Skip to content

Instantly share code, notes, and snippets.

@vrdhn
Last active October 17, 2021 09:29
Show Gist options
  • Save vrdhn/c4fbe7b6febe64da9ea1821b8cfd9ff1 to your computer and use it in GitHub Desktop.
Save vrdhn/c4fbe7b6febe64da9ea1821b8cfd9ff1 to your computer and use it in GitHub Desktop.
simple script to run gitea, and do backups every time.
#!/bin/bash
## run-gitea.sh (C) Vardhan Varma <[email protected]>
## SPDX-License-Identifier: AGPL-3.0-or-later
##
## Features:
## (1) run gitea server for LAN usage
## (2) make encrypted archive on stopping.
## (3) backup archive to usb with label USBBACKUP
##
## Limitations:
## * avoid whitespace in name of anything.
##
## Configuration:
## * in same directory as this script, put run-gitea-override.sh
## * set CUSTOM and PASSWORD in that file.
##
## Requires:
## (1) gitea
## (2) 7z
##
## TODO:
## (1) restore script.
ROOT="$(dirname $(readlink -f "$0"))"
CUSTOM=custom
PASSWORD="override-in-run-gitea-override.sh"
. $ROOT/run-gitea-override.sh
DIR=$ROOT/$CUSTOM-gitea-root
WORK_PATH=$DIR
CUSTOM_PATH=$DIR/$CUSTOM
APPINI_PATH=$CUSTOM_PATH/app.ini
PID_PATH=$ROOT/$CUSTOM-gitea.pid
GITEA_OPT="--work-path $WORK_PATH --custom-path $CUSTOM_PATH --config $APPINI_PATH --pid $PID_PATH"
echo "**** $CUSTOM *****"
echo "**** $PASSWORD ****"
start ()
{
( mkdir -p $WORK_PATH && cd $WORK_PATH && gitea $GITEA_OPT ) &
echo "Gitea started in background, visit http://localhost:3000"
}
run ()
{
mkdir -p $WORK_PATH && cd $WORK_PATH && gitea $GITEA_OPT "$@"
}
stop ()
{
echo "Stopping gitea ..."
( cd $WORK_PATH && gitea $GITEA_OPT manager shutdown )
( rm $PID_PATH
sleep 2
killall gitea
killall -9 gitea
) > /dev/null 2>&1
echo "Taking backup..."
( cd $ROOT
test -f $CUSTOM-gitea-backup-9.7z && rm $CUSTOM-gitea-backup-9.7z
test -f $CUSTOM-gitea-backup-8.7z && mv $CUSTOM-gitea-backup-8.7z $CUSTOM-gitea-backup-9.7z
test -f $CUSTOM-gitea-backup-7.7z && mv $CUSTOM-gitea-backup-7.7z $CUSTOM-gitea-backup-8.7z
test -f $CUSTOM-gitea-backup-6.7z && mv $CUSTOM-gitea-backup-6.7z $CUSTOM-gitea-backup-7.7z
test -f $CUSTOM-gitea-backup-5.7z && mv $CUSTOM-gitea-backup-5.7z $CUSTOM-gitea-backup-6.7z
test -f $CUSTOM-gitea-backup-4.7z && mv $CUSTOM-gitea-backup-4.7z $CUSTOM-gitea-backup-5.7z
test -f $CUSTOM-gitea-backup-3.7z && mv $CUSTOM-gitea-backup-3.7z $CUSTOM-gitea-backup-4.7z
test -f $CUSTOM-gitea-backup-2.7z && mv $CUSTOM-gitea-backup-2.7z $CUSTOM-gitea-backup-3.7z
test -f $CUSTOM-gitea-backup-1.7z && mv $CUSTOM-gitea-backup-1.7z $CUSTOM-gitea-backup-2.7z
test -f $CUSTOM-gitea-backup.7z && mv $CUSTOM-gitea-backup.7z $CUSTOM-gitea-backup-1.7z
)
cp $0 $CUSTOM_PATH
(cd $WORK_PATH && gitea $GITEA_OPT dump -V -L -f "$ROOT/$CUSTOM-gitea-backup.zip" )
zipto7z "$ROOT/$CUSTOM-gitea-backup.zip" "$ROOT/$CUSTOM-gitea-backup.7z"
ls -altr $ROOT/*.7z
}
running ()
{
if ! [ -f $PID_PATH ] ; then
return 1;
fi
if ps -p $(cat $PID_PATH) > /dev/null ; then
return 0;
else
return 1
fi
}
## May cause issue with automatic mounting deamons.
usbbackup()
{
if [ -e /dev/disk/by-label/BACKUPUSB ]
then
echo "BACKING UP TO USB"
mkdir /tmp/$$
sudo mount /dev/disk/by-label/BACKUPUSB /tmp/$$
sudo mkdir -p /tmp/$$/BACKUPS/GITEA-BACKUPS/
sudo cp "$ROOT/$CUSTOM-gitea-backup.7z" /tmp/$$/BACKUPS/GITEA-BACKUPS/
sudo ls -l /tmp/$$/BACKUPS/GITEA-BACKUPS/
sudo umount /tmp/$$
sudo eject /dev/disk/by-label/BACKUPUSB
echo "USE BACKUP DONE"
else
echo "BACKUP USB NOT FOUND"
fi
}
zipto7z ()
{
rm -fr /tmp/zipto7z/$$/
mkdir -p /tmp/zipto7z/$$/
7z x "$1" -o/tmp/zipto7z/$$/
cd /tmp/zipto7z/$$/
7z a -p$PASSWORD "$2"
rm -fr /tmp/zipto7z/$$/
rm "$1"
}
if [ "x$1" == "x" ] ; then
if running ; then
stop
usbbackup
else
start
fi
else
run "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment