source: scripts/usr/local/lib/7dtd/commands/backup.sh@ 500

Last change on this file since 500 was 353, checked in by alloc, 6 years ago

Scripts 112

File size: 3.8 KB
RevLine 
[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
20sdtdCommandBackup() {
[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
[353]31 telnetCommand $I saveworld 2 > /dev/null
[67]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
[308]50
[52]51 if [ ! -d $SDTD_BACKUP_ROOT ]; then
[17]52 # Create new backup dir
53 mkdir $SDTD_BACKUP_ROOT
54 fi
55
[308]56 for H in $(getHooksFor serverPreBackup); do
57 $H
58 done
59
[17]60 $RSYNC -a --delete --numeric-ids --delete-excluded $SDTD_BASE/instances/./ $NewBackup
61 touch $NewBackup
[52]62
63 ## Compress if enabled
64 case ${SDTD_BACKUP_COMPRESS:-none} in
65 all)
66 local dfname=$(basename $NewBackup)
67 cd $SDTD_BACKUP_ROOT
68 tar -czf $dfname.tar.gz $dfname
69 touch -r $dfname $dfname.tar.gz
70 rm -Rf $dfname
71 ;;
72 old)
73 if [ -d $LatestBackup ]; then
74 local dfname=$(basename $LatestBackup)
75 cd $SDTD_BACKUP_ROOT
76 tar -czf $dfname.tar.gz $dfname
77 touch -r $dfname $dfname.tar.gz
78 rm -Rf $dfname
79 fi
80 ;;
81 none)
82 ;;
83 esac
84
[61]85 cd $SDTD_BACKUP_ROOT
86
[52]87 ## Purge old/too many backups
88 local keepMin=${SDTD_BACKUP_MIN_BACKUPS_KEEP:-0}
89 if [ $(isANumber $SDTD_BACKUP_MAX_BACKUPS) -eq 1 ]; then
90 local removeBut=$SDTD_BACKUP_MAX_BACKUPS
91 if [ $SDTD_BACKUP_MAX_BACKUPS -lt $keepMin ]; then
92 removeBut=$keepMin
93 fi
94 local num=0
95 local F
96 for F in $(ls -t1 $SDTD_BACKUP_ROOT); do
97 (( num++ ))
98 if [ $num -gt $removeBut ]; then
[53]99 rm -Rf $F
[52]100 fi
101 done
102 fi
103 if [ $(isANumber $SDTD_BACKUP_MAX_AGE) -eq 1 ]; then
104 local FINDBASE="find $SDTD_BACKUP_ROOT -mindepth 1 -maxdepth 1"
105 # Only continue if there are more than MIN_BACKUPS_KEEP backups at all
106 if [ $($FINDBASE | wc -l) -gt $keepMin ]; then
107 local minutes=$(( $SDTD_BACKUP_MAX_AGE*60 ))
108 while [ $($FINDBASE -mmin -$minutes | wc -l) -lt $keepMin ]; do
109 minutes=$(( minutes+60 ))
110 done
111 $FINDBASE -mmin +$minutes -exec rm -Rf {} \;
112 fi
113 fi
114 if [ $(isANumber $SDTD_BACKUP_MAX_STORAGE) -eq 1 ]; then
115 local maxKBytes=$(( $SDTD_BACKUP_MAX_STORAGE*1024 ))
116 local curNumFiles=$(ls -t1 $SDTD_BACKUP_ROOT | wc -l)
[53]117 while [ $(du -sk $SDTD_BACKUP_ROOT | tr '[:blank:]' ' ' | cut -d\ -f1) -gt $maxKBytes -a $curNumFiles -gt $keepMin ]; do
[52]118 local toDel=$(ls -tr1 | head -n 1)
119 rm -Rf $toDel
120 (( curNumFiles-- ))
121 done
122 fi
[17]123
[26]124 for H in $(getHooksFor backup); do
[53]125 if [ "$SDTD_BACKUP_COMPRESS" = "all" ]; then
126 $H $NewBackup.tar.gz
127 else
128 $H $NewBackup
129 fi
[26]130 done
[308]131 for H in $(getHooksFor serverPostBackup); do
132 if [ "$SDTD_BACKUP_COMPRESS" = "all" ]; then
133 $H $NewBackup.tar.gz
134 else
135 $H $NewBackup
136 fi
137 done
[17]138}
139
140sdtdCommandBackupHelp() {
141 echo "Usage: $(basename $0) backup"
142 echo
143 echo "Backups all data files (instance configurations, save data, logs)."
144}
145
146sdtdCommandBackupDescription() {
147 echo "Backup game data files"
148}
Note: See TracBrowser for help on using the repository browser.