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

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

Fix backup removal

File size: 2.8 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 # Check for backup folder existence
14 if [ -e $SDTD_BACKUP_ROOT ]; then
15 # Exists, copy(link) latest backup
16 unset -v LatestBackup
17 local fileI
18 for fileI in $(find "$SDTD_BACKUP_ROOT" -mindepth 1 -maxdepth 1 -type d); do
19 if [ "$fileI" -nt "$LatestBackup" ]; then
20 LatestBackup=$fileI
21 fi
22 done
23 if [ -d "$LatestBackup" ]; then
24 cp -al "$LatestBackup" "$NewBackup"
25 fi
26 fi
27
28 if [ ! -d $SDTD_BACKUP_ROOT ]; then
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
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 cd $SDTD_BACKUP_ROOT
59
60 ## Purge old/too many backups
61 local keepMin=${SDTD_BACKUP_MIN_BACKUPS_KEEP:-0}
62 if [ $(isANumber $SDTD_BACKUP_MAX_BACKUPS) -eq 1 ]; then
63 local removeBut=$SDTD_BACKUP_MAX_BACKUPS
64 if [ $SDTD_BACKUP_MAX_BACKUPS -lt $keepMin ]; then
65 removeBut=$keepMin
66 fi
67 local num=0
68 local F
69 for F in $(ls -t1 $SDTD_BACKUP_ROOT); do
70 (( num++ ))
71 if [ $num -gt $removeBut ]; then
72 rm -Rf $F
73 fi
74 done
75 fi
76 if [ $(isANumber $SDTD_BACKUP_MAX_AGE) -eq 1 ]; then
77 local FINDBASE="find $SDTD_BACKUP_ROOT -mindepth 1 -maxdepth 1"
78 # Only continue if there are more than MIN_BACKUPS_KEEP backups at all
79 if [ $($FINDBASE | wc -l) -gt $keepMin ]; then
80 local minutes=$(( $SDTD_BACKUP_MAX_AGE*60 ))
81 while [ $($FINDBASE -mmin -$minutes | wc -l) -lt $keepMin ]; do
82 minutes=$(( minutes+60 ))
83 done
84 $FINDBASE -mmin +$minutes -exec rm -Rf {} \;
85 fi
86 fi
87 if [ $(isANumber $SDTD_BACKUP_MAX_STORAGE) -eq 1 ]; then
88 local maxKBytes=$(( $SDTD_BACKUP_MAX_STORAGE*1024 ))
89 local curNumFiles=$(ls -t1 $SDTD_BACKUP_ROOT | wc -l)
90 while [ $(du -sk $SDTD_BACKUP_ROOT | tr '[:blank:]' ' ' | cut -d\ -f1) -gt $maxKBytes -a $curNumFiles -gt $keepMin ]; do
91 local toDel=$(ls -tr1 | head -n 1)
92 rm -Rf $toDel
93 (( curNumFiles-- ))
94 done
95 fi
96
97 for H in $(getHooksFor backup); do
98 if [ "$SDTD_BACKUP_COMPRESS" = "all" ]; then
99 $H $NewBackup.tar.gz
100 else
101 $H $NewBackup
102 fi
103 done
104}
105
106sdtdCommandBackupHelp() {
107 echo "Usage: $(basename $0) backup"
108 echo
109 echo "Backups all data files (instance configurations, save data, logs)."
110}
111
112sdtdCommandBackupDescription() {
113 echo "Backup game data files"
114}
Note: See TracBrowser for help on using the repository browser.