Script Name: mega_del_old.sh

Description

Delete files from a directory that are older then a given number of days for a Mega.nz account.


Configuration

There are configuration settings that can go into .mega_scriptsrc for section [MEGA_DELETE_OLD]

See Configuration file.

Parameters

-p

Optional: The full path directory to delete older files from Mega.nz.
Default is "/Root"

-a

Optional: The number of days as a positive integer before todays date to delete files older then from Mega.nz.
Default is 60 days.

-i

Optional: The configuration file to use that contain the credentials for the Mega.nz account you want to access.
See Set up .megarc.

-o

Optional: The Log file to log to results of running mega_del_old.sh.
Can be value of "t" in which case results are outputted to the terminal window
Can be value of "s" in which case no results are written to a log file or terminal window.
Defaults to /var/log/mega_delete_old.log (requires script be run as root)
Setting -o option overrides the LOG option of [MEGA_DELETE_OLD] of Configuration file.

-d

Optional: Date of Log in the format of yyyy-mm-dd-hh-mm-ss. This will be used as the date stamp in the log file
Example: 2018-06-21-14-31-22
If -o is set to "s" then -d will be ignored.

-h

Optional: Display Script help

-v

Optional: Display script version


Possible Codes returned from Script

Code

Definition

0

Normal Exit. No Errors Encountered.

3

No write privileges to create log file.

4

Log file exist but no write privileges.

100

There is another mega process running. Can not continue.

101

megarm not found. Megtools requires installing or properly configure MT_MEGA_RM option of .mega_scriptsrc to point to the megarm location

102

megals not found. Megtools requires installing or properly configure MT_MEGA_LS option of .mega_scriptsrc to point to the megals location

105

megadf can not be found. Megtools requires installing or properly configure MT_MEGA_DF option of .mega_scriptsrc to point to the megadf location

111

Optional argument -i was passed in but the config can not be found or we do not have read permissions


Remarks

Never pass in the directory Root to mega_del_old.sh as the frist part of the path.

For -p -p '/MyPath/myfile' is fine; however, -p '/Root/MyPath/myfile' will fail.


If -i is omitted then you must have .megarc set in the current users home directory.


-o is optional but useful when calling mega_upload_file.sh from a master script such as mega_db_save_upload.sh (source).


-d is optional but also useful when calling mega_upload_file.sh from a master script such as mega_db_save_upload.sh (source). By passing Log and Date to mega_upload_file.sh then one log file will contain the same datestamp for all of the other called related scripts.


If the name of this file "mega_del_old.sh" is change for any reason then it needs to be reflected in the configuration file. See Configuration File.

Example

In this example we are deleting anything older then 60 days in the "/example/test" directory and sending the output to the terminal by setting -o to "t"


# /bin/bash /usr/local/bin/mega_del_old.sh -p '/example/test' -a 60 -o t
2018-06-08-19-22-35 MEGA DELETE OLD: Processing Files older than 'Mon Apr  9 19:22:35 DST 2018' for folder '/Root/example/test'
2018-06-08-19-22-35 MEGA DELETE OLD: Total Files: 2
2018-06-08-19-22-35 MEGA DELETE OLD: Deleted Files: 0
2018-06-08-19-22-35 MEGA DELETE OLD: Total: 50.0 GiB Used:  132 bytes Free:  50.0 GiB


In this example we are deleting anything older then 1 days in the "/example/test" directory and sending the output to the terminal by setting -o to "t"

You can see that there are two file deleted that are older then one day.


# /bin/bash /usr/local/bin/mega_del_old.sh -p '/example/test' -a 1 -o t
2018-06-08-19-23-35 MEGA DELETE OLD: Processing Files older than 'Thu Jun  7 19:23:35 DST 2018' for folder '/Root/example/test'
2018-06-08-19-23-35 MEGA DELETE OLD: Deleted '/Root/example/test/hello world.txt' with modifed date of: 2018-06-07
2018-06-08-19-23-35 MEGA DELETE OLD: Deleted '/Root/example/test/hello.txt' with modifed date of: 2018-06-06
2018-06-08-19-23-35 MEGA DELETE OLD: Total Files: 2
2018-06-08-19-23-35 MEGA DELETE OLD: Deleted Files: 2
2018-06-08-19-23-35 MEGA DELETE OLD: Total: 50.0 GiB Used:  108 bytes Free:  50.0 GiB


If you want to delete all files in a directory you can set -a to 0.

In the following example we upload file ~/hello.txt and then delete it by setting -a to 0 and set -o to "t" to output the results to the terminal.


# /bin/bash /usr/local/bin/mega_upload_file.sh -p '/example/test' -l ~/hello.txt; echo $?
0

# /bin/bash /usr/local/bin/mega_del_old.sh -p '/example/test' -a 0 -o 't'
2018-06-08-19-37-52 MEGA DELETE OLD: Processing Files older than 'Fri Jun  8 19:37:52 DST 2018' for folder '/Root/example/test'
2018-06-08-19-37-52 MEGA DELETE OLD: Deleted '/Root/example/test/hello.txt' with modifed date of: 2018-06-08
2018-06-08-19-37-52 MEGA DELETE OLD: Total Files: 1
2018-06-08-19-37-52 MEGA DELETE OLD: Deleted Files: 1
2018-06-08-19-37-52 MEGA DELETE OLD: Total: 50.0 GiB Used:  108 bytes Free:  50.0 GiB


Related

mega_del_old.sh Latest Source