Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ################################################################################################################
- # Simple backup script for Raspberry Pi 4 hosting Gitea.
- #
- # Backup file names:
- # File names contain the project name and the timestamp (year, month, week, day):
- # PROJECT-YYYY-MM-WW-DDatHHMMSS.zip
- # Example:
- # FocusGitea-2019-11-45-16-at120943.zip has been created on 2019-11-16 (week 45) at 12:09 and 43 seconds for
- # project "FocusGitea".
- #
- # Configuration:
- # Backups are created on a daily basis. An amount of those backups can be saved to the device at the same time.
- # "Yearlies" are backups which are created once per year. Each time the script detects that the year has changed
- # relative to the last backup, it will put the new backup into folder "yearlies".
- # "Monthlies", "weeklies" and "yearlies" work the same, except that they are saved in intervals of per month/
- # week/day.
- #
- #
- # author: Matthias Paul, FOCUS electronics (Q4/2019)
- ################################################################################################################
- ################################################################################################################
- ######## CONFIGURATION #########################################################################################
- ################################################################################################################
- # amount of yearly backups to keep (set -1 to disable, set 0 for infinite amount of backups)
- N_YEARLIES=2
- # amount of monthly backups to keep
- N_MONTHLIES=3
- # amount of weekly backups to keep
- N_WEEKLIES=3
- # amount of daily backups to keep
- N_DAILIES=2
- # name of the project (used as filename, so only use valid characters here)
- PROJECTNAME=FocusGitea
- # main backup folder (incl. ending slash)
- BACKUPFOLDER=/home/paule/backup/
- ################################################################################################################
- ######## CREATE BACKUP #########################################################################################
- ################################################################################################################
- # current year (integer)
- YEAR=$(date +%Y)
- # current month (integer)
- MONTH=$(date +%m)
- # current week (integer)
- WEEK=$(date +%U)
- # current day (integer)
- DAY=$(date +%d)
- # current timestamp (string)
- TIMESTAMP=$(date +%H%M%S)
- # name (excl. folder) of backup file
- BACKUPFILE=$PROJECTNAME-$YEAR-$MONTH-$WEEK-$DAY"at"$TIMESTAMP.zip
- # length of project string
- PROJECTNAMELEN=${#PROJECTNAME}
- # length of backup folder sting
- BACKUPFOLDERLEN=${#BACKUPFOLDER}
- # TODO: remove (debug)
- touch $BACKUPFILE
- ################################################################################################################
- ######## MOVE BACKUP ###########################################################################################
- ################################################################################################################
- # defines the destination folder of the backup file
- DESTFOLDER=
- if [ $N_YEARLIES -gt -1 ]
- then
- # yearlies are enabled, ensure to put it into that folder
- FOLDER=$BACKUPFOLDER"yearlies/"
- # create folder if not exists
- mkdir -p $FOLDER
- # check if file should be added here, because year changed
- # current backup and latest backup in folder;
- # 1st step: retrieve most up-to-date file by filename
- # ATTENTION: Do not use file properties, becaus there's np
- # file creation property in Linux.
- TOPFILE=$(ls -r $FOLDER$PROJECTNAME*.zip 2>/dev/null | head -n 1)
- echo $FOLDER
- echo $TOPFILE
- if [ -z $TOPFILE ]
- then
- # no previous backups available, put new backup here
- DESTFOLDER=$FOLDER
- mv $BACKUPFILE $DESTFOLDER$BACKUPFILE
- echo "Initial backup "$BACKUPFILE" copied to "$DESTFOLDER
- else
- # there's at least one yearly backup, check if date changed;
- # get substring of length 4 starting 1 character after project name (incl. folder)
- STARTSTR=$((PROJECTNAMELEN+BACKUPFOLDERLEN+10))
- YEARSTR="${TOPFILE:STARTSTR:4}"
- if [ $YEARSTR -lt $YEAR ]
- then
- # backup needs to placed here, because it is newer
- DESTFOLDER=$FOLDER
- mv $BACKUPFILE $FOLDER$BACKUPFILE
- echo "Added backup "$BACKUPFILE" to "$FOLDER
- if [ $N_YEARLIES -gt 0 ]
- then
- # if a maximum amount of backups had been defined,
- # delete previous backups after having copied the file;
- # display all files matching the project name in reverse order
- # (starting at most up-to-date one), then start at n-th entry
- ls -rt $FOLDER$PROJECTNAME*.zip | tail -n +2
- fi
- fi
- fi
- # move backup file into destination folder
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement