Add backups for gitea
This commit is contained in:
@ -3,6 +3,12 @@
|
||||
set -eu
|
||||
set -o pipefail
|
||||
|
||||
echo "Backup: perform gitea backup"
|
||||
|
||||
su --login gitea -c '/home/gitea/gitea-dump.sh'
|
||||
mkdir -p {{ backup_directory }}/gitea
|
||||
mv /home/gitea/backups/* {{ backup_directory }}/gitea
|
||||
|
||||
echo "Backup: perform backup with gobackup"
|
||||
|
||||
gobackup perform --config={{ backup_gobackup_config }}
|
||||
@ -21,4 +27,8 @@ curl -s -X POST 'https://api.telegram.org/bot{{ notifications_tg_bot_token }}/se
|
||||
-d 'parse_mode=HTML' \
|
||||
-d 'text=<b>{{ notifications_name }}</b>: бекап успешно завершен!'
|
||||
|
||||
echo -e "\nRemove old files"
|
||||
|
||||
keep-files.py {{ backup_directory }}/gitea --keep 2
|
||||
|
||||
echo -e "\nBackup: done"
|
||||
|
@ -1,6 +1,6 @@
|
||||
services:
|
||||
|
||||
server:
|
||||
gitea_web_app:
|
||||
image: gitea/gitea:1.22.6
|
||||
restart: unless-stopped
|
||||
container_name: gitea_web_app
|
||||
@ -10,6 +10,7 @@ services:
|
||||
- "GITEA__server__SSH_PORT=2222"
|
||||
volumes:
|
||||
- ./data:/data
|
||||
- ./backups:/backups
|
||||
- /etc/timezone:/etc/timezone:ro
|
||||
- /etc/localtime:/etc/localtime:ro
|
||||
ports:
|
||||
|
13
files/gitea/gitea-dump.sh.j2
Normal file
13
files/gitea/gitea-dump.sh.j2
Normal file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -eu
|
||||
set -o pipefail
|
||||
|
||||
echo "Gitea: backup data with gitea dump"
|
||||
|
||||
(cd {{ base_dir }} && docker compose exec -u "{{ user_create_result.uid }}:{{ user_create_result.group }}" -w /backups gitea_web_app gitea dump -c /data/gitea/conf/app.ini)
|
||||
|
||||
|
||||
echo "Gitea: remove old backups"
|
||||
|
||||
keep-files.py {{ backups_dir }} --keep 2
|
45
files/keep-files.py
Normal file
45
files/keep-files.py
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import argparse
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Retain specified number of files in a directory sorted by name, delete others.')
|
||||
parser.add_argument('directory', type=str, help='Path to target directory')
|
||||
parser.add_argument('--keep', type=int, default=2,
|
||||
help='Number of files to retain (default: 2)')
|
||||
args = parser.parse_args()
|
||||
|
||||
# Validate arguments
|
||||
if args.keep < 0:
|
||||
parser.error("--keep value cannot be negative")
|
||||
|
||||
if not os.path.isdir(args.directory):
|
||||
parser.error(f"Directory not found: {args.directory}")
|
||||
|
||||
# Get list of files (exclude subdirectories)
|
||||
files = []
|
||||
with os.scandir(args.directory) as entries:
|
||||
for entry in entries:
|
||||
if entry.is_file():
|
||||
files.append(entry.name)
|
||||
|
||||
# Sort files alphabetically
|
||||
sorted_files = sorted(files)
|
||||
|
||||
# Identify files to delete
|
||||
to_delete = sorted_files[:-args.keep] if args.keep > 0 else sorted_files.copy()
|
||||
|
||||
# Delete files and print results
|
||||
for filename in to_delete:
|
||||
filepath = os.path.join(args.directory, filename)
|
||||
try:
|
||||
os.remove(filepath)
|
||||
print(f"Deleted: {filename}")
|
||||
except Exception as e:
|
||||
print(f"Error deleting {filename}: {str(e)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user