[LINUX] Backup of server materials using shell scripts (with file generation management)

Overview

Conclusion

backup.sh


#!/bin/sh -u

#Set the current directory at the location of the script
cd `dirname $0`

#Get processing start time
todaydatetime="date '+%Y-%m-%d %H:%M:%S'"
echo "Perform backup file processing"
eval $todaydatetime

#Start measuring processing time
SECONDS=0

#Backup target
TARGET_DIR='/home/origin'

#Directory for storing backup files
backupdirpath="/home/backup"

##Directory existence check
if [ -d ${backupdirpath} ]; then
    #If the backup directory exists,
    #Display the list of stored files
    ls -lrth ${backupdirpath}
else
    echo "There is no backup directory. The process ends."
    eval $todaydatetime
    exit 1
fi

#Define backup file name(* Make sure you know the date by the file name.)
BACKUP_FILE_NAME=`date +%y%m%d`

#Backup file creation process
tar czfp $BACKUP_FILE_NAME.tar.gz $TARGET_DIR
mv $BACKUP_FILE_NAME.tar.gz $BACKUP_DIR

#Generation management of backup files in a local backup folder
tgtfile="${backupdirpath}/backup_????-??-??.tar.gz"
chkage=$(ls -1 ${tgtfile}|wc -l)

#Set the number of storage generations
age="5"
##For the process of extracting and deleting the 6th and subsequent items with the tail command
##The value set for the variable is "Number of storage generations"+1 ”
tailage="+6"

#File list to be deleted
delfilelist=$(ls -1 -t ${tgtfile}|tail -n ${tailage})

##Judgment of file deletion necessity
if [[ ${chkage} -gt $age ]]; then
    #If the number of backup files is greater than the number of storage generations
    #Sort in reverse order and delete leaving the latest 5 generations
    echo "Number of generations stored in the folder:${chkage}"
    echo "Number of storage generations${age}Delete old files as it exceeds"
    echo "Files to be deleted:${delfilelist}"
    echo "Delete old backup"
    rm -f ${delfilelist}
else
    echo "There are no unnecessary backup files. The process ends."
    eval $todaydatetime
    exit 1
fi

#Get the processing end time
echo "Finish backup file processing"
eval $todaydatetime

#End of processing time measurement
echo "Processing execution time:${SECONDS}Seconds"

Bonus (shell script that creates daily backup logs)

top_backup.sh


#!/bin/sh -u

cd `dirname $0`

#Parent script that controls the backup script
sh ./backup.sh > logfile_`date "+%Y-%m-%d"`.log 2>&1

#Delete old log files
tgtfile="./logfile_????-??-??.log"
chkage=$(ls -1 ${tgtfile}|wc -l)
#Set the number of storage generations
age="5"
##For the process of extracting and deleting the 6th and subsequent items with the tail command
##The value set for the variable is "Number of storage generations"+1 ”
tailage="+6"
#File list to be deleted
delfilelist=$(ls -1 -t ${tgtfile}|tail -n ${tailage})

if [[ ${chkage} -gt $age ]]; then
    #If the number of backup files is greater than the number of storage generations
    #Sort in reverse order and delete leaving the latest 5 generations
    echo "Files to be deleted:${delfilelist}"
    echo "Delete old log files"
    rm -f ${delfilelist}
else
    exit 1
fi

Recommended Posts

Backup of server materials using shell scripts (with file generation management)
Server management with Jupyter (1) import
Process the contents of the file in order with a shell script
Edit the file of the SSH connection destination server on the server with VS Code