0

I have the bash script which makes pg_dumpall and uploads to my FTP server.

#/bin/bash
timeslot=`date +\%Y-\%m-\%d-\%H:\%M:\%S`
backup_dir="/var/lib/backup"
name=recycle_backup_$timeslot.sql.gz
set -e 

find /var/lib/backup/*.sql.gz -mtime +7 -type f -exec rm -rvf {} \; -exec curl -p --insecure ftp://aaa.bbb/backup/{} --user user:password -Q 'DELE backup/'{} \;

echo $timeslot "Running docker container to backup Postgresql"

sudo docker run --rm \
        -t \
        --name postgres-backup \
        -v "/var/lib/backup":"/mnt" \
        -w "/mnt" \
        -e PGPASSWORD="password" \
        --entrypoint=/usr/bin/pg_dumpall \
        postgres:9 \
        -h 1.2.3.4 -U postgres | gzip -9 > $backup_dir/$name


echo "Your back is successfully stored in " $backup_dir

curl -T /var/lib/backup/$name ftp://aaa.bbb/backup/ --user user:password

It cleans up all old .sql.gz files (older than 7 days).

After these steps, the script makes a docker container with Postgres, runs commands to make backup and saves it locally to /var/lib/backup/filename.sql.gz.

The problem is I can't clean up files on my FTP server with arguments that "find" returns.

find /var/lib/backup/*.sql.gz -mtime +7 -type f -exec rm -rvf {} \; -exec curl -p --insecure ftp://aaa.bbb/backup/{} --user user:password -Q 'DELE backup/'{} \;

How to add the argument {} from find command to this curl request? -Q 'DELE BACKUP/'{} \;

Thanks for the support.

2 Answers2

0

That's not going to work, you can only use {} once with find.

Maybe try this approach? Remove the echo if this looks sane ...

find /var/lib/backup/*.sql.gz -mtime +7 -type f -exec rm -rvf {} + | awk '{print gensub( /...(.*)/, "\\1","1",$2)}' | while read -s file 
do 
  echo curl -p --insecure ftp://aaa.bbb/backup/{} --user user:password -Q 'DELE backup/'${file}
done
tink
  • 14,342
  • 4
  • 46
  • 50
  • I tried the command below to filter outdated files and print their names, but this doesn't work. find /var/lib/backup/*.sql.gz -mtime +7 -type f -exec echo {} + | awk '{print gensub( /...(.*)/, "\\1","1",$2)}' – qlimenoque Dec 19 '19 at 09:25
  • This command cannot find files older than X days and print their pathes – qlimenoque Dec 20 '19 at 17:11
  • Right you are. I didn't have any "old files" for testing, so stripped that condition out. Fixing that now. – tink Dec 20 '19 at 19:17
0

Solved the problem with a second script, which connects to FTP server and deletes outdated files in a loop from this topic Linux shell script for delete old files from ftp