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

Last change on this file since 163 was 67, checked in by alloc, 10 years ago

v25: Fixes #22, #23. Also adds new serverconfig options

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