Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ################################################################################################################
  4. # Simple backup script for Raspberry Pi 4 hosting Gitea.
  5. #
  6. # Backup file names:
  7. # File names contain the project name and the timestamp (year, month, week, day):
  8. # PROJECT-YYYY-MM-WW-DDatHHMMSS.zip
  9. # Example:
  10. # FocusGitea-2019-11-45-16-at120943.zip has been created on 2019-11-16 (week 45) at 12:09 and 43 seconds for
  11. # project "FocusGitea".
  12. #
  13. # Configuration:
  14. # Backups are created on a daily basis. An amount of those backups can be saved to the device at the same time.
  15. # "Yearlies" are backups which are created once per year. Each time the script detects that the year has changed
  16. # relative to the last backup, it will put the new backup into folder "yearlies".
  17. # "Monthlies", "weeklies" and "yearlies" work the same, except that they are saved in intervals of per month/
  18. # week/day.
  19. #
  20. #
  21. # author: Matthias Paul, FOCUS electronics (Q4/2019)
  22. ################################################################################################################
  23.  
  24.  
  25.  
  26. ################################################################################################################
  27. ######## CONFIGURATION #########################################################################################
  28. ################################################################################################################
  29. # amount of yearly backups to keep (set -1 to disable, set 0 for infinite amount of backups)
  30. N_YEARLIES=2
  31. # amount of monthly backups to keep
  32. N_MONTHLIES=3
  33. # amount of weekly backups to keep
  34. N_WEEKLIES=3
  35. # amount of daily backups to keep
  36. N_DAILIES=2
  37. # name of the project (used as filename, so only use valid characters here)
  38. PROJECTNAME=FocusGitea
  39. # main backup folder (incl. ending slash)
  40. BACKUPFOLDER=/home/paule/backup/
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. ################################################################################################################
  49. ######## CREATE BACKUP #########################################################################################
  50. ################################################################################################################
  51.  
  52.  
  53. # current year (integer)
  54. YEAR=$(date +%Y)
  55. # current month (integer)
  56. MONTH=$(date +%m)
  57. # current week (integer)
  58. WEEK=$(date +%U)
  59. # current day (integer)
  60. DAY=$(date +%d)
  61. # current timestamp (string)
  62. TIMESTAMP=$(date +%H%M%S)
  63.  
  64. # name (excl. folder) of backup file
  65. BACKUPFILE=$PROJECTNAME-$YEAR-$MONTH-$WEEK-$DAY"at"$TIMESTAMP.zip
  66. # length of project string
  67. PROJECTNAMELEN=${#PROJECTNAME}
  68. # length of backup folder sting
  69. BACKUPFOLDERLEN=${#BACKUPFOLDER}
  70.  
  71.  
  72. # TODO: remove (debug)
  73. touch $BACKUPFILE
  74.  
  75. ################################################################################################################
  76. ######## MOVE BACKUP ###########################################################################################
  77. ################################################################################################################
  78.  
  79. # defines the destination folder of the backup file
  80. DESTFOLDER=
  81. if [ $N_YEARLIES -gt -1 ]
  82. then
  83. # yearlies are enabled, ensure to put it into that folder
  84. FOLDER=$BACKUPFOLDER"yearlies/"
  85. # create folder if not exists
  86. mkdir -p $FOLDER
  87.  
  88. # check if file should be added here, because year changed
  89. # current backup and latest backup in folder;
  90. # 1st step: retrieve most up-to-date file by filename
  91. # ATTENTION: Do not use file properties, becaus there's np
  92. # file creation property in Linux.
  93. TOPFILE=$(ls -r $FOLDER$PROJECTNAME*.zip 2>/dev/null | head -n 1)
  94. echo $FOLDER
  95. echo $TOPFILE
  96. if [ -z $TOPFILE ]
  97. then
  98. # no previous backups available, put new backup here
  99. DESTFOLDER=$FOLDER
  100. mv $BACKUPFILE $DESTFOLDER$BACKUPFILE
  101. echo "Initial backup "$BACKUPFILE" copied to "$DESTFOLDER
  102. else
  103. # there's at least one yearly backup, check if date changed;
  104. # get substring of length 4 starting 1 character after project name (incl. folder)
  105. STARTSTR=$((PROJECTNAMELEN+BACKUPFOLDERLEN+10))
  106. YEARSTR="${TOPFILE:STARTSTR:4}"
  107. if [ $YEARSTR -lt $YEAR ]
  108. then
  109. # backup needs to placed here, because it is newer
  110. DESTFOLDER=$FOLDER
  111. mv $BACKUPFILE $FOLDER$BACKUPFILE
  112. echo "Added backup "$BACKUPFILE" to "$FOLDER
  113. if [ $N_YEARLIES -gt 0 ]
  114. then
  115.  
  116. # if a maximum amount of backups had been defined,
  117. # delete previous backups after having copied the file;
  118. # display all files matching the project name in reverse order
  119. # (starting at most up-to-date one), then start at n-th entry
  120. ls -rt $FOLDER$PROJECTNAME*.zip | tail -n +2
  121. fi
  122. fi
  123.  
  124. fi
  125.  
  126.  
  127. # move backup file into destination folder
  128.  
  129. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement