Mik*_*ike 15 shell-script variable
我创建了一个脚本来在我的 CentOS 7 服务器上运行自动备份。备份存储到 /home/backup 目录。该脚本有效,但现在我想合并一种在备份发生后计算文件数的方法,如果数量超过 5,则删除最旧的备份。
以下是我的备份脚本。
#!/bin/bash
#mysqldump variables
FILE=/home/backup/databasebk_!`date +"Y-%m-%d_%H:%M"`.sql
DATABASE=database
USER=root
PASS=my password
#backup command process
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
#zipping the backup file
gzip $FILE
#send message to the user with the results
echo "${FILE}.gz was created:"
ls -l ${FILE}.gz
# This is where I would like to count the number of files
# in the directory and if there are more than 5 I would like
# to delete the oldest file. Any help is greatly appreciated
Run Code Online (Sandbox Code Playgroud)
谢谢-迈克
Bop*_*reH 19
在没有循环的纯 Bash 中:
ls -t | tail -n +6 | xargs -d '\n' rm
Run Code Online (Sandbox Code Playgroud)
解释:
ls -t
打印当前目录中的所有文件,按修改时间排序,最新的在前。tail -n +6
忽略前 5 行并打印第 6 行。xargs -d '\n' rm
删除传递给它的文件,每行一个。如果您绝对确定文件名中没有空格或引号,则可以仅使用xargs rm
.请注意,这会根据需要删除尽可能多的文件,只在目录中保留 5 个文件。如果您只想删除一个最旧的文件,请替换+6
为1
.
Ste*_*ris 12
你可以看看set -- /home/backup/databasebk_*
while$#
大于5,删除一个文件。
所以代码看起来类似于
set -- /home/backup/databasebk_*
while [ $# -gt 5 ]
do
echo "Removing old backup $1"
rm "$1"
shift
done
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为您选择的文件名自动按“最旧的”顺序排列。
为了保持一致性,我会设置一个变量(我通常称之为它,BASE
但你可以随意调用它)
所以
BASE=/home/backup/databasebk_
FILE=${BASE}!`date +"%Y-%m-%d_%H:%M"`.sql
....
set -- ${BASE}*
while [ $# -gt 5 ]
do
echo "Removing old backup $1"
rm "$1"
shift
done
Run Code Online (Sandbox Code Playgroud)
小智 12
我认为更好的方法是使用 logrotate,因为此应用程序已经执行了您尝试使用脚本实现的操作。我在我的测试服务器上测试了以下脚本。
$ mysqldump -uroot -pmy5trongpass database > mydatabase.sql
$ ls
$ mydatabase.sql
Run Code Online (Sandbox Code Playgroud)
未创建第一个数据库时的错误示例。
#**logrotate -f** will force our new rule to run now and backup the database
#this is for testing purposes.
$ logrotate -f /etc/logrotate.d/mysql_backup
error: stat of /home/backups/mydatabase.sql failed: No such file or directory
Run Code Online (Sandbox Code Playgroud)
$ cat /etc/logrotate.d/mysql_backup
/home/backups/mydatabase.sql{
create 640 root mysql
daily
rotate 2
nocompress
postrotate
mysqldump -uroot -pmy5trongpass test > mydatabase.sql;
echo "done"
endscript
}
Run Code Online (Sandbox Code Playgroud)
$ logrotate -f /etc/logrotate.d/mysql_backup
Run Code Online (Sandbox Code Playgroud)
$ logrotate -f /etc/logrotate.d/mysql_backup
done
$ ll
total 16
-rw-r-----. 1 root mysql 1261 Feb 3 21:46 mydatabase.sql
-rw-r-----. 1 root mysql 1261 Feb 3 21:44 mydatabase.sql.1
-rw-r-----. 1 root mysql 1261 Feb 3 21:44 mydatabase.sql.2
Run Code Online (Sandbox Code Playgroud)
您将看到将创建一个新文件,后跟一个数字,可以使用以下选项将其更改为显示日期
https://linux.die.net/man/8/logrotate
dateext
Archive old versions of log files adding a daily extension like YYYYMMDD instead of simply adding a number. The extension may be configured using the dateformat option.
dateformat format_string
Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the character separating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.
Run Code Online (Sandbox Code Playgroud)
使用zsh
外壳:
rm -f /home/backup/databasebk_*.sql.gz(.om[6,-1])
Run Code Online (Sandbox Code Playgroud)
这将创建一个匹配的常规文件的路径名列表,根据文件/home/backup/databasebk_*.sql.gz
的修改时间戳对列表进行排序(最近修改的最先),并提取元素 6 到最后。然后将这些文件传递给rm -f
删除。
文件名通配模式末尾的glob 限定符,(.om[6,-1])
指定了常规文件( .
),按修改时间排序( om
),并且应该返回第 6 次到最后一个匹配项( [6,-1]
)。
归档时间: |
|
查看次数: |
4237 次 |
最近记录: |