How to Purge A Directory After X days

Hi everyone,
I have a bash script install on my Cent OS 6 to auto backup my website and upload into GG drive
Here is full : http://pastebin.com/u3grLZqD

My remote connection to GG drive is remote
According to this script, it will create and upload a backup folder $SERVER_NAME/$TIMESTAMP to GG drive and auto delete backup older than 2weeks
Folder on GG drive will be like that:

  • SERVER_NAME/2017-01-12
  • SERVER_NAME/2017-01-13
  • SERVER_NAME/2017-01-14
    But, it just delete content inside, not delete folder (folder become empty, have to delete manually)
    This line for deleting:

/usr/sbin/rclone -q --min-age 2w delete “remote:$SERVER_NAME”

If i change to purge, it will delete everything (include biggest folder SERVER_NAME)

/usr/sbin/rclone -q --min-age 2w purge “remote:$SERVER_NAME”

If i change to /**, directory not found

/usr/sbin/rclone -q --min-age 2w purge “remote:$SERVER_NAME/**”

My question is:

  1. Why remote:SERVER_NAME/** dont match remote:SERVER_NAME/2017-01-11
    How can I find and delete sub folder of remote:SERVER_NAME
  2. How can i fix this line to auto remove all backups older than 2 week

rclone’s folder handling is still a bit erratic - see https://github.com/ncw/rclone/issues/100 - that is why the empty folders aren’t being removed. purge does remove the directories though.

You can use rclone rmdirs to help manage them: http://rclone.org/commands/rclone_rmdirs/

Use delete and rmdir but it doesnt’ help because each directory have sub folder inside (although sub folder is empty)
It’s : My Drive > HOCVPS_BACKUP > YYYY-MM-DD > mysql (or nginx)
Level 1: HOCVPS_BACKUP
Level 2: Folder backup for each day, YYYY-MM-DD
Level 3: File zip compress site, MySQL folder, Nginx folder
Level 4: mysql backup file in MySQL folder and Nginx conf file in Nginx folder
So, delete help to delete file zip compress site, mysql backup file, nginx config file
But rmdir cannot remove YYYY-MM-DD cause YYYY-MM-DD have 2 empty folder inside
Here is my code:

/usr/sbin/rclone -q --min-age 2d delete “remote:$SERVER_NAME”
/usr/sbin/rclone -q --min-age 2d rmdirs “remote:$SERVER_NAME”

It saids:

2017/01/16 16:09:15 Failed to rmdir: directory not empty: []*drive.ChildReference{(*drive.ChildReference)(0xc4201a6770), (*drive.ChildReference)(0xc4201a6850), (*drive.ChildReference)(0xc4201a6930), (*drive.ChildReference)(0xc4201a6a10), (*drive.ChildReference)(0xc4201a6af0), (*drive.ChildReference)(0xc4201a6bd0)}

Besides, why i cann’t use * to find sub-directory? For examples

#rclone -q --min-age 2d rmdir ‘remote:BACKUP/*’
2017/01/16 16:25:59 Failed to rmdir: directory not found

This line cannot find and remove any empty sub-directory inside BACKUP
How to use this filtering * to match sub folder : http://rclone.org/filtering/

You need the rmdirs command - note extra s - that should fix all your issues.

rclone rmdirs remote:HOCVPS_BACKUP

Error: unknown command “rmdirs” for “rclone”

Did you mean this?
rmdir

Run ‘rclone --help’ for usage.

Seem I need update to 1.35 :slight_smile: Thank you. It worked

i still got those error :

pi@raspberrypi:~ $ rclone -q --min-age 1s rmdirs google_drive:marabahan_backup/by_date/*
2018/01/29 23:21:59 ERROR : Google drive root 'marabahan_backup/by_date/*': Failed to list "": directory not found
2018/01/29 23:22:00 ERROR : : Failed to rmdir: directory not empty
2018/01/29 23:22:00 ERROR : Attempt 1/3 failed with 2 errors and: directory not empty
2018/01/29 23:22:00 ERROR : Google drive root 'marabahan_backup/by_date/*': Failed to list "": directory not found
2018/01/29 23:22:01 ERROR : : Failed to rmdir: directory not empty
2018/01/29 23:22:01 ERROR : Attempt 2/3 failed with 2 errors and: directory not empty
2018/01/29 23:22:01 ERROR : Google drive root 'marabahan_backup/by_date/*': Failed to list "": directory not found
2018/01/29 23:22:02 ERROR : : Failed to rmdir: directory not empty
2018/01/29 23:22:02 ERROR : Attempt 3/3 failed with 2 errors and: directory not empty
2018/01/29 23:22:02 Failed to rmdirs: directory not empty

i already make sure when doing rclone lsd that there are subfolder in those remote folder.
why this can be happen as previous answer seems already solved ?

thanks you

You don’t need the * on the end

1 Like