Pwsh Centos 7 Plex Backup

1 minute read

Description:

I had a bash script doing backups on Centos, but wanted to see if I could convert those to the core version of Powershell instead. Sure enough, worked on the first try:

To Resolve:

  1. Create a file called plex.ps1 and paste / run :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    <#
    
    # Register the Microsoft RedHat repository
    #curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
    # Install PowerShell
    #sudo yum install -y powershell
    # create /home/myuser/scripts/plex.ps1
    
    #>
    
    #!/usr/bin/env pwsh
    
    $Date = Get-Date -Format "yyyy-MM-dd"
    $log = "/home/myuser/scripts/logs/plexbackup_" + $date + ".log"
    
    If (!(Test-Path $log))
    {
          New-Item -Itemtype File -Path $log -Force | Out-Null
    }
    
    $stop = "systemctl stop plexmediaserver.service"
    $start = "systemctl start plexmediaserver.service"
    
    $options = "-zavh --exclude={'Cache/','Crash Reports/','Logs/','Plug-in Support/Cache/'}"
    $source = "/var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/"
    $destination = "/mnt/plex/backup"
    $rsync = "rsync $options $source $destination" 
    
    Write-Output "Starting Backup of Plex Database."  | Out-File $log -Append -Encoding "ascii"
    Write-Output "============================================================" | Out-File $log -Append -Encoding "ascii"
    
    # Stop Plex
    Write-Output "Stopping Plex Media Server" | Out-File $log -Append -Encoding "ascii"
    Write-Output "------------------------------------------------------------" | Out-File $log -Append -Encoding "ascii"
    bash -c $stop | Out-File $log -Append -Encoding "ascii"
    
    # Backup database
    Write-Output "Starting Backup." | Out-File $log -Append -Encoding "ascii"
    Write-Output "------------------------------------------------------------" | Out-File $log -Append -Encoding "ascii"
    bash -c $rsync | Out-File $log -Append -Encoding "ascii"
    
    # Restart Plex
    Write-Output "Starting Plex Media Server." | Out-File $log -Append -Encoding "ascii"
    Write-Output "------------------------------------------------------------" | Out-File $log -Append -Encoding "ascii"
    bash -c $start | Out-File $log -Append -Encoding "ascii"
    
    # Done
    Write-Output "Backup Complete" | Out-File $log -Append -Encoding "ascii"
    Write-Output "============================================================" | Out-File $log -Append -Encoding "ascii"
    

Comments