[LINUX] Backup of server materials using shell scripts (with file generation management)
Overview
- Since I had a local backup (generation management) of specific materials on the server every day, I will summarize the shell script as a memorandum.
- It's a technology that is quite refreshing now, but because it's warm and new,
Conclusion
- The created shell script is as follows.
- Please modify the directory etc. according to the environment as appropriate.
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)
- A memo when asked for such daily backup logs
- If you want to see it easily, it is better to build Jenkins or a job tool, but if it is not enough to cost it
- Create a parent shell that calls the backup shell described above.
(Please call this when preparing with cron etc.)
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