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

Last change on this file since 58 was 56, checked in by alloc, 10 years ago

Minor backup fix

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