[17] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
[258] | 3 | # Copyright 2016 Christian 'Alloc' Illy
|
---|
| 4 | #
|
---|
| 5 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 6 | # you may not use this file except in compliance with the License.
|
---|
| 7 | # You may obtain a copy of the License at
|
---|
| 8 | #
|
---|
| 9 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 10 | #
|
---|
| 11 | # Unless required by applicable law or agreed to in writing, software
|
---|
| 12 | # distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 14 | # See the License for the specific language governing permissions and
|
---|
| 15 | # limitations under the License.
|
---|
| 16 |
|
---|
| 17 |
|
---|
[17] | 18 | # Backups game data files.
|
---|
| 19 |
|
---|
| 20 | sdtdCommandBackup() {
|
---|
[52] | 21 | local DT=`date "+%Y-%m-%d_%H-%M"`
|
---|
| 22 | local NewBackup=$SDTD_BACKUP_ROOT/$DT
|
---|
[54] | 23 |
|
---|
| 24 | if [ ! -d "$SDTD_BASE/instances" ]; then
|
---|
| 25 | return
|
---|
| 26 | fi
|
---|
[17] | 27 |
|
---|
[67] | 28 | if [ "$SDTD_BACKUP_SAVEWORLD" == "true" ]; then
|
---|
| 29 | for I in $(getInstanceList); do
|
---|
| 30 | if [ $(isRunning $I) -eq 1 ]; then
|
---|
| 31 | telnetCommand $I saveworld 2
|
---|
| 32 | fi
|
---|
| 33 | done
|
---|
| 34 | fi
|
---|
| 35 |
|
---|
[17] | 36 | # Check for backup folder existence
|
---|
| 37 | if [ -e $SDTD_BACKUP_ROOT ]; then
|
---|
| 38 | # Exists, copy(link) latest backup
|
---|
| 39 | unset -v LatestBackup
|
---|
[52] | 40 | local fileI
|
---|
[56] | 41 | for fileI in $(find "$SDTD_BACKUP_ROOT" -mindepth 1 -maxdepth 1 -type d); do
|
---|
[54] | 42 | if [ "$fileI" -nt "$LatestBackup" ]; then
|
---|
[52] | 43 | LatestBackup=$fileI
|
---|
| 44 | fi
|
---|
[17] | 45 | done
|
---|
[54] | 46 | if [ -d "$LatestBackup" ]; then
|
---|
| 47 | cp -al "$LatestBackup" "$NewBackup"
|
---|
[52] | 48 | fi
|
---|
| 49 | fi
|
---|
| 50 |
|
---|
| 51 | if [ ! -d $SDTD_BACKUP_ROOT ]; then
|
---|
[17] | 52 | # Create new backup dir
|
---|
| 53 | mkdir $SDTD_BACKUP_ROOT
|
---|
| 54 | fi
|
---|
| 55 |
|
---|
| 56 | $RSYNC -a --delete --numeric-ids --delete-excluded $SDTD_BASE/instances/./ $NewBackup
|
---|
| 57 | touch $NewBackup
|
---|
[52] | 58 |
|
---|
| 59 | ## Compress if enabled
|
---|
| 60 | case ${SDTD_BACKUP_COMPRESS:-none} in
|
---|
| 61 | all)
|
---|
| 62 | local dfname=$(basename $NewBackup)
|
---|
| 63 | cd $SDTD_BACKUP_ROOT
|
---|
| 64 | tar -czf $dfname.tar.gz $dfname
|
---|
| 65 | touch -r $dfname $dfname.tar.gz
|
---|
| 66 | rm -Rf $dfname
|
---|
| 67 | ;;
|
---|
| 68 | old)
|
---|
| 69 | if [ -d $LatestBackup ]; then
|
---|
| 70 | local dfname=$(basename $LatestBackup)
|
---|
| 71 | cd $SDTD_BACKUP_ROOT
|
---|
| 72 | tar -czf $dfname.tar.gz $dfname
|
---|
| 73 | touch -r $dfname $dfname.tar.gz
|
---|
| 74 | rm -Rf $dfname
|
---|
| 75 | fi
|
---|
| 76 | ;;
|
---|
| 77 | none)
|
---|
| 78 | ;;
|
---|
| 79 | esac
|
---|
| 80 |
|
---|
[61] | 81 | cd $SDTD_BACKUP_ROOT
|
---|
| 82 |
|
---|
[52] | 83 | ## Purge old/too many backups
|
---|
| 84 | local keepMin=${SDTD_BACKUP_MIN_BACKUPS_KEEP:-0}
|
---|
| 85 | if [ $(isANumber $SDTD_BACKUP_MAX_BACKUPS) -eq 1 ]; then
|
---|
| 86 | local removeBut=$SDTD_BACKUP_MAX_BACKUPS
|
---|
| 87 | if [ $SDTD_BACKUP_MAX_BACKUPS -lt $keepMin ]; then
|
---|
| 88 | removeBut=$keepMin
|
---|
| 89 | fi
|
---|
| 90 | local num=0
|
---|
| 91 | local F
|
---|
| 92 | for F in $(ls -t1 $SDTD_BACKUP_ROOT); do
|
---|
| 93 | (( num++ ))
|
---|
| 94 | if [ $num -gt $removeBut ]; then
|
---|
[53] | 95 | rm -Rf $F
|
---|
[52] | 96 | fi
|
---|
| 97 | done
|
---|
| 98 | fi
|
---|
| 99 | if [ $(isANumber $SDTD_BACKUP_MAX_AGE) -eq 1 ]; then
|
---|
| 100 | local FINDBASE="find $SDTD_BACKUP_ROOT -mindepth 1 -maxdepth 1"
|
---|
| 101 | # Only continue if there are more than MIN_BACKUPS_KEEP backups at all
|
---|
| 102 | if [ $($FINDBASE | wc -l) -gt $keepMin ]; then
|
---|
| 103 | local minutes=$(( $SDTD_BACKUP_MAX_AGE*60 ))
|
---|
| 104 | while [ $($FINDBASE -mmin -$minutes | wc -l) -lt $keepMin ]; do
|
---|
| 105 | minutes=$(( minutes+60 ))
|
---|
| 106 | done
|
---|
| 107 | $FINDBASE -mmin +$minutes -exec rm -Rf {} \;
|
---|
| 108 | fi
|
---|
| 109 | fi
|
---|
| 110 | if [ $(isANumber $SDTD_BACKUP_MAX_STORAGE) -eq 1 ]; then
|
---|
| 111 | local maxKBytes=$(( $SDTD_BACKUP_MAX_STORAGE*1024 ))
|
---|
| 112 | local curNumFiles=$(ls -t1 $SDTD_BACKUP_ROOT | wc -l)
|
---|
[53] | 113 | while [ $(du -sk $SDTD_BACKUP_ROOT | tr '[:blank:]' ' ' | cut -d\ -f1) -gt $maxKBytes -a $curNumFiles -gt $keepMin ]; do
|
---|
[52] | 114 | local toDel=$(ls -tr1 | head -n 1)
|
---|
| 115 | rm -Rf $toDel
|
---|
| 116 | (( curNumFiles-- ))
|
---|
| 117 | done
|
---|
| 118 | fi
|
---|
[17] | 119 |
|
---|
[26] | 120 | for H in $(getHooksFor backup); do
|
---|
[53] | 121 | if [ "$SDTD_BACKUP_COMPRESS" = "all" ]; then
|
---|
| 122 | $H $NewBackup.tar.gz
|
---|
| 123 | else
|
---|
| 124 | $H $NewBackup
|
---|
| 125 | fi
|
---|
[26] | 126 | done
|
---|
[17] | 127 | }
|
---|
| 128 |
|
---|
| 129 | sdtdCommandBackupHelp() {
|
---|
| 130 | echo "Usage: $(basename $0) backup"
|
---|
| 131 | echo
|
---|
| 132 | echo "Backups all data files (instance configurations, save data, logs)."
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | sdtdCommandBackupDescription() {
|
---|
| 136 | echo "Backup game data files"
|
---|
| 137 | }
|
---|