[17] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
| 3 | # Backups game data files.
|
---|
| 4 |
|
---|
| 5 | sdtdCommandBackup() {
|
---|
[52] | 6 | local DT=`date "+%Y-%m-%d_%H-%M"`
|
---|
| 7 | local NewBackup=$SDTD_BACKUP_ROOT/$DT
|
---|
[17] | 8 |
|
---|
| 9 | # Check for backup folder existence
|
---|
| 10 | if [ -e $SDTD_BACKUP_ROOT ]; then
|
---|
| 11 | # Exists, copy(link) latest backup
|
---|
| 12 | unset -v LatestBackup
|
---|
[52] | 13 | local fileI
|
---|
| 14 | for fileI in "$SDTD_BACKUP_ROOT"/*; do
|
---|
[53] | 15 | if [ "$fileI" -nt "$LatestBackup" -a -d "$fileI" ]; then
|
---|
[52] | 16 | LatestBackup=$fileI
|
---|
| 17 | fi
|
---|
[17] | 18 | done
|
---|
[52] | 19 | if [ -d $LatestBackup ]; then
|
---|
| 20 | cp -al $LatestBackup $NewBackup
|
---|
| 21 | fi
|
---|
| 22 | fi
|
---|
| 23 |
|
---|
| 24 | if [ ! -d $SDTD_BACKUP_ROOT ]; then
|
---|
[17] | 25 | # Create new backup dir
|
---|
| 26 | mkdir $SDTD_BACKUP_ROOT
|
---|
| 27 | fi
|
---|
| 28 |
|
---|
| 29 | $RSYNC -a --delete --numeric-ids --delete-excluded $SDTD_BASE/instances/./ $NewBackup
|
---|
| 30 | touch $NewBackup
|
---|
[52] | 31 |
|
---|
| 32 | ## Compress if enabled
|
---|
| 33 | case ${SDTD_BACKUP_COMPRESS:-none} in
|
---|
| 34 | all)
|
---|
| 35 | local dfname=$(basename $NewBackup)
|
---|
| 36 | cd $SDTD_BACKUP_ROOT
|
---|
| 37 | tar -czf $dfname.tar.gz $dfname
|
---|
| 38 | touch -r $dfname $dfname.tar.gz
|
---|
| 39 | rm -Rf $dfname
|
---|
| 40 | ;;
|
---|
| 41 | old)
|
---|
| 42 | if [ -d $LatestBackup ]; then
|
---|
| 43 | local dfname=$(basename $LatestBackup)
|
---|
| 44 | cd $SDTD_BACKUP_ROOT
|
---|
| 45 | tar -czf $dfname.tar.gz $dfname
|
---|
| 46 | touch -r $dfname $dfname.tar.gz
|
---|
| 47 | rm -Rf $dfname
|
---|
| 48 | fi
|
---|
| 49 | ;;
|
---|
| 50 | none)
|
---|
| 51 | ;;
|
---|
| 52 | esac
|
---|
| 53 |
|
---|
| 54 | ## Purge old/too many backups
|
---|
| 55 | local keepMin=${SDTD_BACKUP_MIN_BACKUPS_KEEP:-0}
|
---|
| 56 | if [ $(isANumber $SDTD_BACKUP_MAX_BACKUPS) -eq 1 ]; then
|
---|
| 57 | local removeBut=$SDTD_BACKUP_MAX_BACKUPS
|
---|
| 58 | if [ $SDTD_BACKUP_MAX_BACKUPS -lt $keepMin ]; then
|
---|
| 59 | removeBut=$keepMin
|
---|
| 60 | fi
|
---|
| 61 | local num=0
|
---|
| 62 | local F
|
---|
| 63 | for F in $(ls -t1 $SDTD_BACKUP_ROOT); do
|
---|
| 64 | (( num++ ))
|
---|
| 65 | if [ $num -gt $removeBut ]; then
|
---|
[53] | 66 | rm -Rf $F
|
---|
[52] | 67 | fi
|
---|
| 68 | done
|
---|
| 69 | fi
|
---|
| 70 | if [ $(isANumber $SDTD_BACKUP_MAX_AGE) -eq 1 ]; then
|
---|
| 71 | local FINDBASE="find $SDTD_BACKUP_ROOT -mindepth 1 -maxdepth 1"
|
---|
| 72 | # Only continue if there are more than MIN_BACKUPS_KEEP backups at all
|
---|
| 73 | if [ $($FINDBASE | wc -l) -gt $keepMin ]; then
|
---|
| 74 | local minutes=$(( $SDTD_BACKUP_MAX_AGE*60 ))
|
---|
| 75 | while [ $($FINDBASE -mmin -$minutes | wc -l) -lt $keepMin ]; do
|
---|
| 76 | minutes=$(( minutes+60 ))
|
---|
| 77 | done
|
---|
| 78 | $FINDBASE -mmin +$minutes -exec rm -Rf {} \;
|
---|
| 79 | fi
|
---|
| 80 | fi
|
---|
| 81 | if [ $(isANumber $SDTD_BACKUP_MAX_STORAGE) -eq 1 ]; then
|
---|
| 82 | local maxKBytes=$(( $SDTD_BACKUP_MAX_STORAGE*1024 ))
|
---|
| 83 | local curNumFiles=$(ls -t1 $SDTD_BACKUP_ROOT | wc -l)
|
---|
[53] | 84 | while [ $(du -sk $SDTD_BACKUP_ROOT | tr '[:blank:]' ' ' | cut -d\ -f1) -gt $maxKBytes -a $curNumFiles -gt $keepMin ]; do
|
---|
[52] | 85 | local toDel=$(ls -tr1 | head -n 1)
|
---|
| 86 | rm -Rf $toDel
|
---|
| 87 | (( curNumFiles-- ))
|
---|
| 88 | done
|
---|
| 89 | fi
|
---|
[17] | 90 |
|
---|
[26] | 91 | for H in $(getHooksFor backup); do
|
---|
[53] | 92 | if [ "$SDTD_BACKUP_COMPRESS" = "all" ]; then
|
---|
| 93 | $H $NewBackup.tar.gz
|
---|
| 94 | else
|
---|
| 95 | $H $NewBackup
|
---|
| 96 | fi
|
---|
[26] | 97 | done
|
---|
[17] | 98 | }
|
---|
| 99 |
|
---|
| 100 | sdtdCommandBackupHelp() {
|
---|
| 101 | echo "Usage: $(basename $0) backup"
|
---|
| 102 | echo
|
---|
| 103 | echo "Backups all data files (instance configurations, save data, logs)."
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | sdtdCommandBackupDescription() {
|
---|
| 107 | echo "Backup game data files"
|
---|
| 108 | }
|
---|