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

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

Version 14: Fixes #12

File size: 2.6 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 # Check for backup folder existence
10 if [ -e $SDTD_BACKUP_ROOT ]; then
11 # Exists, copy(link) latest backup
12 unset -v LatestBackup
13 local fileI
14 for fileI in "$SDTD_BACKUP_ROOT"/*; do
15 if [ $fileI -nt $LatestBackup -a -d $fileI ]; then
16 LatestBackup=$fileI
17 fi
18 done
19 if [ -d $LatestBackup ]; then
20 cp -al $LatestBackup $NewBackup
21 fi
22 fi
23
24 if [ ! -d $SDTD_BACKUP_ROOT ]; then
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
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
66 rm $F
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)
84 while [ $(du -sk $SDTD_BACKUP_ROOT) -gt $maxKBytes -a $curNumFiles -gt $keepMin ]; do
85 local toDel=$(ls -tr1 | head -n 1)
86 rm -Rf $toDel
87 (( curNumFiles-- ))
88 done
89 fi
90
91 for H in $(getHooksFor backup); do
92 $H $NewBackup
93 done
94}
95
96sdtdCommandBackupHelp() {
97 echo "Usage: $(basename $0) backup"
98 echo
99 echo "Backups all data files (instance configurations, save data, logs)."
100}
101
102sdtdCommandBackupDescription() {
103 echo "Backup game data files"
104}
Note: See TracBrowser for help on using the repository browser.